374 Part III Designing the User Interface
To Do This
Create a new form
with program code
and set its properties
Create the form by using the Dim and New keywords and the Form
class, and then set any necessary properties. For example:
Dim form2 As New Form
form2.Text = "My New Form"
Position a startup form
on the Windows
desktop
Set the StartPosition property to one of the available options, such as
CenterScreen or CenterParent.
Size and position
a startup form on
the Windows desktop
by using code
Set the StartPosition to Manual, declare a Rectangle structure that
defines the form’s size and position, and then use the DesktopBounds
property to size and position the form on the desktop. For example:
form2.StartPosition = FormStartPosition.Manual
Dim Form2Rect As New Rectangle(200, 100, 300, 250)
form2.DesktopBounds = Form2Rect
Minimize, maximize,
or restore a form at
run time
Set the MaximizeBox and MinimizeBox properties for the form to
True in design mode to allow for maximize and minimize operations.
In the program code, set the form’s WindowState property to
FormWindowState .Minimized, FormWindowState .Maximized, or
FormWindowState .Normal when you want to change the window
state of the form.
Add controls to
a form at run time
Create a control of the desired type, set its properties, and then add it
to the form’s Controls collection. For example:
Dim button1 as New Button
button1.Text = "Click Me"
button1.Location = New Point(20, 25)
form2.Controls.Add(button1)
Anchor an object
a specific distance
from specific edges
of the form
Set the Anchor property of the object, and specify the edges you
want to remain a constant distance from. Use the Or operator when
specifying multiple edges. For example:
Button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
Dock an object to one
of the form’s edges
Set the Dock property of the object, and then specify the edge you want
the object to be attached to. For example:
PictureBox1.Dock = DockStyle.Top
Specify the startup
form in a project
Click the Properties command on the Project menu to open the
Project Designer. For a Windows Forms Application project, you can
specify any form in your project as the startup form by clicking the
form name in the Startup Form list box.
Create a Visual Basic
program with no
user interface (or
only a command-line
interface)
Create a console application project by clicking the New Project
command on the File menu, clicking the Console Application
template, and then clicking OK. You then add the program code to one
or more modules, not forms, and execution begins with a procedure
named Sub Main.