170 likes | 177 Views
CS 102. Graphical/Event-Driven Programs. Overview. Event loop/architecture Form_Load method Basic controls Example simple program Input boxes List boxes. Event-driven Programs. Created by Visual Studio Project should be of type: Windows Form Application
E N D
CS 102 Graphical/Event-Driven Programs
Overview • Event loop/architecture • Form_Load method • Basic controls • Example simple program • Input boxes • List boxes
Event-driven Programs • Created by Visual Studio • Project should be of type: Windows Form Application • VS creates several key files for you: • <project>.sln – Solution file. Contains names and types of all files in the project • <project> - Directory containing all files in the project • <project>\Form1.Designer.vb – GUI form details • <project>\Form1.vb – Code for the form • Other files you don’t need to worry about • Don’t change these files manually. Do it all through VS.
Event-driven Programs • Everything you need for a working program is already created • You can just execute the program • Won’t do anything, but the form will display! • Just add controls as needed, and event handlers as desired
Form Load • Critical event procedure • Exists for EVERY form in your program • It is 100% optional – you do not have to use it • Any code in the form is executed when the form is first loaded and drawn on the screen • To edit the method, just double click anywhere in the background of the form • If you decide you don’t want it, just delete the code for the method
Form Load • The following is the code module that results after you double click on the empty form for a new project, and add one line of code to it Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MsgBox("Form load is being executed!") End Sub End Class • First and last lines are standard for all forms • Form load header is standard and created by VS
Form Load • Form Load methods are very common, and are used for tasks such as: • Initializing data in fields on the form • List boxes • Text boxes • Others • Setting the focus (field that is currently active) • Reading data from a database for use on the form • Anything else you want initially performed on the form
Controls • Visual Studio provides an incredible number of out-of-the-box controls for your use • Just drop them on the form, and write code for their events • You can obtain a huge number of controls written by others for virtually any task, for example: • Twitter controls • Controls for writing OnStar applications • It is pretty easy to create your own new controls, and to add custom events to them
OOB Controls • The following is a brief list of controls available to you in VB: • Label – A static text label on a form • Button – A push button • Text box – A box for users to type/enter data • Radio button – A set of radio buttons to select one choice (of many) • Check box – A box that can be checked, or not • Combo box – A dropdown list box that you can type data into • Picture box – A control that is used to display an image
OOB Controls • List box – A control with multiple entries. You can select one, or multiple items • Date/time picker – A control that allows you to pick date and/or time • Link label – A static text label that is also a hyperlink • Progress bar – A bar to show completion of a process • MonthCalendar – A control that displays a calendar
OOB Controls • Timer – A control that calls an event every so often, based on the property in the timer • Web browser – Handles all facets of a web browser • Data grid controls – Interacts, displays, enters, modify database data • Horizontal/vertical scroll bars – Allows you to select from a range • Numeric Up/Down – Allows you to set a number by clicking up/down
OOB Controls • Masked Text Box – A text box that the user can supply a mask, to enforce data entry rules • Menu strip – Add menus to your application • Rich Text Box – A multi-line text box that allows Rich Text Format (RTF) to be entered • Open/Save File dialog controls • Print File dialogs
Input Boxes • Method for user to enter data • You can specify: • Dialog title • Prompt • Default value • You can create a validation routine for the data when it is entered • Button click method • You can mask the data as entered for security • Ex: Password field
List Box Control • Control to list a series of items • Can select among them • Can select multiple items from the list • Can have scroll bars on the listbox • Can add items to the list • Can remove items from the list • Can retrieve selected items • Can have multi-column list boxes • Can have checked list boxes
End Method • Often, when you are writing event methods you will want to stop the entire VB program • Use ‘End’ method • Private Sub Button1_Click(ByVal sender As System.Object, _ • ByVal e As System.EventArgs) Handles Button1.Click • End • End Sub
Properties • Every control that is created has pre-defined properties • Properties allow you to easily/quickly change many attributes of the control • Properties are changed in the properties window (by default, bottom right of page) • Users can create new properties for controls (you will not do this for this course) • Many properties are common among many control types – some are specific for that control
Properties • Some common properties are: • Name – The unique name of the control. VB auto-names them for you. You should ALWAYS change the name of the control • Backcolor – Background color • Forecolor – Foreground color • Enabled – Is the control active (can you use it) • Visible – Is the control visible, or not seen • Location – Where is the control on the form • This is almost always set by drag/drop • Tab Index – Which control number is this (tab order) • Text – What text is displayed in the control when the form is displayed