Unit Iv
Unit Iv
For example,
Form1.Caption = "Hello"
You can set any of the form properties using Properties Window. Most of
the properties can be set or read during application execution. You can refer to
Microsoft documentation for a complete list of properties associated with different
controls and restrictions applied to them.
Control Methods
Methods cause an object to do something A method is a procedure created as
a member of a class and they cause an object to do something. Methods are used to
access or manipulate the characteristics of an object or a variable. There are mainly
two categories of methods you will use in your classes:
If you are using a control such as one of those provided by the Toolbox, you
can call any of its public methods. The requirements of such a method
depend on the class being used.
If none of the existing methods can perform your desired task, you can add a
method to a class.
For example, the MessageBox control has a method named Show, which is called
in the code snippet below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles Button1.Click
MessageBox.Show("Hello, World")
End Sub
End Class
Control Events
Events are what happens when an object does something. An event is a
signal that informs an application that something important has occurred. For
example, when a user clicks a control on a form, the form can raise a Click event
and call a procedure that handles the event. There are various types of events
associated with a Form like click, double click, close, load, resize, etc.
Following is the default structure of a form Load event handler subroutine. You
can see this code by double clicking the code which will give you a complete list
of the all events associated with Form control:
If you click the icon on the top left corner, it opens the control menu, which
contains the various commands to control the form like to move control from one
place to another place, to maximize or minimize the form or to close the form
Form Properties
Form has some impotent properties as fallows
1. AcceptButton:-The button that's automatically activated when you press Enter,
No matter which control has the focus at the time. Usually the
OK button on a form is set as Accept Button for a form.
2. CancelButton: - The button that's automatically activated when you hit the Esc
key. Usually, the Cancel button on a form is set as
CancelButton for a form.
3. BackColor:- Sets the form background color.
4 . Enabled:- If True, allows the form to respond to mouse and keyboard
events; if False, disables form.
5 .MinimizeBox:- By default, this property is True and you can set it to False
to hide the Minimize button on the title bar.
6 .MaximizeBox:- By default, this property is True and you can set it to False
to hide the Maximize button on the title bar.
7 .Name:- This is the actual name of the form.
8 .Text :- Text, which will appear at the title bar of the form.
Form Methods
The following are some of the commonly used methods of the Form class. :
1 Activate :- Activates the form and gives it focus.
2 ActivateMdiChild :- Activates the MDI child of a form.
3 Close :- Closes the form.
4 Contains :- Retrieves a value indicating whether the specified
control is a child of the control.
5 Focus :- Sets input focus to the control.
6 Show : - Displays the control to the user.
7 ShowDialog :- Shows the form as a modal dialog box.
Form Events
Events associated with forms control:
1 Click :- Occurs when the form is clicked.
2 Closed: - Occurs before the form is closed.
3 Closing:- Occurs when the form is closing.
4 DoubleClick: - Occurs when the form control is double-clicked.
VB.Net By Prof.A.D.Chavan Page 5
Unit-IV
TextBox :
Text box controls allow entering text on a form at runtime. By default, it
takes a single line of text, however, you can make it accept multiple texts and
even add scroll bars to it.
Let's create a text box by dragging a Text Box control from the Toolbox and
dropping it on the form.
Example
The following source code shows how to set some properties of the Label through
coding.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.Text = "This is my first Label"
Label1.BorderStyle = BorderStyle.FixedSingle
Label1.TextAlign = ContentAlignment.MiddleCenter
End Sub
End Class
VB.Net Button Control
The Button control represents a standard Windows button. It is generally
used to generate a Click event by providing a handler for the Click event.
Let's create a label by dragging a Button control from the Toolbox ad
dropping it on the form.
8.TabIndex:- Gets or sets the tab order of the control within its container.
9.Text:- Gets or sets the text associated with this control.
Methods of the Button Control
The following are some of the commonly used methods of the Button control:
1. GetPreferredSize:- Retrieves the size of a rectangular area into which a control
can be fitted.
2. NotifyDefault:- Notifies the Button whether it is the default button so that it
can adjust its appearance accordingly.
3. Select :- Activates the control.
4. ToString:- Returns a String containing the name of the Component,
if any. This method should not be overridden.
Events of the Button Control
The following are some of the commonly used events of the Button control:
1.Click:- Occurs when the control is clicked.
2.DoubleClick:- Occurs when the user double-clicks the Button control.
3.GotFocus:- Occurs when the control receives focus.
4.TabIndexChanged:-Occurs when the TabIndex property value changes.
5.TextChanged:- Occurs when the Text property value changes.
6.Validated:- Occurs when the control is finished validating.
The following vb.net source code shows how to change the button Text
property while Form loading event and to display a message box when pressing a
Button Control.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Text = "Click Here"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("WelCome")
End Sub
End Class
VB.Net ListBox controls.
ListBox: It represents a Windows control to display a list of items.
You can populate the list box items either from the properties window or at
runtime. To add items to a ListBox, select the ListBox control and get to the
properties window, for the properties of this control. Click the ellipses (...) button
next to the Items property. This opens the String Collection Editor dialog box,
where you can enter the values one at a line.
Properties of the ListBox Control
The following are some of the commonly used properties of the ListBox control:
1.AllowSelection:- Gets a value indicating whether the ListBox currently
enables selection of list items.
2.BorderStyle:- Gets or sets the type of border drawn around the list box.
3.ColumnWidth:- Gets of sets the width of columns in a multicolumn list box.
4.HorizontalExtent:-Gets or sets the horizontal scrolling area of a list box.
5.HorizontalScrollBar:- Gets or sets the value indicating whether a horizontal
scrollbar is displayed in the list box.
6.ItemHeight:- Gets or sets the height of an item in the list box.
7.Items:- Gets the items of the list box.
8.MultiColumn:- Gets or sets a value indicating whether the list box
supports multiple columns.
9.ScrollAlwaysVisible:- Gets or sets a value indicating whether the vertical scroll
ComboBox Control:-
The ComboBox control is used to display a drop-down list of various items.
It is a combination of a text box in which the user enters an item and a drop-down
list from which the user selects an item.
Let's create a combo box by dragging a ComboBox control from the
Toolbox and dropping it on the form.
You can populate the list box items either from the properties window or at
runtime. To add items to a ListBox, select the ListBox control and go to the
properties window for the properties of this control. Click the ellipses (...) button
next to the Items property. This opens the String Collection Editor dialog box,
where you can enter the values one at a line.
Properties of the ComboBox Control
The following are some of the commonly used properties of the ComboBox
control:
1.AllowSelection:- Gets a value indicating whether the list enables selection of list
items.
2.AutoCompleteCustomSource:-Gets or sets a custom System.Collections.
Specialized.StringCollection to use when the
AutoCompleteSourceproperty is set to
CustomSource.
3.AutoCompleteMode:- Gets or sets an option that controls how automatic
completion works for the ComboBox.
4.AutoCompleteSource:-Gets or sets a value specifying the source of complete
strings used for automatic completion.
5.DataBindings:- Gets the data bindings for the control.
6.DataManager:- Gets the CurrencyManager associated with this control.
7.DataSource:- Gets or sets the data source for this ComboBox.
8.DropDownHeight:- Gets or sets the height in pixels of the drop-down portion
of the ComboBox.
VB.Net By Prof.A.D.Chavan Page 16
Unit-IV
9.DropDownStyle:- Gets or sets a value specifying the style of the combo box.
10.DropDownWidth:- Gets or sets the width of the of the drop-down portion of a
combo box.
11.DroppedDown:- Gets or sets a value indicating whether the combo box is
displaying its drop-down portion.
12.FlatStyle:- Gets or sets the appearance of the ComboBox.
13.ItemHeight:- Gets or sets the height of an item in the combo box.
14.Items:- Gets an object representing the collection of the items
contained in this ComboBox.
15.MaxDropDownItems:- Gets or sets the maximum number of items to be
displayed in the drop-down part of the combo box.
16.MaxLength:- Gets or sets the maximum number of characters a user can
enter in the editable area of the combo box.
17.SelectedIndex:- Gets or sets the index specifying the currently selected item.
18.SelectedItem:- Gets or sets currently selected item in the ComboBox.
19.SelectedText:- Gets or sets the text that is selected in the editable portion of a
ComboBox.
20.SelectedValue:-Gets or sets the value of the member property specified by the
ValueMember property.
21.SelectionLength:-Gets or sets the number of characters selected in the editable
portion of the combo box.
22.SelectionStart:- Gets or sets the starting index of text selected in the combo
box.
23.Sorted:- Gets or sets a value indicating whether the items in the combo
box are sorted.
24.Text:- Gets or sets the text associated with this control.
Methods of the ComboBox Control
The following are some of the commonly used methods of the ComboBox control:
1.BeginUpdate:-Prevents the control from drawing until the EndUpdate method is
called, while items are added to the combo box one at a time.
2.EndUpdate:-Resumes drawing of a combo box, after it was turned off by the
BeginUpdate method.
3.FindString:-Finds the first item in the combo box that starts with the string
specified as an argument.
4.FindStringExact:-Finds the first item in the combo box that exactly matches the
specified string.
VB.Net By Prof.A.D.Chavan Page 17
Unit-IV
5.SelectAll:-Selects all the text in the editable area of the combo box.
Events of the ComboBox Control
The following are some of the commonly used events of the ComboBox control:
1.DropDown:-Occurs when the drop-down portion of a combo box is displayed.
2.DropDownClosed:-Occurs when the drop-down portion of a combo box is no
longer visible.
3.DropDownStyleChanged:-Occurs when the DropDownStyle property of the
ComboBox has changed.
4.SelectedIndexChanged:-Occurs when the SelectedIndex property of a
ComboBox control has changed.
5.SelectionChangeCommitted:-Occurs when the selected item has changed and
the change appears in the combo box
The user can type a value in the text field or click the button to display a drop
down list. In addition to display and selection functionality, the ComboBox also
provides features that enable you to efficiently add items to the ComboBox.
Add item to combobox
ComboBox1.Items.Add("Sunday")
ComboBox1.Items.Add("Monday")
ComboBox1.Items.Add("Tuesday")
How to set the selected item in a comboBox
You can set which item should shown while it displaying in the form for first time.
ComboBox Example
The following VB.Net source code add seven days in a week to a combo box while
load event of a Windows Form and display the fourth item in the combobox.
Public Class Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Sunday")
ComboBox1.Items.Add("Monday")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("wednesday")
ComboBox1.Items.Add("Thursday")
ComboBox1.Items.Add("Friday")
ComboBox1.Items.Add("Saturday")
ComboBox1.SelectedItem = ComboBox1.Items(3)
End Sub
VB.Net By Prof.A.D.Chavan Page 18
Unit-IV
8.TabStop:-Specifies whether the user will be able to focus on the picture box by
using the TAB key.
9.Text:-Gets or sets the text for the picture box.
10.WaitOnLoad:-Specifies whether or not an image is loaded synchronously
Methods of the PictureBox Control
The following are some of the commonly used methods of the PictureBox control:
1.CancelAsync:-Cancels an asynchronous image load.
2.Load:-Displays an image in the picture box
3.LoadAsync:-Loads image asynchronously.
4.ToString:-Returns the string that represents the current picture box.
Events of the PictureBox Control
The following are some of the commonly used events of the PictureBox control:
1.CausesValidationChanged:-Overrides the Control.CausesValidationChanged
property.
2.Click:-Occurs when the control is clicked.
3.Enter:-Overrides the Control.Enter property.
4.FontChanged:-Occurs when the value of the Font property changes.
5.ForeColorChanged:-Occurs when the value of the ForeColor property changes.
6.KeyDown:-Occurs when a key is pressed when the control has focus.
7.KeyPress:-Occurs when a key is pressed when the control has focus.
8.KeyUp:-Occurs when a key is released when the control has focus.
9.Leave:-Occurs when input focus leaves the PictureBox.
10.LoadCompleted:-Occurs when the asynchronous image-load operation is
completed, been canceled, or raised an exception.
11.LoadProgressChanged:-Occurs when the progress of an asynchronous image-
loading operation has changed.
12.Resize:-Occurs when the control is resized.
13.RightToLeftChanged:-Occurs when the value of the RightToLeft property
changes.
14.SizeChanged:-Occurs when the Size property value changes.
15.SizeModeChanged:-Occurs when SizeMode changes.
16.TabIndexChanged:-Occurs when the value of the TabIndex property changes.
17.TabStopChanged:-Occurs when the value of the TabStop property changes.
18.TextChanged:-Occurs when the value of the Text property changes.
Image Controls
As you can guess from its name, you use Image controls to display images in
Web applications. You set the URL of the image with the ImageUrl property. The
alignment of the image in relation to other elements on the Web page is specified
by setting ImageAlign property You can specify the text to display in place of an
image if the image is not available by setting the AlternateText property.
The Checked property of the radio button is used to set the state of a radio
button. You can display text, image or both on radio button control. You can also
change the appearance of the radio button control by using the Appearance
property.
Properties of the RadioButton Control
The following are some of the commonly used properties of the RadioButton
control:
1.Appearance:-Gets or sets a value determining the appearance of the radio
button.
2.AutoCheck:-Gets or sets a value indicating whether the Checked value and the
appearance of the control automatically change when the control is clicked.
3.CheckAlign:-Gets or sets the location of the check box portion of the radio
button.
4.Checked:-Gets or sets a value indicating whether the control is checked.
5.Text:-Gets or sets the caption for a radio button.
6.TabStop:- Gets or sets a value indicating whether a user can give focus to the
RadioButton control using the TAB key.
Methods of the RadioButton Control
The following are some of the commonly used methods of the RadioButton
control:
1 PerformClick:- Generates a Click event for the control, simulating a click by a
user.
Events of the RadioButton Control
The following are some of the commonly used events of the RadioButton control:
1 AppearanceChanged:-Occurs when the value of the Appearance property of the
RadioButton control is changed.
2 CheckedChanged:-Occurs when the value of the Checked property of the
RadioButton control is changed.
A radio button or option button is a type of graphical user interface element
that allows the user to choose only one of a predefined set of options. When a user
clicks on a radio button, it becomes checked, and all other radio buttons with same
group become unchecked.
The radio button and the check box are used for different functions. Use a
radio button when you want the user to choose only one option. When you want
the user to choose all appropriate options, use a check box. Like check boxes, radio
buttons support a Checked property that indicates whether the radio button is
selected.
Public Class Form1
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
Label1.ForeColor = Color.Red
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Label1.ForeColor = Color.Green
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Label1.ForeColor = Color.Blue
End Sub
End Class
Design-time
To create a Panel control at design-time, you simply drag and drop a Panel
control from Toolbox to a Form in Visual Studio. After you drag and drop a Panel
on a Form, the Panel looks like Figure. Once a Panel is on the Form, you can move
it around and resize it using mouse and set its properties and events.
Run-time
Creating a Panel control at run-time is merely a work of creating an instance of
Panel class, set its properties and adds Panel class to the Form controls.
First step to create a dynamic Panel is to create an instance of Panel class. The
following code snippet creates a Panel control object.
Dim dynamicPanel As New Panel()
In the next step, you may set properties of a Panel control. The following code
snippet sets location, size and Name properties of a Panel.
dynamicPanel.Location = New System.Drawing.Point(26, 12)
dynamicPanel.Name = "Panel1"
dynamicPanel.Size = New System.Drawing.Size(228, 200)
dynamicPanel.BackColor = Color.LightBlue
Once the Panel control is ready with its properties, the next step is to add the
Panel to a Form. To do so, we use Form.Controls.Add method that adds Panel
control to the Form controls and displays on the Form based on the location and
size of the control. The following code snippet adds a Panel control to the current
Form.
Controls.Add(dynamicPanel)
Setting Panel Properties
After you place a Panel control on a Form, the next step is to set its properties. The
easiest way to set properties is from the Properties Window. You can open
Panel has most of the common control properties. Here I am going to discuss
the main purpose of a Panel.
Adding Controls to a Panel
You can add controls to a Panel by dragging and dropping control to the
Panel. We can add controls to a Panel at run-time by using its Add method. The
following code snippet creates a Panel, creates a TextBox and a CheckBox and
adds these two controls to a Panel.
Private Sub CreateButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CreateButton.Click
Dim dynamicPanel As New Panel()
dynamicPanel.Location = New System.Drawing.Point(26, 12)
dynamicPanel.Name = "Panel1"
dynamicPanel.Size = New System.Drawing.Size(228, 200)
dynamicPanel.BackColor = Color.LightBlue
Dim textBox1 As New TextBox()
textBox1.Location = New Point(10, 10)
textBox1.Text = "I am a TextBox5"
textBox1.Size = New Size(200, 30)
Dim checkBox1 As New CheckBox()
checkBox1.Location = New Point(10, 50)
checkBox1.Text = "Check Me"
checkBox1.Size = New Size(200, 30)
dynamicPanel.Controls.Add(textBox1)
dynamicPanel.Controls.Add(checkBox1)
Controls.Add(dynamicPanel)
End Sub
The output looks like Figure .
Windows Forms contain a rich set of classes for creating your own custom
menus with modern appearance, look and feel. The MenuStrip,
Retry:-The dialog box return value is Retry (usually from a button labeled
Retry).
Yes:-The dialog box return value is Yes (usually from a button labeled Yes).
I'll take a closer look at these dialogs now.
Open File Dialogs
As you'd expect from its name, the Open File dialog lets the user select a file to
open. In fact, it's the same Open File dialog used by Windows itself. You can see
this dialog box in Figure 1, as displayed in the OpenFileDialog .
You can use the ShowDialog method to display the dialog box at run
time. You can use the FileName property to get the file the user selected, open a
file in read-write mode using the OpenFile method, and so on.
You also can set the handy CheckFileExists and CheckPathExists
properties to True to check if a specified file or path already exists, and if it should
be created otherwise.
Font Dialogs
Font dialogs let the user select a font size, face, color, and so on. You can see
a font dialog box at work in Figure 3 from the FontDialog .