Use of winform control focus

Source: Internet
Author: User
Document directory
  • Explicitly specify a control to get the focus
  • Specify default focus
  • Determine which control gets focus

In C #'s desktop application development, it is generally a page (form), and there are many controls in it ).
The so-called focus is the control you selected, and the control gets the focus. for example, you can click a control or use a shortcut key to select a control. if the area is editable, there will be a shining vertical line. If it is not editable, it will be highlighted.

 

What is the use of focus?

The concept of focus is not often used in code, but it is often used in two familiar scenarios.

One scenario is,When we fill in a bunch of registration information on a page, click Submit. if this parameter is not specified, an error is reported. then let the text area get the focus. in this way, you can directly enter the content without having to move the mouse down to get the focus and then enter it. therefore, specifying a control to obtain the focus can facilitate user operations.

The two scenarios are,When you use Visual Studio, click F1 to bring up the help document. the content displayed in the help document is related to the content of your current focus. therefore, if you want to create a product, when the user focus is in some editing areas, You need to determine which control gets the focus first if you want F1 to bring up the corresponding help document.

 

Usage of focus

 

Explicitly specify a control to get the focus

If there is a button btnok, the text box txtlogin. Then the two methods to get the focus are to call btnok. Focus (); and txtlogin. Focus ();

The same is true for other controls. You can call the function focus.

 

Specify default focus

However, if a page is opened, a control should be focused by default. It seems that the focus () method is not used.

You haveThe Property (properties) of the control specifies that the tabindex is 0.In this way, when the window (form) is opened, the control gets the focus by default. you may ask what tabindex is. we will see a tab key on the left of the keyboard. When we press it, We will select different controls, which is equivalent to constantly switching. whileThe switch order is to specify the number size by tabindex. Therefore, if you set tabindex to 0, it is equivalent to the first one. It is the default focus..

 

Special situations in which tabindex values are the same

However, the tabindex values of each control can be the same. You may ask what if the tabindex values of the two controls are equal?

If you often think that the values are the same, you should get the focus in the top-down and left-right order. However, I tried to find that this is not the case.Suppose There are buttons btn1, text box txt2, and text box txt3 from left to right.. Assume that their tabindex values are 3, 2, and 1 in sequence. no problem. The default focus is txt3. if it is 3, 2, 2 in sequence, tabindex is 1. because txt2 occupies the front, txt2 obtains the focus.

However, ifIt is strange that the tabindex values of the three are 3.. You may think it will be btn1's focus, because it is at the top, but the answer is wrong. well, it should be txt2, which ranks 2nd. unfortunately not. it turns out that txt3. by default, txt3. without txt3, txt2 gets the focus no matter where it is.Therefore, it seems that the priority of the Editable control to get the focus is higher than that of the control that cannot be edited. If the priority is the same, the last one to get the focus is selected when tabindex is the same..

Of course, the tabindex values of all controls are not equal by default. When you drag the first control to the window, the value of tabindex is 0. Then, each time you add a control, tabindex is increased by 1 by default. you can change the tabindex manually. so if you want to manually change it, do not change it to the same. each control must be unique.

 

Determine which control gets focus

 

For example, in scenario 2 above, if you click the F1 shortcut key, how can you determine which control is getting the focus? There are two methods:

First, review all the controls on the page (form) and then make judgments.

Foreach (control con in this. Controls)

{

If (con. Focused) // If a control obtains focus, its focused value is true.

// Do something

}

 

There is also a simpler method

Control con = This. activecontrol; // obviously, this method is simpler.

 

Of course, you may ask, although the above shows a control that gets the focus, I don't seem to know which specific control, such as a textbox or button. we know that each control has a unique name. you should know the name of each control in advance, and then use if (con. name = "txt1") to determine the control. then perform the conversion. for example, textbox TXT = (textbox) con;

Of course, if you don't want to know which control is specific, you just need to know the control type. Then textbox TXT = con as textbox; if the conversion is successful (that is, txt! = NULL), the textbox that gets the focus. If it fails, it will be transferred again. For example, button BTN = con as button;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.