6.4. Inspecting and Generating Control Events

What additional events are associated with a TextBox control? More generally, how can we discover the set of events associated with a control?

To inspect the set of events associated with a control, we do the following: (1) click on the control to display its Properties window, and (2) in the Properties window, click on the yellow jagged lightning icon. The Properties window now displays the set of events associated with the current control (as Figure 6.10 shows).

Figure 6.10. TextBox-Supported Events


The entries on the left in the Properties window represent each of the events supported by the control. The entries on the right, if present, represent registered callback methods. (In Figure 6.10, only TextChanged displays an associated callback method.) To see a summary description of the event's meaning, click on the event once. At the bottom of the Properties window, a summary description is displayed. To generate the stub callback method for the event and the code to register it, double-click on the event.

6.4.1. Labels Are Programmable

At first glance, labels look like one of the more uninteresting controls. Typically, we drag a label to its position, set its text, and that's all. However, because we are able to set and unset its text during program execution, a label serves double duty as an attractive target for setting text dynamically.

For example, let's redo our first program to respond by writing into a blank label in the form rather than popping up a message box. The initial form is pictured in Figure 6.11. Note that label2 has its Text field set to an empty string (we'll set this field in response to the OK button event).

Figure 6.11. Targeting a Label at Runtime


When this program is executed, the form pops up with the second label (which is not visible because we have set its Text property to empty). Alternatively, we can ignore the Text property. Rather we initialize the label's Visible property to false. Whenever a control's Visible property is false, that control is not displayed to the user. The OK button click event would then set both the Text property to the greeting and the Visible property to true. Here is how that might look:

protected void button1_Click (object sender, EventArgs e)
{
      label2.Text = "Hello," + "
" + textBox1.Text;
      label2.Visible = true;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168