|
Working with Forms
Well, let's now start working with Forms. When you open a new form you can have
a look at the default properties of the form by selecting View->Properties
Window or by pressing F4 on the keyboard. The properties
window opens with default properties set to form by the software.
Briefly on Properties (Categorized):
Appearance
Appearance section of the properties window allow us to make changes to the appearance
of the form. With the help of appearance properties we can have a background color,
background image for the entire form, set the border style for the form from a predefined
list, change the cursor, set the font for the text on the form and so on.
Behavior
Notable Behavior property is the enabled property which lets us enable/disable the
form by setting the property to True/False.
Layout
Layout properties are all about the structure of the form. With these properties we
can set the location of the form, maximum size of the form, minimum size of the form,
exact size of the form with the size property when designing. Note the property start
position, this property lets you specify the location of the form where it should
appear when you run the application which you can select from a predefined list.
Window Style
The notable property under this is the ControlBox property which by default it is
set to True. Setting the property to False makes the minimize, maximize and cancel
buttons on the top right side of the form invisible.
Form Event
The default event of a form is the load event which looks like this in code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs)_
Handles MyBase.Load
End Sub
|
You can write code in the load event of the form just like you write for all other
controls.
An Example:
You can run the Form by selecting Debug->Start from the
main menu or by pressing F5 on the keyboard. When you run a blank form with no controls
on it, nothing is displayed. It looks like the image below.
Now, add a TextBox and a Button to the form from the toolbox. The toolbox can be selected
from
View->ToolBox on the main menu or by holding Ctrl+Alt+X on
the keyboard. Once adding the TextBox and Button is done, run the form. The output
window displays a TextBox and a Button. When you click the Button nothing happens.
We need to write an event for the Button stating something should happen when you
click it. To do that get back to design view and double-click on the
Button. Doing that opens an event handler for the Button where you specify what
should happen when you click the button. That looks like this in code.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
End Sub
End Class
|
Place the code TextBox1.Text="Welcome to Forms" in the Click event
of the Button and run the application. When you click the Button the output "Welcome
to Forms" is displayed in the TextBox.
Alternatively, you can also use the MsgBox or MessageBox functions
to display text when you click on the Button. To do that place a Button on the form
and double-click on that to open it's event. Write this line of code, MsgBox("Welcome
to Forms") or MessageBox.Show("Welcome to Forms").
It looks like this in code.
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
MsgBox("Welcome to Forms")
End Sub
or
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
MessageBox.Show("Welcome to Forms")
End Sub
|
When you run the form and click the Button, a small message box displays, "Welcome
to Forms". The image below displays that.
Next>> Adding a New Form,
Working with Multiple Forms, Visual Inheritance
|