How to make the toolbar ToolStrip to trigger the focus control in C #

Source: Internet
Author: User
Tags win32

How to enable the toolbar ToolStrip to trigger the focus control's leave, validating, dataerror events to validate data in C #

The title, WinForm code farmers probably know that the problem is that when the input focus is still in the TextBox, DataGridViewCell, and other controls, if you click the Normal button, checkbox and other controls, then the validation will be validated, the submission will be submitted, The error will be the error, the blocked operation will be blocked. However, if you click on a toolbar item (such as ToolStripButton, the project instead of the control, you know), it will not trigger the focus control validation events, but will directly execute the button event, which is the impact of the experience. In a word, I will not be happy without solving the problem of ToolStrip. First look at the way:

<summary>///Toolbar (no right vertical bar, no handle, can trigger other control validation)///</summary> public class Toolstripex:toolstrip {readonly Button btn;//defines a control that is used to transfer focus, such as button public Toolstripex () {//Initialize and specify a control size of 0, 0.
            
        Because you don't want this button to be seen btn = New Button {Width = 0, Height = 0}; Here is the option//Let the toolbar be visually authentic.
    
        If the ToolStripButton was pressed more distinctly, otherwise only a dismal wireframe toolstripmanager.visualstylesenabled = false;
    Do not show drag handle gripstyle = Toolstripgripstyle.hidden; ///When the toolbar gets the handle and adds the control to the form, it is not done in the constructor because the form may still be null//HTTP://WWW.BIANCENG.CN/PROGRAMMING/CSHARP/201410/45478.HT m protected override void OnHandleCreated (EventArgs e) {base.
        OnHandleCreated (e); Form fm = this.
        Findform (); if (FM!= NULL) {FM. Controls.Add (BTN); }//this is added after btn.
    Location will be 0,0}////When the toolbar is encountered (in fact, other similar events are also used) to shift focus to the BTN to trigger the focus control validation//Note Although the toolbar click, but after the practice of clicking on the children will be the first trigger the event So when the focus control validation does not pass, the Click event of the subkey is not executed, which I think is implemented by the Win32 message mechanism PROtected override void OnClick (EventArgs e) {base.
        OnClick (e); Btn.
    Focus (); //Optional. Remove the 1px vertical bar at the far right of the toolbar, this flaw for me is simply unbearable, grass mud horse Microsoft, sick protected override void OnPaint (PaintEventArgs e) {e.graphics.se
        Tclip (New Rectangle (0, 0, Width-1, Height)); Base.
    OnPaint (e); }
}

The easy way to do this is to move the focus to other controls that normally have the focus when you click on the toolbar to trigger the leave/validating/dataerror of the previous control.

In fact, in order to solve this problem I quite cost a lot of twists and turns, the first thought is actually this recruit, but feel wretched point, as a pursuit of the code farmers, I think should be from the message level to solve, so the beginning of the yin strokes aside, concentrate on the news. At first I think the essence of this problem is because the toolbar is like a panel control, is not the focus of the control, unlike the button stream, can let the other control focus shifted, so there is this problem. So I would like to call the Win32 API, so that the toolbar can emit the same message as the button, so that the focus control deceived, that point to the button, so as to verify their data, transfer their focus. After many practices, really let the toolbar get the focus, let the focus control lose focus, with Spy + + Watch the focus control received the message also and click button to receive the message looks the same, but still does not trigger the verification, this is to pull the egg ~ I that depressed ah. Btw~ actually set SetStyle (Controlstyles.selectable, True) to the toolbar to achieve the same goal, but the same can not solve the problem.

Perhaps is not yet figured out the nature of the problem, perhaps is Win32 news or play not turn ~ In short, has experienced several failed attempts, I have to give up the big solution, and then back to pick up the wretched scheme, so the article in fact is the result of compromise, inevitably heart unwilling, and so on, I will try again " Orthodox "solution. If you pass by an expert, thank you!

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.