Chapter 24: Creating a Project Icon

 

Enabling and Disabling Controls

There is one Visual Studio C# statement that enables or disables a control. The controls that are most frequently enabled or disabled are buttons , textBoxes, radioButtons, and checkBoxes:

 button1.Enabled = false; 

-or-

 button1.Enabled = true; 

-or-

 textBox1.Enabled = false; 

-or-

 textBox1.Enabled = true; 

The user can accomplish nothing with a disabled control. As an example, see the following window from the KT Weekly Data Posting project and note how the programmer was finally able to guide the user through this window to get meaningful results by enabling and disabling controls on the window at the proper times.

The problem with the window in Figure 20-1 is that some users neglected to enter a year in the first text box or a week in the second text box. Instead, they went directly to the Post Current Portfolio Prices and Print Net Worth button.

image from book
Figure 20-1: KT Weekly Data Posting window

The Post Current Portfolio Prices and Print Net Worth button opened the next window, but there was insufficient data to continue the process because the user had not entered the year and week numbers in the first window.

The user could enter a Price per share -> number, but once he clicked on the Perform Computations on this Stock/Bond/Fund button, the program defaulted. Why? Because the program had no idea which data should be retrieved to calculate the six items from Year-Week to Gain or (Loss). What to do?

It was evident that the Post Current Portfolio Prices and Print Net Worth button in Figure 20-1 had to be disabled initially, then enabled after the user entered proper data in the two text boxes. But how do you know when the user has entered data into both the year and the week text boxes in Figure 20-1?

One proposed solution was to enable the Post Current Portfolio button (Figure 20-1) as soon as the program left textBox 2 (the one where the user enters a week of the year). Is this possible? Can you leave that button in the disabled state and enable it just as the program leaves textBox 2?

The answer is no, because the only mechanism to inform textBox 2 that it was being exited by the user (so the enabled statement for the Post Current Portfolio button could be executed in the Leave event handler) was to click on another control (shift the focus away from textBox 2). The only way to enable the Post Current Portfolio button was to introduce an intermediate pushbutton named Both Numbers Entered. This solved the problem, but it made the users angry that they had to pass through so many checkpoints to navigate the window. Figure 20-3 shows the final arrangement:

image from book
Figure 20-3: Final arrangement of Weekly Data Posting window 1

When the program is loaded, disable both the Post Current Portfolio Prices and Print Net Worth button and the Both Numbers Entered button:

 KTW050:     private void Form1_Load(object sender, System.EventArgs e) KTW051:     { // Disable two buttons. KTW052:       button1.Enabled = false; // 'Post Current Portfolio Prices'. KTW053:       button3.Enabled = false; // 'Both Numbers Entered'. KTW054:     } 

As the user leaves the first text box (textBox 1), enable the Both Numbers Entered button:

 KTW101:     private void textBox1_Leave(object sender, System.EventArgs e) KTW102:     { // Year number. KTW103:       strTB1 = textBox1.Text; KTW104:       string strShortYear = strTB1.Substring(2,2); KTW105:       intYear = Convert.ToInt32(strShortYear); // This is sent to Form2. KTW106:       button3.Enabled = true; // Enable 'Both Numbers Entered'. KTW107:     } 

Hopefully the user would enter data in the second text box (week), and the Both Numbers Entered button would be enabled and ready to carry on the program. The Both Numbers Entered event handler performs some bookkeeping tasks , then enables the Post Current Portfolio button in line KTW176 below.

 KTW170:     private void button3_Click(object sender, System.EventArgs e) KTW171:     { // Both numbers entered.               // Create the 'Year-Week' number to place in the data files. KTW172:       string SubString1 = strTB1.Substring(2,2); // Last 2 digits in calendar year. KTW173:       strYW = SubString1 + "-" + strTB2; KTW174:       strShortYW = SubString1 + strTB2;               // Write 'Year-Week' number to Form1, label11. KTW175:       label11.Text = "Year-Week Number is '" + strYW + "' ."; KTW176:       button1.Enabled = true; // 'Post Current Portfolio' button. KTW177:     } 

Now the user can click on the Post Current Portfolio button and the programmer who wrote the sequence is pretty much assured that all the information will be present when the user reaches the next window, shown in Figure 20-2.

image from book
Figure 20-2: Weekly Data Posting window 2
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net