Unit 2 Chapter 4 Windows Forms Copy 1
Unit 2 Chapter 4 Windows Forms Copy 1
Introduction
Form is a placeholder for all controls that can be placed from toolbox. In VB.NET, the support for
Windows forms is in the System.Windows.Forms namespace and the from class is
System.Windows.Forms.Form. The Visual Basic Integrated Development Environment (IDE) shows
several aspects of forms.
At the top of the form, there is a title bar, which displays the title of the form, by default it is Form1. At
the right in the title bar there is control box that includes maximize, minimize and close buttons. Under the
title bar, it has a menu bar. If the form contains any menu, it gets displayed in the menu bar. Under the
menu bar, forms can have tool bars. The main area of the form is called client area where controls such as
Text Boxes, Command Buttons Labels etc can be placed based on the requirement. The whole form is
surrounded by a border.
The important class for Windows forms is the Form class in System.Windows.Forms namespace each
form in the namespace is an instance of that class. Classes can have members—fields (data items)
methods (built-in procedures) and properties (data items accessed through an interface based on
methods).
There are two kinds of class members for Form class. They are
Static/Shared members (class members) which are accessed directly using the class as
classname. membername. No object is needed.
Instance members (object members) which are accessed by using an instance of a class as
objectname.membername.
The Form class contains only one class property namely ActiveForm, which holds currently active form
for the entire application. Public object properties of Windows forms are listed below:
Public object properties Windows forms p
Property Description
AcceptButton Gets or sets the button on the form that is pressed when the user uses Enter key.
ActiveControl Gets or sets the active control (the one with the focus)
ActiveMdiChild Gets the currently active multiple document interface (MDI) child window.
AllowDrop Indicates if the form can accept data that the user drags and drops into it.
AutoScale Indicates if the form adjusts its size to fit the height of the font used on the form and
scales its controls.
AutoScroll Indicates if the form implements auto scrolling.
BackColor Gets or sets the background color for the current form.
BackgroundImage Gets or sets the background image in the form.
Bottom Gets the location of the bottom of the form.
Bounds Gets or sets the bounding rectangle for the form.
CancelButton Indicates the button control that is pressed when the user presses the ESC key.
CanFocus Indicates if the form can receive focus.
Capture Specifies if the form has captured the mouse, or captures the mouse.
ClientRectangle Returns the rectangle that represents the client area.
ClientSize Gets or sets the size of the client area.
ContainsFocus Indicates if the form or child control has the input focus.
ContextMenu Gets or sets the shortcut menu for the current form.
ControlBox Gets or sets a value indicating if a control box is displayed.
Controls Gets or sets the collection of controls contained within the form.
Cursor Gets or sets the cursor that is displayed when the user moves the mouse pointer over
the current form.
DesktopBounds Gets or sets the size and location of the form on the Windows desktop.
DesktopLocation Gets or sets the location for the form.
DialogResult Gets or sets the dialog result for the form.
Enabled Gets or sets a value indicating if the form is enabled.
Focused Indicates if he form has input focus.
ForeColor Gets or sets the foreground color of the form.
FormBorderStyle Gets or sets the border style fo the form.
Height Gets or sets the height of the form.
Icon Gets or sets the icon for the form,.
IsMdiChild Indicates if the form is an MDI child form.
IsMdiContainer Gets or sets a value indicating if the form is a container for MDI.
Left Gets or sets the x-coordinate for the left edge of the form in pixels.
Location Gets or sets the coordinates of the upper-left corner of the form relative to the
upper-left corner of its container.
MaximizeBox Gets or sets a value indicating if the maximize button is displayed in the caption bar
of the form.
MaximumSize Returns the maximum size the form can be resized to.
MdiChildren Returns an array of forms of the MDI child forms that are parented to this form.
MdiParent Gets or sets the current MDI parent form for the current form.
Menu Gets or sets the Main Menu that is displayed in the form.
MinimizeBox Gets or sets a value indicating if the minimize button is displayed in the caption bar
of the form.
MinimumSize Gets the minimum size the form can be resized to.
Modal Gets a value indicating if the current form is displayed modally.
Name Gets or sets the name of the form.
OwnedForms Gets an array of Form objects of all forms that are owned by current form.
Owner Gets or sets the form that owns the current form.
Parent Gets or sets the parent container of this form.
ParentForm Gets or sets the form that the container form is assigned to.
Right Gets the distance between the right edge of the form and the left edge of the
container.
ShowInTaskbar Gets or sets a value indicating if the form is displayed in the Windows taskbar.
Size Gets or sets the size for the form.
StartPosition Gets or sets the starting position of the form at run time.
TabStop Gets or sets a value indicating if the user can give the focus to the form using Tab
key
Tag Gets or sets the object that contains data about the form.
Text Gets or sets the text associated with the current form.
Top Gets or sets the top coordinate of the form.
TopLevel Gets or sets a value indicating if the form should be displayed as top-level window.
TopMost Gets or sets a value indicating if the form should be displayed as the topmost form
of the application.
Visible Gets or sets a value indicating if the form is visible.
Width Gets or sets the width of the form.
WindowState Gets or sets window state of the form.
Windows forms also support events. Events allow the user something has happened to the control. For
example: when the user clicks a form, a Click event occurs.
Event Description
Activated Occurs when the form is activated in the code or by the user.
Click Occurs when the form is clicked.
Closed Occurs when the form is closed.
Closing Occurs when the form is closing.
ControlAdded Occurs when a control is added.
ControlRemoved Occurs when a control is removed.
CursorChanged Occurs when the cursor property value has changed.
Deactivate Occurs when the form loses focus and it is not active form.
DoubleClick Occurs when the form is double-clicked.
DragDrop Occurs when a drag-and-drop operation is completed.
DragEnter Occurs when an object is dragged into the bounds of the form.
DragLeave Occurs when an object has been dragged into and out of the bounds of the form.
DragOver Occurs when an object has been dragged over the bounds of the form.
Enter Occurs when a form is entered.
ForeCplorChanged Occurs when the ForeColor property value has been changed.
GotFocus Occurs when the form receives focus.
KeyDown Occurs when a key is pressed down while the form has focus.
KeyPress Occurs when a key is pressed while the form has focus.
KeyUp Occurs when a key is released while the form has focus.
Layout Occurs when a form has to lay out its child controls.
Load Occurs before a form is displayed for the first time.
LocationChanged Occurs when the Location property value has changed.
LostFocus Occurs when the form loses focus.
MdiChildActivate Occurs when an MDI child form is activated or closed within an MDI application.
MouseDown Occurs when the mouse pointer is over the form and a mouse button is pressed.
MouseEnter Occurs when the mouse pointer enters the form.
MouseHover Occurs when the mouse pointer hovers over the form.
MouseLeave Occurs when the mouse pointer leaves the form.
MouseMove Occurs when the mouse pointer is moved over the form.
MouseUp Occurs when the mouse pointer is moved over the form and a mouse button is
released.
MouseWheel Occurs when the mouse wheel moves while the form has focus.
Move Occurs when the form is moved.
Paint Occurs when the form is redrawn.
Resize Occurs when the form is resized.
SizeChanged Occurs when the Size property value has changed.
TextChanged Occurs when the Text property value has changed.
MDI Forms
There are three types of forms in Visual Basic namely standard form, MDI form and MDI child form.
Multiple Document Interface (MDI) form closely resembles standard form with a major difference—the
client area of an MDI form acts as an enclosure for other forms. MDI form is also called as a parent form
that can contain many MDI children. The third type of form in Visual Basic is MDI child form. These
types of forms appear in MDI child window.
This will change the size and location of both form and a button in the form when Button1 is clicked.
SetBounds can also be used to do the same thing:
Overloads Public Sub SetBounds(ByVal x As Integer, ByVal y As Integer, _
ByVal width As Integer, ByVal height As Integer, _
ByVal specified As BoundsSpecified)
x—The new Left property value of the control
y—The new Right property value of the control
width—The new Width property value of the control
height—The new Height property value of the control
Overloads keyword indicates that SetBounds is overloaded, which means, there is more than one form of
this method. BoundsSpecified is an enumeration which can have one of the following values:
All—Specifies that both Location and Size property values are indicated.
Height—Specifies that the height of the control is indicated.
Location—Specifies that both x and y coordinates of the control are indicated.
None—Specifies that no bounds are indicated.
Size—Specifies both Width and Height property values of the control are indicated.
Width—Specifies that the width of the control is indicated.
X—Specifies that the left edge of the control is indicated.
Y—Specifies that the right edge of the control is indicated.
Example:
Private Sub Button1_Click(ByVal Sender As System.ObjecT, _
ByVal e As System.EventArgs) Handles Button1.Click
SetBounds(0,0,100,100)
Button1.SetBounds(0,0,100,50)
End Sub
MsgBox Constants
Constant Value Description
OKOnly 0 Shows OK button only
OKCancel 1 Shows OK and Cancel buttons.
AbortRetryIgnore 2 Shows Abort, Retry and Ignore buttons.
YesNoCancel 3 Shows Yes, No and Cancel buttons.
YesNo 4 Shows Yes and No buttons.
RetryCancel 5 Shows Retry and Cancel buttons.
Critical 16 Shows Critical Message icon.
Question 32 Shows Warning Query icon.
Exclamation 48 Shows Warning Message icon.
Information 64 Shows Information Message icon.
DefaultButton1 0 First button is default.
DefaultButton2 256 Second button is default.
DefaultButton3 512 Third button is default.
ApplicationModal 0 User must respond to the message box before continuing work in the
current application.
SystemModal 4096 All applications are unavailable until the user dismisses the message
box.
MsgBoxForeground 65536 Specifies the message box window as the foreground window.
MsgBoxRight 524288 Text will be right-aligned.
MsgBoxRTLReading 1048576 Specifies text to appear as right-to-left on RTL systems such as Hebrew
and Arabic
This function returns a value from the MsgBoxResult enumeration. The possible values are:
OK, Cancel, Abort, Retry, Ignore, Yes or No
Private Sub Button1_Click(ByVal Sender As System.ObjecT, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim Result As Integer
Result=MsgBox(This is Message Box”, MsgBoxStyle.OKCancel+_
MsgBoxStyle.Information+ MsgBoxStyle.SystemModal, “Message Box”)
If (Result = MsgBoxResult.OK) Then
TextBox1.Text =”You clicked OK”
End If
End Sub
This example will display a message box with message “This is Message Box” that contains two buttons
OK and Cancel. When OK button is pressed, TextBox1 will contain the text “You clicked OK”
The result of the Show method is a value from the DialogResult enumeration, showing the button clicked
by the user.
Abort—Returns Abort. None—Returns nothing.
Cancel—Returns Cancel. OK—Returns OK.
Ignore—Returns Ignore. Retry—Returns Retry.
No—Returns No. Yes—Returns Yes
Example:
Private Sub Button1_Click(ByVal Sender As System.Object, _
ByVal e As Event.Args) Handles Butt1.Click
Dim Result As Integer
Result=MessageBox.Show(“This is a message Box”, “Message Box”, _
MessageBpxButtons.OKCancel, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
If(Result=DialogResult.OK) Then
TextBox1.Text=”You Clicked OK”
End If
End Sub
When user enters text and click OK button on the InputBox, the InoutBox function resturns that text and
displays it in Text1.