Cs 106 computing fundamentals ii chapter 210 adding controls to user forms
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

CS 106 Computing Fundamentals II Chapter 210 “ Adding Controls to User Forms ” PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin. CS 106 Computing Fundamentals II Chapter 210 “ Adding Controls to User Forms ”. Syllabus. Goal Adding Controls Mac vs. Windows

Download Presentation

CS 106 Computing Fundamentals II Chapter 210 “ Adding Controls to User Forms ”

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 106 computing fundamentals ii chapter 210 adding controls to user forms

Herbert G. Mayer, PSU CS

Status 7/4/2013

Initial content copied verbatim from

CS 106 material developed by

CS professors: Cynthia Brown & Robert Martin

CS 106Computing Fundamentals IIChapter 210“Adding Controls to User Forms”


Syllabus

Syllabus

  • Goal

  • Adding Controls

  • Mac vs. Windows

  • Double Click Form Name to Get Controls

  • Useful Controls

  • Writing Codes


Cs 106 computing fundamentals ii chapter 210 adding controls to user forms

Goal

  • Presentation “Create_User_Form” is paired with this presentation “Add_Control_2_User_Form”

  • Overall goal is to show how to enter a string and then display the concatenated string in these steps:

  • 1 A label prompts user to enter a string

  • 2 A text box reads this string input, e.g. “Cindy”

  • 3 A button, when subsequently pushed, causes the string concatenation and display event

  • 4 A second text box displays “This is: Cindy!”


Adding controls

Adding Controls

  • A user form isn’t much use without some controls

  • We’re going to add controls and write code for them

  • Note that the code to open the form should belong to the workbook project

  • The code for controls on the form, though, belongs to the form, and is on a separate code window


Mac vs windows

Mac vs. Windows

  • The following slides show the Windows version

  • There are videos of doing both versions, and there are a few slides at the end of this presentation that show how things look on the Mac

  • Mac users should read through the whole presentation as not everything is duplicated


Double click f orm n ame to g et code

Double click Form Name to Get Code

Click here


Ready to add controls

Ready to add controls…

This is the design view of the form, where you can add controls and set their properties.


Useful controls

Useful Controls

  • Listboxes: for printing data for the user to see. Also good for giving a list of choices to pick from

  • Command Button: clicking the button creates an event we can write a program for

  • Labels: used to put useful information and instructions on the form; can serve as prompt for input

  • Check boxes: used to offer options where the user can pick any number from none to all


Useful controls1

Useful Controls

  • Option buttons: used to offer options where exactly one should be chosen

  • Frames: used to group controls like option buttons; needed if there is more than one set of options

  • Textboxes: a place for the user to type input

  • Others we won’t use in CS 106


Events for controls

Events for Controls

  • Most things you do with a control, like checking a check box, typing in a text box, or clicking a command button, are events in VBA

  • You can write code to handle any control event

  • If you don’t write code for an event, then nothing happens

  • We will typically use the command button control to trigger execution of our program


Finding userform code page

Finding UserForm Code Page

If you click on the Window item, you can move between the code page and design page for the form and the code page for the workbook.


The form code page

The Form Code Page

This looks a lot like the window for the workbook code, so be careful you use the right window! I just typed Option Explicit at the top.


Adding four controls

Adding Four Controls

A label. I changed the caption, font, and size properties

A text box. I changed the name to txtName.

A label. I changed the name to lblHello, the font to a large size, and the Visible property to False.

I changed the caption property and the name to btnPrint


Names for controls

Names for Controls

  • Controls that are referred to in the code should have meaningful names

  • Start with a three letter prefix that identifies the control: btn for button, lbl for label, txt for text box, etc.

  • You can leave the names unchanged on controls that are not referred to, like most labels


Writing the code

Writing the Code

  • To get started, double click on the control you want to write an event routine for

  • Works if you have already opened the code window for your form

  • VBA will guess the name of the routine and put the procedure header on the code page, all set for you to start coding


Double click on btnprint

Double Click on btnPrint


My code changes the properties of the label lblhello

My code changes the properties of the label lblHello:

Option Explicit

'***************************************************************

'Demonstrate the use of controls on a user form

'***************************************************************

Private Sub btnPrint_Click()

Dim name As String

'*** read the name from the text box

name = txtName.Text

'*** create the caption and show the label

lblHello.Caption = "Hello, " & name & "!"

lblHello.Visible = True

End Sub


Push f5 to run the code or use the run menu

Push F5 to run the code, or use the Run menu


After pushing the button

After Pushing the Button:


Try it yourself

Try it Yourself!

  • You can download the example program and try using it

  • Try making some changes in the properties, fonts, colors, etc. of the various controls, and in the layout of the user interface

  • Also try adding a button that does something to the text in the large label


Mac version

Mac version


Adding controls1

Adding Controls

  • A blank user form isn’t much use!

  • Let’s add a few controls.

  • When using controls that our program refers to, we give them meaningful names.

  • We have a standard way to construct names. Form names start with frm; buttons with btn; text boxes with txt; labels with lbl


Watch the video

Watch the Video

  • There’s a video that shows the construction of this user form application

  • The following slides show some of the major highlights


Add controls

Add controls

I’ve added two labels, a text box, and a command button, using the palette at right to choose my controls, and the properties window to set their properties.


The large l abel

The Large Label

I changed the font size on the large label to 16, made it a bold font, and changed the name to lblHello. I set its Visible property to False, so you don’t see it when you start the program.


C ommand b utton

Command Button

I changed the name of the button to btnClick and the caption to read “Now Click This Button”. I changed the font size to 12, as well.


The code

The Code

  • Option Explicit

  • '******************************************************

  • ' Read name from text box and print on label

  • '******************************************************

  • Private Sub btnClick_Click()

  • Dim name As String

  • name = txtName.Text

  • lblHello.Caption = "Hello, " & name & "!"

  • lblHello.Visible = True ‘important so we see the message!

  • End Sub


Try it yourself1

Try it Yourself!

  • You can download the example program and try using it

  • Try making some changes in the properties, fonts, colors, etc. of the various controls, and in the layout of the user interface

  • Also try adding a button that does something to the text in the large label


  • Login