Upload
dorothy-brittney-blankenship
View
220
Download
1
Embed Size (px)
Citation preview
1
CS 106Computing Fundamentals II
Chapter 29“Creating A User Form”
Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/4/2013Status 7/4/2013
Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by
CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin
2
Syllabus A Better InterfaceA Better Interface GoalGoal Windows Version . . . detailedWindows Version . . . detailed Change NameChange Name PropertiesProperties Mac Version . . . Abbreviated, but see Mac Version . . . Abbreviated, but see
videovideo Showing FormShowing Form Viewing CodeViewing Code
3
A Better Interface
• Our programs can use input boxes for input Our programs can use input boxes for input and write on the spreadsheet page for and write on the spreadsheet page for outputoutput
• This works ok but is not very elegant or This works ok but is not very elegant or flexibleflexible
• VBA gives us a way to create a custom VBA gives us a way to create a custom form, known as the form, known as the User FormUser Form, to use as an , to use as an interface for your programinterface for your program
• We’ll skim the Windows version, then look We’ll skim the Windows version, then look in detail at the Mac versionin detail at the Mac version
4
Goal
• Presentation “Create_User_Form” is paired with Presentation “Create_User_Form” is paired with “Add_Control_2_User_Form”“Add_Control_2_User_Form”
• Overall goal is to show how to enter a string Overall goal is to show how to enter a string and then display the concatenated string in and then display the concatenated string in these steps:these steps:
• 1 A label prompts user to enter a string1 A label prompts user to enter a string
• 2 A text box reads this string input, e.g. 2 A text box reads this string input, e.g. “Cindy”“Cindy”
• 3 A button, when subsequently pushed, causes 3 A button, when subsequently pushed, causes the string concatenation and display eventthe string concatenation and display event
• 4 A second text box displays “This is: Cindy!”4 A second text box displays “This is: Cindy!”
5
Windows version
6
To Get Started…
Open a new workbook, go to the Developer tab, and click Visual Basic.In VBA, double click ThisWorkBook in the project window to show the code sheet, and type OptionExplicit. Then click the Insert UserForm icon, and choose UserForm
7
Things to Notice
Your new user form. Grab the bottom corner and drag to make it bigger.
The toolbox has items you can put on the user form. If you don’t see it, click the toolbox icon (highlighted above)
Properties of the selected form or tool
8
Change the Name of the Form
Name is the first propertyIn the list.
We will refer to the form by its name in our program.
9
Change the Caption
Captionproperty
Here’s the caption. The user will see it when using the form. The user will not see the name.
10
Other Properties
• There are plenty of other properties to There are plenty of other properties to play with. You can try them out. play with. You can try them out.
• One fun one is the background color. Let’s One fun one is the background color. Let’s set it to something other than white…set it to something other than white…
11
User Form Made Yellow
The BackColor property
The colors are reached through the little down arrow at the right of the property line. System colors are the ones Windows is using for your system. The Palette gives you some other options.
12
Showing the Form
• Unless you make it happen, the form won’t show Unless you make it happen, the form won’t show up in your spreadsheet programup in your spreadsheet program
• The line of code that makes the form show up The line of code that makes the form show up is is
• formName.ShowformName.Show
• Let’s make it show up when you open the Let’s make it show up when you open the workbook, using the workbook, using the Workbook_Open()Workbook_Open() event event procedure.procedure.
• Double click in the project window to get back Double click in the project window to get back to the ThisWorkbook project, and type the to the ThisWorkbook project, and type the code. Then close the workbook and reopen itcode. Then close the workbook and reopen it
13
Here is the code…
14
Screen, after Opening Workbook
15
Mac version
16
To Get Started
Go to the Developer tab and then to theVBA editor. Under the InsertMenu, click Userform.
17
The new userform
Controls you canadd to the userform
Properties window showingUserform properties
Showing itIn the project
18
Changing the Properties
• Use the properties window to change the Use the properties window to change the propertiesproperties
• For each item, a separate properties For each item, a separate properties window opens, but only after being clicked window opens, but only after being clicked = selected!= selected!
• We’ll change the name to frmExampleWe’ll change the name to frmExample
• And we’ll make it bigger by changing the And we’ll make it bigger by changing the height to 450 and width to 600height to 450 and width to 600
• We’ll also change the caption to “Welcome”We’ll also change the caption to “Welcome”
• And the backcolor to yellowAnd the backcolor to yellow
19
I couldn’t find a yellow I liked on the color wheel, so I used the “crayons” to select one
20
Here is the yellow form after I clicked OK.
21
Showing Form
• Right now there is nothing that makes the Right now there is nothing that makes the form show up when you are in the workbook.form show up when you are in the workbook.
• The command The command frmExample.ShowfrmExample.Show makes it makes it visiblevisible
• We could put this in a macro that is We could put this in a macro that is activated by clicking a button, but let’s activated by clicking a button, but let’s put it in the special put it in the special Workbook_Open()Workbook_Open() macro that makes it show up automatically, macro that makes it show up automatically, as a side-effect of the opening event, as a side-effect of the opening event, when you open the workbook.when you open the workbook.
22
I double clicked hereto get the code windowfor this workbook
I typed my code here
23
Viewing Code
Option ExplicitOption Explicit
'*****************************************'*****************************************
' Show form frmExample when the workbook ' Show form frmExample when the workbook opensopens
'*****************************************'*****************************************
SubSub Workbook_Open() Workbook_Open()
frmExample.ShowfrmExample.Show
End SubEnd Sub
24
Save and quit…
• Save the workbook as a Macro-enabled Save the workbook as a Macro-enabled (.xlsm) workbook and then quit Excel(.xlsm) workbook and then quit Excel
• The next slide shows the re-opened The next slide shows the re-opened workbook with enabled macrosworkbook with enabled macros
25
The user form
caption