Windows ControlsAll of the basic Windows controls work in much the same way as the TextBox and Button we have used so far. Many of the more common ones are shown in the Windows Controls program in Figure 3-4. Figure 3-4 A selection of basic Windows controls Each of these controls has properties such as Name, Text, Font, Forecolor, and Borderstyle that you can change most conveniently using the properties window shown at the right of Figure 3-2. You can also change these properties in your program code as well. The Windows Form class that the designer generates always creates a Form1 constructor that calls an InitializeComponent method like the preceding one. Once that method has been called, the rest of the controls have been created, and you can change their properties in code. Generally, we will create a private init() method that is called right after the Initialize-Component method, in which we add any such additional initialization code.LabelsA label is a field on the window form that simply displays text. Usually programmers use this to label the purpose of text boxes next to them. You can't click on a label or tab to it so it obtains the focus. However, if you want, you can change the major properties in Table 3-1 either in the designer or at runtime.Table 3-1 Properties for the Label Control Property Value Name At design time only BackColor A Color object BorderStyle None, FixedSingle, or Fixed3D Enabled True or false. If false, grayed out. Font Set to a new Font object Forecolor A Color object Image An image to be displayed within the label ImageAlign Where in the label to place the image Text Text of the label Visible True or false
TextBoxThe TextBox is a single line or multiline editable control. You can set or get the contents of that box using its Text property.
the control acts like a toggle button that stays depressed when you click on it and becomes raised when you click on it again. All the properties in Table 3-1 apply as well.ButtonsA Button is usually used to send a command to a program. When you click on it, it causes an event that you usually catch with an event handler. Like the CheckBox, you create this event handler by double-clicking on the button in the designer. All of the properties in Table 3-1 can be used as well. Buttons are also frequently shown with images on them. You can set the button image in the designer or at runtime. The images can be in bmp, gif, jpeg, or icon files.Radio ButtonsRadio buttons or option buttons are round buttons that can be selected by clicking on them. Only one of a group of radio buttons can be selected at a time. If there is more than one group of radio buttons on a window form, you should put each set of buttons inside a Group box as we did in the program in Figure 3-4. As with checkboxes and buttons, you can attach events to clicking on these buttons by double-clicking on them in the designer. Radio buttons do not always have events associated with them. Instead, programmers check the Checked property of radio buttons when some other event, like an OK button click, occurs.ListBoxes and ComboBoxesBoth ListBoxes and ComboBoxes contain an Items array of the elements in that list. A ComboBox is a single-line drop-down that programmers use to save space when selections are changed less frequently. ListBoxes allow you to set properties that allow multiple selections, but ComboBoxes do not. Some of their properties include those in Table 3-3.Table 3-3 The ListBox and ComboBox Properties