如果用户单击表单上的其他位置,如何取消选择文本框?

发布于 2024-12-01 12:19:06 字数 148 浏览 3 评论 0原文

目前在我的应用程序中不可能取消选择文本框。唯一的方法是选择另一个文本框。我和我的用户都同意,单击表单上的其他任何位置都应取消选择当前文本框。我尝试覆盖许多控件上的 MouseDown 并将焦点设置为随机标签,但它不适用于某些控件(例如 MenuStrip 或滚动条)。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏有森光若流苏 2024-12-08 12:19:06

假设您的论坛上没有其他控件,请尝试添加可以接收焦点的面板控件。

Panel 控件上的 TabIndex 设置为小于 TextBoxNumericUpDown 控件的值。

现在,当您的主窗体接收焦点时,Panel 应该接收焦点,而不是 TextBox 区域。

ScreenShot

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.

ScreenShot

嗼ふ静 2024-12-08 12:19:06

我最近有类似的问题。我的界面非常复杂,有很多面板和标签页,所以我找到的更简单的答案都不起作用。

我的解决方案是以编程方式向表单中的每个不可聚焦控件添加鼠标单击处理程序,该处理程序将尝试将任何标签聚焦在表单上。在不同的标签页上时,聚焦特定标签是行不通的,所以我最终循环遍历并聚焦所有标签。

完成的代码如下:

    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);
        }
    }

然后将其添加到您的 Form_Load 事件中:

HookControl(this);

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);
听,心雨的声音 2024-12-08 12:19:06

由于您的 winform 上可能有标签或任何其他控件,因此我会采用解决方案 此处推荐,并在单击表单时将焦点置于标签上。

最坏的情况,您甚至可以在 -100、-100 位置添加一个标签,将其设置为选项卡顺序中的第一个标签,然后在表单单击时将其 Focus() 。

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.

暖伴 2024-12-08 12:19:06

我有一些“解决方法”给你。只是后台的另一个控件(可以获得焦点)。我针对 GridView 对此进行了测试(它将把您的控件绘制为灰色) - 但您应该能够使用您想要的颜色的自定义控件来完成此操作,或者仅设置 gridview 的背景颜色(doh)。
这样,每次用户单击背景时,该背景控件都会获得焦点。

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.

谁许谁一生繁华 2024-12-08 12:19:06

这是通用答案:要在用户单击表单上的其他任何位置时取消选择文本框,首先使控件失去焦点。为此,订阅表单的 Click 事件:

private void Form1_Click(object sender, EventArgs e)
{
    this.ActiveControl = null;
}

接下来订阅您的 TextBoxes 焦点离开事件并将 SelectionLength 设置为 0 (要取消选择文本,虽然文本框在失去焦点时不显示选择,但不知何故它不会被取消选择):

private void textBoxes_Leave(object sender, EventArgs e)
{
    TextBox txbox = sender as TextBox;
    txbox.SelectionLength = 0;
}

如果您嵌套了 TexBox在自定义用户控件中,您必须以类似的方式在该用户控件中添加事件。希望对其他人有帮助。

This is generic answer: To deselect TextBoxes when user clicks anywhere else on the form, first make controls to lose focus. For this subscribe to Click Event of the form:

private void Form1_Click(object sender, EventArgs e)
{
    this.ActiveControl = null;
}

Next subscribe your TextBoxes to Focus Leave event and set SelectionLength to 0 (to deselect text, somehow it is not deselected although textbox does not show selection when loses focus):

private void textBoxes_Leave(object sender, EventArgs e)
{
    TextBox txbox = sender as TextBox;
    txbox.SelectionLength = 0;
}

If you have your TexBoxes nested in custom user control, you have to add events within that user control in a similar manner. Hope that helps to anyone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文