Importing a Project Icon

 

Focus

The principal way for a user to request that a control receive the immediate focus of the operating system is to mouse-click on that control. This works fine most of the time, but sometimes the user expects the programmer to preset the focus to the proper control ( especially when the window is first opened). The programmer can set the most probable sequence in which controls will be addressed by the user by carefully setting the TabIndex numbers in all the controls on one window. The system will always choose the lowest numbered tab first.

As an example, the programmer would set the TabIndex (a property of all controls) for the first TextBox control in Figure 20-1 to 0 and the TabIndex for the second TextBox control in Figure 20-1 to 1. When the programmer is creating controls on a window template, the IDE will always assign the first control a TabIndex of 0, the next control a 1, etc. However, if the sequence must change later, the programmer must renumber the TabIndexes in the Properties window while each control is highlighted in the window template. This often alleviates the focus problem ” but not always. It is important that no two controls have the same TabIndex number.

A Typical Focus Problem

This section offers a demonstration of a focus problem that presented itself during the creation of a Keeping Track program named KTWDP (a shortened version of KTWeeklyDataPosting).

  1. Enter the IDE and bring project KTWDP from the Visual Studio 2005\Projects\DemosSourceCode\KTWDP folder into the IDE screen.

  2. Compile and run the program.

This program has one minor error that could cause much grief among the users. It goes like this: If the user opens this program and then immediately decides to exit the program, the user is given a run-time error like this:

image from book
Figure 20-4: Focus problem

Finished programs are not supposed to have these kinds of problems, so how can it be fixed?

Once you have executed KTWDP in the IDE and the main window appears (see Figure 20-1), immediately click on the Return to Main button at the lower right of the window. What happens? You should see the error message in Figure 20-4 (or something similar).

Recycle project KTWDP and look closely at the first window (Figure 20-1). What control in this window has the focus? It s the text box at the top right of the window (textBox1). As soon as this window comes up, the focus is placed on the first text box, and the program expects the user to type data here. (The cursor is blinking in textBox1, which indicates that the focus is there.)

In this case, however, the user simply clicked on the Return to Main button and the operating system became angry . How can we avoid this problem? Do not allow textBox1 to hold the focus when the window first opens.

How do we do this? Recall that every control has a TabIndex, an integer that is assigned by the IDE as the programmer creates controls on a window template. As the KTWDP project is now configured, textBox1 has a TabIndex of 0, which means that the focus moves to this text box as soon as the window is opened. You can confirm that by bringing the Form1 window template into the center of the IDE and clicking on the top text box (textBox1). This action brings the textBox1 properties into the Properties window, and by scrolling down to Behavior and TabIndex you will see its assigned number is 0.

To alleviate the problem of an error in terminating the project immediately (by clicking on Return to Main), we will switch the TabIndex of textBox1 with another control. For example, while the Form1 window template is in the IDE and the textBox1 control is highlighted, scroll down to the TabIndex and change the number 0 to 8. This means that you now have two controls with a TabIndex of 8, but you are about to fix that. Click on the KT Weekly Data Posting label at the top of the template (to place its properties in the Properties window), and change its TabIndex from 8 to 0.

Note  

The only reason we swap two TabIndexes at a time is to make sure that we don t duplicate a number (the compiler prefers one-of-a-kind TabIndexes for active control). It does not care what TabIndex number is assigned to something like a label. You could just as well have assigned textBox1 a TabIndex of 98 and the second text box a TabIndex of 99.

Once you have swapped the two TabIndexes, recompile the project and run it. As soon as the main window appears, note that the cursor is no longer present in the upper text box. Click on the Return to Main button. The run-time error should not appear this time.

The Tradeoffs

As the project was originally configured, as soon as the main window came up (Figure 20-1) the focus was placed on the upper text box and the user could begin typing immediately. After swapping TabIndexes between the top text box and the label at the top of the window, the focus is on the label and not on the text box. The user must now click on the top text box to type data there. This requires the user to perform one more task in this window.

However, the run-time error will not appear now if the user clicks on the Return to Main button immediately. There is no free lunch in this world. For a complete tagged listing of project KTWDP, see the file named App D -- DemoVC#TaggedList.doc in the Visual Studio 2005\ Tagged KT-Demo Code directory.

 


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