1 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.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming.
Creating a User Form. A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for output This works ok but is.
Using Macros and Visual Basic for Applications (VBA) with Excel
Text Box controls are used when users are required to type some input (during program execution), or output is displayed on the form (known as the user-
Visual Basic for Applications Class III. User Forms  We place controls on User Forms to get input from the user.  Common controls include text boxes,
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
05/09/ Introducing Visual Basic Sequence Programming.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
Chapter 2 - More Controls More controls – Text boxes - used for user input – Frames - containers to group items – check boxes - user select an option -
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Visual Basic.net Textbox & Label Controls. Textbox Naming convention (txt) Primary use (user input) Can except numeric and character values.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
CS130 Project 1 A simple VB application ("project" or program): user enters amount of sales then clicks the "Calculate button", the application displays.
What is Visual Basic.NET? 110 B-1 e.g. a word processor doesn’t do anything until the user clicks on a button, types text... A programming language that.
CSC 230 (Blum)1 Visual Basic 2005 Hello World Fall 2005 T. Blum.
CSC 157 (Blum)1 Hello World. CSC 157 (Blum)2 Start/Programs/Microsoft Visual Studio.NET 2003/Microsoft Visual Studio.NET 2003.
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106.
1 Advanced Computer Programming Lab Calculator Project.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Showing the Developer Tab. Windows vs Mac This is one of the places where the Mac and Windows procedures are different. We’ll look at the Windows method.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
VB Objects & Events (Exercises) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 3, Friday 1/31/2003)
COMPUTER PROGRAMMING I 3.02 Apply Properties Associated with the Controls.
VAT Calculator program Controls Properties Code Results.
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
CMPF114 Computer Literacy Chapter 3 The Visual Basic Environment 1.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
CSC 230 (Blum)1 Visual Basic 2005 Hello World Fall 2005 T. Blum.
Sorry, the pen lost control. Anyway, you need to download both the.vbp and the.frm for each project. I looked at PrCalc, I did not look at inventory.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Using Forms and Form Elements In Visual Basic.NET.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
3.01 Apply Controls Associated With Visual Studio Form
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
User Forms.
Creating and executing a program
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Building an Application in the Visual Basic .NET Environment
Introduction to TouchDevelop
Herbert G. Mayer, PSU CS Status 8/2/2013
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Introduction to Programming
Group Boxes, Radio buttons and Checked List Boxes
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

1 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

2 Syllabus Goal Goal Adding Controls Adding Controls Mac vs. Windows Mac vs. Windows Double Click Form Name to Get Controls Double Click Form Name to Get Controls Useful Controls Useful Controls Writing Codes Writing Codes

3 Goal Presentation “Create_User_Form” is paired with this presentation “Add_Control_2_User_Form”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: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 string1 A label prompts user to enter a string 2 A text box reads this string input, e.g. “Cindy”2 A text box reads this string input, e.g. “Cindy” 3 A button, when subsequently pushed, causes the string concatenation and display event3 A button, when subsequently pushed, causes the string concatenation and display event 4 A second text box displays “This is: Cindy!”4 A second text box displays “This is: Cindy!”

4 Adding Controls A user form isn’t much use without some controlsA user form isn’t much use without some controls We’re going to add controls and write code for themWe’re going to add controls and write code for them Note that the code to open the form should belong to the workbook projectNote 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 windowThe code for controls on the form, though, belongs to the form, and is on a separate code window

5 Mac vs. Windows The following slides show the Windows versionThe 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 MacThere 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 duplicatedMac users should read through the whole presentation as not everything is duplicated

6 Double click Form Name to Get Code Click here

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

8 Useful Controls Listboxes: for printing data for the user to see. Also good for giving a list of choices to pick fromListboxes: 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 forCommand 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 inputLabels: 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 allCheck boxes: used to offer options where the user can pick any number from none to all

9 Useful Controls Option buttons: used to offer options where exactly one should be chosenOption 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 optionsFrames: 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 inputTextboxes: a place for the user to type input Others we won’t use in CS 106Others we won’t use in CS 106

10 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 VBAMost 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 eventYou can write code to handle any control event If you don’t write code for an event, then nothing happensIf you don’t write code for an event, then nothing happens We will typically use the command button control to trigger execution of our programWe will typically use the command button control to trigger execution of our program

11 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.

12 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.

13 Adding Four Controls A label. I changed the caption, font, and size properties A text box. I changed the name to txtName. I changed the caption property and the name to btnPrint A label. I changed the name to lblHello, the font to a large size, and the Visible property to False.

14 Names for Controls Controls that are referred to in the code should have meaningful namesControls 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.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 labelsYou can leave the names unchanged on controls that are not referred to, like most labels

15 Writing the Code To get started, double click on the control you want to write an event routine forTo 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 formWorks 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 codingVBA will guess the name of the routine and put the procedure header on the code page, all set for you to start coding

16 Double Click on btnPrint

17 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 Dim name As String '*** read the name from the text box '*** read the name from the text box name = txtName.Text name = txtName.Text '*** create the caption and show the label '*** create the caption and show the label lblHello.Caption = "Hello, " & name & "!" lblHello.Caption = "Hello, " & name & "!" lblHello.Visible = True lblHello.Visible = True End Sub

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

19 After Pushing the Button:

20 Try it Yourself! You can download the example program and try using itYou 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 interfaceTry 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 labelAlso try adding a button that does something to the text in the large label

21 MAC VERSION

22 Adding Controls A blank user form isn’t much use!A blank user form isn’t much use! Let’s add a few controls.Let’s add a few controls. When using controls that our program refers to, we give them meaningful names.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 lblWe have a standard way to construct names. Form names start with frm; buttons with btn; text boxes with txt; labels with lbl

23 Watch the Video There’s a video that shows the construction of this user form applicationThere’s a video that shows the construction of this user form application The following slides show some of the major highlightsThe following slides show some of the major highlights

24 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.

25 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.

26 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.

27 The Code Option Explicit '****************************************************** ' Read name from text box and print on label '****************************************************** Private Sub btnClick_Click() Dim name As String Dim name As String name = txtName.Text name = txtName.Text lblHello.Caption = "Hello, " & name & "!" lblHello.Caption = "Hello, " & name & "!" lblHello.Visible = True ‘important so we see the message! lblHello.Visible = True ‘important so we see the message! End Sub

28 Try it Yourself! You can download the example program and try using itYou 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 interfaceTry 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 labelAlso try adding a button that does something to the text in the large label