Ch 11: Userforms CP212 Winter 2010. Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Working with Intrinsic Controls and ActiveX Controls
Information System Design Lab 5&6. User Interface Design.
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
Tutorial 8: Developing an Excel Application
XP New Perspectives on Microsoft Access 2002 Tutorial 61 Microsoft Access 2002 Tutorial 6 – Creating Custom Reports.
Excel and VBA Creating an Excel Application
CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
Using the Visual Basic Editor Visual Basic for Applications 1.
CA 121 Intro to Programming Tariq Aziz and Kevin Jones GUI Programming in Visual Studio.NET Chapter 3 Tariq Aziz and Kevin Jones.
Compunet Corporation Programming with Visual Basic.NET GUI Chapter 3 Week 13 Tariq Aziz and Kevin Jones.
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.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Controls General Discussion. VB Controls Visual Basic Controls A control is the generic name for any object placed on a form Controls may be images,
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Not in Text CP212 Winter No VBA Required “Regular” Programming traditional programming is sequential in nature o one command executed after another.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management.
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.
IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Dialog Boxes – Day 1 Built-in Dialog Boxes Input Box Message Box Custom Dialog Boxes – User Forms.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
More Form Tools Combo Box (displays a list) Check Box (yes/no) Frame (groups option buttons) Option Button (exclusive choice) Ref Edit (user selects cells)
1 Visual Basic Part I - A tool for customizing your program Principles of GIS
Lab 10. User Forms Design – Code Part ► Lab 9 Review ► Continue ‘Student Record’ Example ► A Car Loan Application.
Word Create a basic TOC. Course contents Overview: table of contents basics Lesson 1: About tables of contents Lesson 2: Format your table of contents.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Basic GUI VISUAL BASIC. BASIC GUI Slide 2 of 53 Topic & Structure of the lesson Introduction Data Validation Use controls for making choices Write Pull.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Introducing UserForms Joanna Wyrobek 1. Ms Excel UserForms A UserForm object is a window or dialog box that makes up part of an application's user interface.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Working with UserForms Dr Joanna Wyrobek 1. Displaying a modeless UserForm By default, UserForms are displayed modally. This means that the UserForm must.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
VBA Excel Macro 1.Create a Macro: To create a macro in Excel VBA, Create a Macro: To create a macro in Excel VBA,Create a Macro: To create a macro.
Excel Tutorial 8 Developing an Excel Application
Chapter Topics 15.1 Graphical User Interfaces
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
Excel VBA Day 3 of 3 Tom Vorves.
User Forms.
Variables and Arithmetic Operations
UserForm.
Custom dialog boxes Unit objectives
Chapter 15: GUI Applications & Event-Driven Programming
Visual C# - GUI and controls - 1
Presentation transcript:

Ch 11: Userforms CP212 Winter 2010

Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize o btnCancel_Click Displaying a User Form Looping Through Controls Working with Listboxes Working with Excel Controls

Background some information covered in the lecture on interface design textbook calls a Userform a “dialog box” o can be much more than that called simply a form in Visual Basic o related topic: Windows Forms ProgrammingWindows Forms Programming example in text o complex spreadsheet o form gives the user options to choose from o easier to use this “application” than learn Excel

Starting a Form creating a GUI based application or form starts with designing the form to insert a form in VBE o Insert -> Userform o Toolbox gets displayed - sometimes it disappears  click View -> Toolbox to get it back

Designing the Form Need to know 3 things: Which controls are available How to place, resize and line up controls in a form How to set the properties

Controls

Common Uses CommandButton: allows user to execute code (subs or macros) Label: explain something Textbox: get input from the user (can be read- only - set Enabled property to False)

Common Uses ListBox: choose one or more items from a list ComboBox: like a listbox, but allows a custom item to be added that isn't in the list. CheckBox: any, none, or all of the boxes can be selected OptionButton: also called a radio button, only one option can be selected (usually one is mandatory) per group Frame: organize a set of options, or grouping of controls RefEdit: similar to a textbox but also allows user to select a range of cells

Custom Controls Right-click on the toolbox and select Additional Controls One good one is the Calendar tool: o Calendar Control 12.0 (but there are others) The calendar control was removed in Office You can either buy a new one, or create your own. That would be a good exercise, and has been used as part of an assignment in the past.

Laying Controls To help, controls can be resized and aligned to each other or the form Format -> Align, Format -> Make Same Size o Experiment with it

