To access the default instance of a form at run time (Visual Basic only)
1. Refer to the form by its name. You can call methods or access properties from this default instance. For example:
' VB
Form1.Text = "This is my form"
Form1.Show()
2. If referring to a form from within that form’s code, you cannot use the default instance. You must use the special keyword Me (Visual Basic) or this (C#) to access the form’s properties and methods.
To access a form’s methods and properties from inside its code
1. Use the keyword Me (Visual Basic) or this( C#). For example:
' VB
Me.Text = "J and J's Wine Shop – Main Page"
// C#
this.Text = "J and J's Wine Shop – Main Page";
2. You can also create new instances of forms at run time by declaring a variable that represents a type of form and creating an instance of that form.
To add a form to your application at run time
1. Declare and instantiate a variable that represents your form. This example assumes that you have already designed a form named Form1 in your project:
' VB
Dim myForm As Form1
myForm = New Form1()
' Displays the new form
myForm.Show()
// C#
Form1 myForm;
myForm = new Form1();
// Displays the new form
myForm.Show();
Properties of Windows Forms
The visual appearance of your user interface is an important part of your application. A user interface that is poorly designed is difficult to learn and will, therefore, increase training time and expense. You can modify the appearance of your user interface by using Windows Forms properties.
Tuesday, 1 June 2010

Figure 1-1 A Windows Form in the Visual Studio IDE
Adding Forms to Your Project
Most projects will require more than one form. You can add and configure additional forms at design time, or you can create instances of pre-designed forms in code at run time.
To add a new form to your project at design time
1.
From the Project menu, select Add Windows Form. The Add New Item dialog box opens.
2.
Select Windows Form and type a name for the new form in the Name box. Click
Add to add the form to the development environment. You can add and configure as many forms as your application needs at design time. You can also create new instances of forms in your code. This method is most often employed when you want to display a form that has already been designed. In Visual Basic, you can access default instances of a form by referring to that form by name. For example, if you have a form named Form1 in your application, you can refer to it directly by its name, Form1.
Adding and Configuring Windows Forms
This lesson describes how to create and configure Windows Forms. You will learn how to create forms and refer to them in code, alter the visual properties of the form, and control the behavior of the form at run time.
After this lesson, you will be able to:
■ Add a Windows Form to a project at design time.
■ Add a new Windows Form at run time.
■ Resize a window at design time or run time.
■ Identify and set the properties that determine a form’s appearance and behavior at run time. ■ Refer to the default instance of a form in code.
■ Create a non-rectangular form. Estimated lesson time: 45 minutes
Overview of Windows Forms
Windows Forms are the basic building block of the user interface. They provide a container that hosts controls and menus and allow you to present an application in a familiar and consistent fashion. Forms can receive user input in the form of keystrokes
or mouse interactions and can display data to the user through hosted controls.
Although it is possible to create applications that do not contain forms, such as console applications or services, most applications that require sustained user interaction will include at least one Windows Form, and complex applications frequently
require several forms to allow the program to execute in a consistent and logical fashion.
When you create a new Windows Forms project, a form named Form1 is added to your project by default. You can edit your form by adding controls and other visual elements in the designer, which is a graphic representation of a designable, visual element
(such as a Form) that appears in the Visual Studio Integrated Development Environment
(IDE).
After this lesson, you will be able to:
■ Add a Windows Form to a project at design time.
■ Add a new Windows Form at run time.
■ Resize a window at design time or run time.
■ Identify and set the properties that determine a form’s appearance and behavior at run time. ■ Refer to the default instance of a form in code.
■ Create a non-rectangular form. Estimated lesson time: 45 minutes
Overview of Windows Forms
Windows Forms are the basic building block of the user interface. They provide a container that hosts controls and menus and allow you to present an application in a familiar and consistent fashion. Forms can receive user input in the form of keystrokes
or mouse interactions and can display data to the user through hosted controls.
Although it is possible to create applications that do not contain forms, such as console applications or services, most applications that require sustained user interaction will include at least one Windows Form, and complex applications frequently
require several forms to allow the program to execute in a consistent and logical fashion.
When you create a new Windows Forms project, a form named Form1 is added to your project by default. You can edit your form by adding controls and other visual elements in the designer, which is a graphic representation of a designable, visual element
(such as a Form) that appears in the Visual Studio Integrated Development Environment
(IDE).
Subscribe to:
Posts (Atom)
