Visual Basic Controls and Jargon Explained
Learn how to create interactive forms and understand essential VB jargon like controls, methods, and event procedures. Practice exercises included.
Visual Basic Controls and Jargon Explained
E N D
Presentation Transcript
Hello World Program • Make a TextBox to display “Hello, world!” when OK button is clicked.
Hello World Program(cont.) • Enter the following code for command1_click() • Text1.Text = “Hello, world!”
Exercise • Make a ‘clear’ button to clean the TextBox.
Jargon • Controls • Tools in the toolbox to make objects on the forms of VB • Objects • User Interface elements on the VB forms • Have inherent functionality like OOP • Attributes • Describes the property of each object • Way to set attribute value: object.property = value
Jargon(cont.) • Event procedures • Procedures that are executed when some events related to the object occur ex) clicking of ‘end’ button stopping the program • Methods • Specific statements for a specific object • Format: object.method input_value ex) List1.Additem “Check”
Check box • Make a textbox with text1.text = “Division of Internet Engineering” • Make 2 checkBoxes, check1, check2 • Add the following code for each event procedure. Private Sub check1() If check1.value = vbChecked Then Text1.font.bold = True else Text1.font.bold = False End If End sub Private Sub check2() If check2.value = vbChecked Then Text1.font.Italic = True else Text1.font.Italic = False End If End sub
Option Buttons • Can be used to select one among several choices • Index attribute • If an option button is selected, index has appropriate value • You can group them using Frames • Exercise • Make 3 option buttons and 1 textBox. • Use copy and paste function to make the 3 buttons • Enter the following code. • Private Sub Option1_Click(Index As Integer) • If Index = 0 Then • Text1.Text = “Sonata Chosen” • elseif Index = 1 Then • Text1.Text = “Matiz Chosen” • else • Text1.Text = “Tico Chosen” • End If • End Sub
Exercise • Make a form that displays 2 questions asking • What is your hobby? ○ Reading ○ Dancing ○ Music listening • What is your favorite car? ○ Sonata ○ Tico ○ Avante • (hint) Make two frames having 3 option boxes each
ListBox • You can show a list of items by using a list box • Important attributes • ListIndex: the index of user selected item • List: the data array for the list box • Important method • AddItem: adds a new item in the list box
Exercise • Make a form as in p 74 • Use • Label (BorderStyle:1, Caption:List Index) • Listbox • commandButton • Hint: • Use ‘list’ attribute for the list values • Use ‘ctrl+enter’ for continuous entering • Use just ‘enter’ to finish entering • Add the following event procedures Private Sub List1_Click() Dim n, Name n = List1.ListIndex name = List1.List(n) Label1.Caption = “List Index:” & n & “ major ” & name End Sub Private Sub Command1_Click() Unload me End Sub
addItem in the ListBox • AddItem method can be used to enter initial values of the listbox • Format: list.AddItem “item” • Exercise • Make the same form as in the previous slide. • Use AddItem method to enter new values. Private Sub Form_load() List1.Additem “Computer & Internet Eng.” List1.Additem “Multimedia Eng.” List1.Additem “Information Network Eng.” List1.Additem “Electrical Eng.” List1.Additem “Industrial Eng.” End Sub
Exercise • Make 1 textbox, 1 listbox, and 3 buttons ‘add’, ‘delete’, ‘end’ • If ‘add’ button is pressed, enlist the text in the textbox into the listbox • If ‘delete’ button is pressed, drop the item form the listbox • Hint: use the following attributes and methods for ‘add’ and ‘delete’ respectively. • AddItem Text1.Text • List1.RemoveItem (List1.ListIndex)
Combo Box • Similar to ListBox but only one item can be seen • Initial items can be given using the list attribute • Enter ‘Kim, Tom, Judy, Bob’ as value of attribute ‘List’ • Enter ‘Choose One’ as value of attribute ‘Text’ • You can insert an item in the combo box • After inserting an item, press ‘enter’ key • Exercise • Make a form like the above • Enter the following code. Private Sub Combo1_Click() Label1.Caption = Combo1.Text & " has been chosen." End Sub • Private Sub Command1_Click() • End • End Sub
Exercise • Make a form that has one label, and two combo boxes, and one ‘end’ button • For the first combo box, enter the list as choices • Tom, Bob, Jim, Tony, Brown • For the second combo box, enter the list as choices • Mary, Nancy, Kim, Betty, Lee • If male or female is selected, display one of the following message in the label depending on gender • “Mr.XX is selected!” where XX is one in the list • “Ms.XX is selected!” where XX is one in the list