Adding Features to a Windows Forms Application

From webpage

Adding Features to a Windows Forms Application

 
(Page 1 of 5 )

Welcome to the conclusion of a two-part series on designing a Windows Forms application with Visual Studio. In this part we'll learn how to add properties, controls, navigation features, and more. This article is excerpted from chapter two of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456)

Common properties for forms and controls

Figure 2-6 shows some common properties for forms and controls. The first two properties apply to both forms and controls. The other properties are presented in two groups: properties that apply to forms and properties that apply to controls. Note that some of the control properties only apply to certain types of controls. That's because different types of controls have different properties.

Since all forms and controls must have a Name property, Visual Studio creates generic names for all forms and controls, such as Form1 or Button1. Often, though, you should change these generic names to something more meaningful, especially if you're going to refer to them in your Visual Basic code.

To make your program's code easier to read and understand, you can begin each name with a two- or three-letter prefix in lowercase letters to identify the control's type. Then, you can complete the name by describing the function of the control. For instance, you can use a name like btnExit for the Exit button and txtSubtotal for the Subtotal text box.

For Label controls, you can leave the generic names unchanged unless you plan on modifying the properties of the labels in your code. For example, if you want to use a label control to display a message to the user, you can give that label a meaningful name such as lblMessage. But there's no reason to change the names for label controls that display text that won't be changed by the program.

Forms and most controls also have a Text property that is visible when the form is displayed. A form's Text property is displayed in the form's title bar. For a control, the Text property is usually displayed somewhere within the control. The Text property of a button, for example, is displayed on the button, and the Text property of a text box is displayed in the text box.

As you work with properties, you'll find that you can set some of them by selecting a value from a drop-down list. For example, you can select a True or False value for the TabStop property of a control. For other properties, you have to enter a number or text value. And for some properties, a button with an ellipsis (...) is displayed. Then, when you click this button, a dialog box appears that lets you set the property.

The Name property

  1. Sets the name you use to identify a control in your Visual Basic code. 
  2. Can be changed to provide a more descriptive and memorable name for forms and controls that you will refer to when you write your code (such as text boxes and buttons). 
     
  3. Doesn't need to be changed for controls that you won't refer to when you write your code (such as most labels). 
     
  4. Can use a three-letter prefix to indicate whether the name refers to a form (frm), button (btn), label (lbl), or text box (txt).

The Text property

  • Sets the text that's displayed on the form or control. Some controls such as forms and labels display the generic form or control name that's generated by Visual Studio, which you'll almost always want to change. 
  • For a form, the Text value is displayed in the title bar. For controls, the Text value is displayed directly on the control. 
     
  • For a text box, the Text value changes when the user types text into the control, and you can write code that uses the Text property to get the text that was entered by the user.  

Other properties for forms  

 

 

Property

Description

AcceptButton

Identifies the button that will be activated when the user presses the Enter key.

CancelButton

Identifies the button that will be activated when the user presses the Esc key.

StartPosition

Sets the position at which the form is displayed. To center the form, set this property to CenterScreen.

 

 

Other properties for controls  

 

 

Property

Description

Enabled

Determines whether the control will be enabled or disabled.

ReadOnly

Determines whether the text in some controls like text boxes can be edited.

TabIndex

Indicates the control's position in the tab order, which determines the order in which the controls will receive the focus when the user presses the Tab key.

TabStop

Determines whether the control will accept the focus when the user presses the Tab key to move from one control to another. Some controls, like labels, don't have the TabStop property because they can't receive the focus.

TextAlign

Sets the alignment for the text displayed on a control.

 

 

 

Figure 2-6. Common properties for forms and controls

No comments: