You are on page 1of 33

VB.

NET
PROGRAMMING
Functions
Chapter 6:

Objective

 Explain the use of function


 Create program that uses function
Chapter 6:

Functions

 Functions are similar to normal procedures but the main purpose of the
functions is to accept certain inputs and pass them on the main program
to finish execution. There are two types of function, the built-in
functions and the user defined functions. We have encountered using the
built in functions such as the MsgBox function.
Chapter 6:
Functions

 InputBox()
An InputBox display a prompt in a dialog box, waits for the user to
input text or click a button, and then returns a string containing the
contents of the text box.
Chapter 6:
Functions
 Parts of InputBox Function
1. Prompt – Required String expression displayed as the message in the
dialog box.
2. Title – Optional. String expression displayed in the title bar of the
dialog box. If you omit Title, the application name is placed in the title
bar.
3. DefaultResponse – Optional. String expression displayed in the text
box as the default response if no other input is provided. If you omit
DefaultRespose, the displayed text box is empty.
Chapter 6:
Functions
 Parts of InputBox Function
4. XPos – Optional. Numeric expression that specifies, in pixels, the
distance of the left edge of the dialog box from the left edge of the
screen. If you omit XPos, the dialog box is centered on the screen.
5. YPos – Optional. Numeric expression that specifies, in pixels, the
distance of the upper edge of the dialog box is centered on the screen.
Chapter 6:
Sample Program Function
 Create a new Windows application and draw a label on it. Leave the
property text as Blank.
 Double click on the form and declare a variable such:
Dim yourname As String
 On the Form load procedure type the following:
Label1.Text = “Your name is “ + InputBox(“Input your name.”)
 Run your program and you will be propted to input name like:
Chapter 6:
Sample Program Function

Chapter 6:
Sample Program Function

Chapter 6:
Sample Program Function
Chapter 6:
Sample Program Function
Chapter 6:
ListBoxes
 A list box display a list of items from which the user can select one or
more items. If the number of items exceeds the number that can be
displayed, scroll bar is automatically added.
Chapter 6:
Working with ListBoxes
 To add Contents to the Listbox
1. To the Properties Window, choose item and you can directly supply the
contents of your Listbox.
2. To your program, using Listbox.Items.Add(“Items to be Added”)
Chapter 6:
Working with ListBoxes
Chapter 6:
Working with ListBoxes

 To Refer Items in the Listbox


Items in a Listbox are referred by index. When items are added to the
ListBox they are assigned an index. The first item in the ListBox always has
an index of 0 the next 1 and so on.
 To Display the Index of an Item
TextBox1.Text = ListBox1.SelectedIndex ‘This line of code will
display the index number of the selected item from the list in a Textbox.
Chapter 6:
Working with ListBoxes
 To Count the Number of Items in a Listbox
Textbox1.Text = ListBox1.Items.Count
 To Display the Item Selected from ListBox in a Textbox
Textbox1.Text = ListBox1.SelectedItem
 To Remove Items from a Listbox
ListBox1.Items.RemoveAt(4)
ListBox1.Items.Remove(ListBox1.SelectedIndex)
 To Remove All Items
ListBox1.Items.Clear()
Chapter 6:
Sample Program Using ListBox
Chapter 6:
Sample Program Using ListBox
Chapter 6:
ComboBox
 is similar to the list box. The differences are a combo box includes a text
box on top and only allows selection of one item. In some cases, the user
can type in an alternate response.
Chapter 6:
Working with ComboBox
 To add Contents to the ComboBox
1. To the Properties Window, choose Item and you can directly supply the
contents of your ComboBox.
2. To your program, using ComboBox.Items.Add(“Items to be Added”)
 To Access a Specific Item
Accessing specific items in a Windows Forms combox box, list box, or
checked list box is an essential task. It enables you to programmatically
determine what is in a list, at any given position. You can do this by using
the GetItemText.
Chapter 6:
Working with ComboBox

 To Refer Items in the ComboBox


Items in a ComboBox are referred by index. When items are added to
the ComboBox they are assigned an index. The first I tem in the ComboBox
always has an index of 0 the next 1 and so on.
 To Display the Index of an Item
TextBox1.Text = ComboBox1.SelectedIndex ‘This line of code will
display the index number of the selected item from the list in a Textbox.
Chapter 6:
Working with ComboBox
 To Count the Number of Items in a Listbox
Textbox1.Text = ComboBox1.Items.Count
 To Display the Item Selected from ListBox in a Textbox
Textbox1.Text = ComboBox1.SelectedItem
 To Remove Items from a Listbox
ComboBox1.Items.RemoveAt(4)
ComboBox1.Items.Remove(ListBox1.SelectedIndex)
 To Remove All Items
ComboBox1.Items.Clear()
Chapter 6:
Sample Program Using ComboBox
Chapter 6:
Sample Program Using ComboBox
Chapter 6:
Timer
 The timer control is a component that raises an event at regular intervals.
This component is designed for a Windows Forms environment. Unlike
other controls you can see during design and runtime, timer control is
one of the components of VB that you can only see during design time.
Chapter 6:
Sample Program For Timer
Chapter 6:
Sample Program For Timer
Chapter 6:
Sample Program For Timer
Chapter 6:
Picture Boxes
 The picture box allows you to place graphics information on a form. It is
best suited for dynamic environments – for example when doing
animation.
 Picture boxes lie in the top layer of the form display. They behave very
much like small form within a form, possessing most of the same
properties as a form.
Chapter 6:
Picture Boxes Properties
 Image – Establishes the graphics file to display in the picture box.
 SizeMode – Control how the PictureBox will handle image placement
and control sizing.
Chapter 6:
Picture Box
 Picture Box Events:
Click – Triggered when a picture box is clicked.
DblClick – Triggered when a picture box is double clicked.
 Picture Box Methods:
Dispose – Clears the picture box.
Chapter 6:
Sample Program For Picture Box

You might also like