HomeC# C# Simplified, part 7: Working with WinFor...
C# Simplified, part 7: Working with WinForm Controls
In the previous article, we saw the fundamentals behind programming with .NET WinForms. In this article, we will examine the working of some of the important controls, which are used in normal windows applications, with the help of examples.
With the help of the Button control, you can perform some kind of activity on your application. For instance, clicking a button can generate a MessageBox or another Form. Further, you can sometimes add records to a database with the click of a button. The base class of Button is ButtonBase. This is also the base class for CheckBox and RadioButton controls. Listing 7.1 illustrates the usage of this important control:
Listing 7.1
001: // MyButton.cs 002: // ----------- 003: using System; 004: using System.Drawing; 005: using System.Windows.Forms; 006: 007: public class MyButton:Form 008: { 009: Button b1 = new Button(); 010: 011: MyButton() 012: { 013: b1.Text = "OK"; 014: b1.BackColor = Color.Silver; 015: b1.Size = new Size(60,30); 016: b1.Location = new Point(50,40); 017: b1.FlatStyle = FlatStyle.Popup; 018: this.Controls.Add(b1); 019: this.Text = "Button Control Demo"; 020: } 021: 022: public static void Main() 023: { 024: MyButton b = new MyButton(); 025: Application.Run(b); 026: } 027: }
Output
Figure 7.1
You will note that nothing will happen if you click the buttons. This is because we didn't applied events. We will explore this concept in a later part of this article.
Checkboxes enable you to select more than one item at a time. This control is widely used in all applications. The moment you click on an item, a tick mark appears on the control. The tick mark will disappear if you click it again. Listing 7.2 illustrates this control:
Listing 7.2
001: using System; 002: using System.Drawing; 003: using System.Windows.Forms; 004: 005: public class MyCheckbox:Form 006: { 007: CheckBox c1 = new CheckBox(); 008: CheckBox c2 = new CheckBox(); 009: CheckBox c3 = new CheckBox(); 010: 011: MyCheckbox() 012: { 013: c1.Text = "Football"; 014: c2.Text = "BasketBall"; 015: c3.Text = "Swimming"; 016: 017: c1.Location = new Point(50,40); 018: c2.Location = new Point(50,60); 019: c3.Location = new Point(50,80); 020: 021: this.Controls.Add(c1); 022: this.Controls.Add(c2); 023: this.Controls.Add(c3); 024: 025: this.Text = "CheckBox Control Demo"; 026: } 027: 028: public static void Main() 029: { 030: MyCheckbox mychk = new MyCheckbox(); 031: Application.Run(mychk); 032: } 033: }
With RadioButtons, you can let your users select a single choice at a time, rather than a set of choices as seen in the previous session. Listing 7.3 demonstrates the usage of this control:
Listing 7.3
001: using System; 002: using System.Drawing; 003: using System.Windows.Forms; 004: 005: public class MyRadiobutton: Form 006: { 007: RadioButton rad1 = new RadioButton(); 008: RadioButton rad2 = new RadioButton(); 009: GroupBox grp = new GroupBox(); 010: 011: MyRadiobutton() 012: { 013: 014: rad1.Text = "Employed"; 015: rad1.Location = new Point(20,20); 016: 017: rad2.Text = "Unemployed"; 018: rad2.Location = new Point(20,40); 019: 020: grp.Location = new Point(20,20); 021: grp.Text = "Your Employment Status"; 022: grp.BackColor = Color.Silver; 023: 024: // radio buttons added to group box 025: 026: grp.Controls.Add(this.rad1); 027: grp.Controls.Add(this.rad2); 028: 029: // group box added to the form 030: 031: this.Controls.Add(grp); 032: this.Text = "RadioButton Control Demo"; 033: } 034: 035: 036: public static void Main() 037: { 038: MyRadiobutton myrad = new MyRadiobutton(); 039: Application.Run(myrad); 040: } 041: }
From the above listing, you can see that RadioButtons are first added to a GroupBox. This is necessary, because otherwise you will not get the functionality of the RadioButton. If you have worked with Visual Basic 6.0, you will be able to infer that GroupBox is similar to Frames. After this process, the GroupBox instance is added to the Form. It is not necessary for you to add each and every RadioButton instance to the form.
A label is one of the important controls used with all kinds of applications, be they Windows or ASP.NET applications. They normally sit alongside the Textbox control, but labels will also find a place with other controls such as combo box and list box as well. It tells you what information to enter inside a specific control. The text inside the label controls cannot be edited. It cannot be modified by users. Listing 7.4 adds a Textbox and a label control to the form. Once you have worked with this example you can further modify the program by adding more controls.
Label 7.4
001: using System; 002: using System.Drawing; 003: using System.Windows.Forms; 004: 005: public class MyLabel: Form 006: { 007: 008: // Object of TextBox created 009: TextBox t1 = new TextBox(); 010: // Object of Label created 011: Label l1 = new Label(); 012: 013: MyLabel() 014: { 015: this.Controls.Add(l1); 016: this.Controls.Add(t1); 017: t1.Location = new Point(50,50); 018: t1.Size = new Size(200,70); 019: t1.Multiline = true; 020: t1.ScrollBars = ScrollBars.Vertical; 021: t1.CharacterCasing = CharacterCasing.Upper; 022: 023: l1.Text = "Enter your comments:"; 024: l1.Location = new Point(50,30); 025: l1.Size = new Size(150,20); 026: 027: this.Text = "Label Control Demo"; 028: } 029: 030: public static void Main() 031: { 032: Application.Run(new MyLabel()); 033: } 034: }
Combo boxes enable you to select an item, but only one at a time. It consists of a text box with an arrow on the right side as shown in figure 7.5:
Figure 7.5
If you click the arrow, the combo box displays all the items within it as a drop-down menu, as shown in figure 7.6
Figure 7.6
The complete code for producing the above output is shown in the listing given below.
Listing 7.5
001: using System; 002: using System.Drawing; 003: using System.Windows.Forms; 004: 005: public class MyCombo: Form 006: { 007: // object of combo box created 008: ComboBox cb = new ComboBox(); 009: 010: MyCombo() 011: { 012: 013: this.Controls.Add(cb); 014: cb.Items.Add("Visual Basic .NET"); 015: cb.Items.Add("Visual C# .NET"); 016: cb.Items.Add("Visual J# .NET"); 017: cb.Text = "Please select"; 018: 019: cb.Location = new Point(50,50); 020: cb.Size = new Size(200,70); 021: this.Text = "C# FAQ - Combo box Control"; 022: 023: } 024: 025: public static void Main() 026: { 027: Application.Run(new MyCombo()); 028: } 029: }
In the above code, items are added to the combo box using the Add() method of Items collection. You can also use the AddRange() method of Items collection as shown in listing 7.6:
Inside the method you are declaring an array object along with the specified values. If you need to add more items to the combo box you should update both the array index value and the items.
You can easily add items to a combo box if you are using Visual Studio .NET. Select the combo box and click the small ellipse on the right side of the Items property. Enter one value for each line and click the OK button to add the items to your combo box. After completing this process, you are ready to test your application by pressing the F5 key.
The process of creating a list box is the same as for a combo box. The only difference is that you can select more than one item using a list box. By default, though, you can select only one item. In order to select multiple items you have to set the SelectionMode property of the list box to MultiSimple or MultiExtended.
The MultiSimple property enables you to select all items with mouse clicks. It toggles the selected item. On the other hand, the MultiExtended property enables you to select items by holding either the Shift or Control key. The code for producing a list box using the MultiSimple property is shown in listing 7.7
Listing 7.7
001: using System; 002: using System.Drawing; 003: using System.Windows.Forms; 004: 005: public class MyListbox: Form 006: { 007: // Object of Label created 008: ListBox lb = new ListBox(); 009: 010: MyListbox() 011: { 012: this.Controls.Add(lb); 013: lb.Items.Add("Visual Basic .NET"); 014: lb.Items.Add("Visual C# .NET"); 015: lb.Items.Add("Visual J# .NET"); 016: lb.Text = "Please select"; 017: lb.SelectionMode = SelectionMode.MultiSimple; 018: lb.Location = new Point(50,50); 019: lb.Size = new Size(200,70); 020: this.Text = "C# FAQ - List box Control"; 021: } 022: 023: public static void Main() 024: { 025: Application.Run(new MyListbox()); 026: } 027: }
Output
In the next article, you will learn how to handle events using the above explained controls.