Setting Properties most controls have different properties some are common Name: used to refer to the control with VBA Code o name not necessary if you plan on not referring to it with code (like labels) Caption: what you see on the screen, usually on forms, buttons and labels. Text or Value: What gets displayed in a Textbox. Most Properties can be set at design time (in the VBE) or with code

Naming controls use a 3 letter prefix good exercise on pg 213 of text

can greatly improve the usefulness of your form tab order begins as the order in which you placed them on the form you can change the TabIndex property TabIndex starts at 0 Set TabStop to False if you don't want the user tabbing to it. Items in a frame are indexed separately from other items on the form. View TabOrder Tab Order

Writing Event Handlers a GUI program (or a form) just sits there and waits for events a subroutine written to execute when the event happens is called an event handler events handlers are placed in the form's code window, not a module when viewing a form, click View -> Code or double-click a control double-clicking a control will open the event handler for that control's default event (like CommandButton1_Click) event handlers are named controlname_EventName try it and use the Object Browser to learn more o RefEdit control has its own library... hard to find

Displaying a Form give the form a name insert a new module and create a sub to display the form Public Sub Display() ' Shows the form I want to use frmMyForm.Show End Sub form will appear and then the event handlers take over

Example Code from text (pg.221) Shown here is a Window XP screenshot.

Practice Form.xlsm (pg. 223) code for adding items to a list o For Loop or RowSource property variables declared Public at the top of the code window, so they can be accessed from inside the form as well Useful functions: IsNumeric and IsDate to check for data validation can use Exit Sub if an error occurs but you still want form to be displayed

Practice Form cont'd use Select Case for option buttons o optTruck.Value = True if it has been selected Value property of a listbox: Indicates which item was selected. o ListIndex: position in the list of the selected item, base 0 o If ListIndex = -1 then no item selected o make bulletproof code: set ListIndex = 0 in UserForm_Initialize, forcing user to make a selection Must Unload the form to remove it from the screen When unloaded, the original calling sub will continue

Looping Through Controls have many controls on a form and you want to check all of them would be easy if they were in an array o can't be done in VBA so use For Each... in the collection of Controls on the form: Dim ctl As Control For Each ctl In Me.Controls If TypeName(ctl) = "TextBox" Then If ctl.Value = "" or Not IsDate(ctl) Then MsgBox "Enter valid dates", vbInformation, "Invalid Entry" ctl.SetFocus ' Allows user to enter data again Exit Sub End If Next

List Boxes two types: single and multi use MultiSelect property Single o ListIndex = -1 means Nothing selected o ListIndex = 3 means the 4th item was selected o lstMyList.Value will also hold the selected item Multi o can't use Value or ListIndex o Selected property is a 0-based array of True or False values o ListCount will count the items in the list For i = 0 to lstProducts.ListCount -1 isChosen(i) = lstProducts.Selected(i) Next

Private Sub btnOk_Click() ' Display the value the user has selected in the list txtOutput.Text = lstBox.Value txtIndexValue.Text = lstBox.ListIndex End Sub Private Sub UserForm_Initialize() ' Restore default height Me.Height = ' Add items to the list lstBox.AddItem "One" lstBox.AddItem "Two" lstBox.AddItem ("Four") lstBox.AddItem "Three", 2 lstExtended.AddItem ("One") lstExtended.AddItem ("Two") lstExtended.AddItem ("Four") lstExtended.AddItem "Three", 2 lstExtended.AddItem "Five" lstExtended.AddItem "Six" lstExtended.AddItem "Seven" ' Clear the values in the list box lstMulti.Clear ' Get values from the spreadsheet Dim c As Range For Each c In Worksheets("wksData").Range("defaultValues") ' Add each item to the list lstMulti.AddItem c.Value Next

Private Sub btnChooseEnhanced_Click() Dim mySelections As String Dim isChosen() As String ' Resize the array based on the size of items in the list ReDim isChosen(lstExtended.ListCount) ' Show the hidden portion of the form. Me.Height = ' Place the selected items in an array For i = 0 To lstExtended.ListCount - 1 ' Check to see if the item was selected If lstExtended.Selected(i) Then ' If it was selected, place it in the array isChosen(i) = lstExtended.List(i) ' This produces gaps in the output if items were not selected. End If Next ' Create a string of all the items joined together mySelections = Join(isChosen, ", ") txtMultiOutput.Text = mySelections End Sub

Wrap Up Check pg. 230 about working with Excel controls (little or no VBA) read the summary, work on exercises 1, 2, 4