How to deselect textbox if user clicks elsewhere on the form?

烈酒焚心 提交于 2019-12-23 09:35:06

问题


Currently in my application it is impossible to deselect a textbox. The only way is to select another textbox. My users and I agree that clicking anywhere else on the form should deselect the current textbox. I tried overriding the MouseDown on many controls and having the focus set to a random label but it doesn't work for some controls like the MenuStrip or scrollbars. Any ideas?


回答1:


Assuming you have no other controls on your forum, try adding a Panel control that can receive focus.

Set the TabIndex on the Panel control to something less than your TextBox or NumericUpDown control has.

Now, when your main form receives focus, the Panel should receive the focus instead of the TextBox area.




回答2:


I had a similar issue recently. My interface is very complex with lots of panels and tab pages, so none of the simpler answers I found had worked.

My solution was to programatically add a mouse click handler to every non-focusable control in my form, which would try to focus any labels on the form. Focusing a specific label wouldn't work when on a different tab page, so I ended up looping through and focusing all labels.

Code to accomplish is as follows:

    private void HookControl(Control controlToHook)
    {
        // Add any extra "unfocusable" control types as needed
        if (controlToHook.GetType() == typeof(Panel)
            || controlToHook.GetType() == typeof(GroupBox)
            || controlToHook.GetType() == typeof(Label)
            || controlToHook.GetType() == typeof(TableLayoutPanel)
            || controlToHook.GetType() == typeof(FlowLayoutPanel)
            || controlToHook.GetType() == typeof(TabControl)
            || controlToHook.GetType() == typeof(TabPage)
            || controlToHook.GetType() == typeof(PictureBox))
        {
            controlToHook.MouseClick += AllControlsMouseClick;
        }
        foreach (Control ctl in controlToHook.Controls)
        {
            HookControl(ctl);
        }
    }
    void AllControlsMouseClick(object sender, MouseEventArgs e)
    {
        FocusLabels(this);
    }
    private void FocusLabels(Control control)
    {
        if (control.GetType() == typeof(Label))
        {
            control.Focus();
        }
        foreach (Control ctl in control.Controls)
        {
            FocusLabels(ctl);
        }
    }

And then add this to your Form_Load event:

HookControl(this);



回答3:


Since you probably have a label, or any other control on your winform, I would go with the solution recommended here and just give the focus to a label when the Form gets clicked.

Worst case, you can even add a label situated at the -100, -100 position, set him as the first in the tab order and Focus() it on form click.




回答4:


I have some kind of "workaround" for you. Just but another control (that can get the focus) in the background. I tested this for a GridView (which will paint your control grey) - but you should be able to do it with a custom control in the color you want or just set the backgroundcolor of the gridview (doh). This way everytime the user clicks the background this backgroundcontrol will get the focus.



来源:https://stackoverflow.com/questions/7208356/how-to-deselect-textbox-if-user-clicks-elsewhere-on-the-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!