FREE BOOK

Writing Windows C# Programs

Posted by Addison Wesley Free Book | C# Language July 21, 2009
The C# language has its roots in C++, Visual Basic, and Java. Both C# and VB.Net use the same libraries and compile to the same underlying code. Both are managed languages with garbage collection of unused variable space, and both can be used interchangeably. Both also use classes with method names that are very similar to those in Java, so if you are familiar with Java, you will have no trouble with C#.

The Items Collection

You use the Items collection in the ListBox and ComboBox to add and remove elements in the displayed list. It is essentially an ArrayList, as we discuss in Chapter 7. The basic methods are shown in Table 3-4. If you set a ListBox to a multiple selection mode, you can obtain a collection of the selected items or the selected indexes by

           ListBox.SelectedIndexCollection it =
         new ListBox.SelectedIndexCollection(lsCommands);
            ListBox.SelectedObjectCollection so =
            new ListBox.SelectedObjectCollection(lsCommands);


where lsCommands is the ListBox name.

Table 3-4 Methods for the Items Collection 

Method Value
Add Add object to list
Count Number in list
Item[i] Element in collection
RemoveAt(i) Remove element i

Menus

You add a menu to a window by adding a MainMenu control to the window form. Then you can select the menu control and edit its drop-down names and new main item entries, as shown in Figure 3-5.



Figure 3-5 Adding a menu to a form

As with other clickable controls, double-clicking on one in the designer creates an event whose code you can fill in.

ToolTips

A ToolTip is a box that appears when your mouse pointer hovers over a control in a window. This feature is activated by adding an (invisible) ToolTip control to the form and then adding specific ToolTip control and text combinations to the control. In our example in Figure 3-4, we add ToolTips text to the button and ListBox using the tips control we have added to the window.

         tips.SetToolTip(btPush, "Press to add text to list box");
         tips.SetToolTip(lsCommands, "Click to copy to text box");

 This is illustrated in Figure 3-6.



Figure 3-6 A ToolTip over a button

We discuss how to use the DataGrid and TreeList in Chapters 14 and 15, and Toolbar is discussed in Chapters 28 and 29.

The Windows Controls Program

The Windows Controls program, shown in Figure 3-4, controls changes in the text in the label.

  • Font size is set from the combo box.
  • Font color is set from the radio buttons.
  • Boldface is set from the check box.
For the check box, we create a new font that is either lightface or boldface, depending on the state of the check box.

       private void ckBold_CheckedChanged(object sender, EventArgs e)
        {
            if (ckBold.Checked)
            {
                lbText.Font = new Font("Arial",
                fontSize, FontStyle.Bold);
            }
           
else
            {
                lbText.Font = new Font("Arial", fontSize);
            }
        }


When we create the form, we add the list of font sizes to the combo box.

        private void init()
        {
            fontSize = 12;
            cbFont.Items.Add("8");
            cbFont.Items.Add("10");
            cbFont.Items.Add("12");
            cbFont.Items.Add("14");
            cbFont.Items.Add("18");
            lbText.Text = "Greetings";
            tips.SetToolTip(btPush, "Press to add text to list box");
            tips.SetToolTip(lsCommands, "Click to copy to text box");
        }


When someone clicks on a font size in the combo box, we convert that text to a number and create a font of that size. Note that we just call the check box changing code so we don't have to duplicate anything.

        private void cbFont_SelectedIndexChanged(
        object sender, EventArgs e)
        {
            fontSize = Convert.ToInt16(cbFont.SelectedItem);
            ckBold_CheckedChanged(null, null);
        }


For each radio button, we click on it and insert color-changing code.

       private void opGreen_CheckedChanged(object sender, EventArgs e)
        {
            lbText.ForeColor = Color.Green;
        }
        private void opRed_CheckedChanged(object sender, EventArgs e)
        {
            lbText.ForeColor = Color.Red;
        }
        private void opBlack_CheckedChanged(object sender, EventArgs e)
        {
            lbText.ForeColor = Color.Black;
        }

When you click on the ListBox, it copies that text into the text box by getting the selected item as an object and converting it to a string.

        private void lsCommands_SelectedIndexChanged(
       object sender, EventArgs e)
        {
            txBox.Text = lsCommands.SelectedItem.ToString();
        }

Finally, when you click on the File | Exit menu item, it closes the form and,
hence, the program.

       private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Total Pages : 6 23456

comments