如何检测任何非 TextBox 控件上的 MouseDown?

发布于 2024-12-01 10:31:35 字数 373 浏览 0 评论 0原文

TextBoxes 和 NumericUpDowns 有一个奇怪的属性,即一旦选择它们就不允许取消选择它们。当我的用户选择 NumericUpDown 并单击表单上的其他位置时,应取消选择 NumericUpDown。

不幸的是,事实并非如此。目前,我只是处理表单上所有其他控件(如面板和实际表单本身)的 MouseDown 事件,并调用随机标签的 Focus 方法以从 NumericUpDown 中删除焦点。但是,这不能应用于菜单项或滚动条。

必须有更好的方法来做到这一点。用户可能想要滚动面板而不是 NumericUpDown,并直观地单击面板,然后使用滚轮,但目前这会滚动 NumericUpDown,因为它仍然具有焦点。

感谢您的阅读。

编辑:问题仍未解决。

TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.

Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.

There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.

Thanks for reading.

Edit: Problem still unsolved.

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

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

发布评论

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

评论(2

煮酒 2024-12-08 10:31:35

通常面板控件是不可聚焦控件。因此,单击面板不会从 TextBox 或 NumericUpDown Control 中移除焦点。

解决方法可以是,在面板上放置一个按钮并将其移离视图,例如设置其 x = -100 和 y = -100。不要设置visible = false。

现在,每当用户单击面板(Panel_Click 事件)时,都会将焦点(Button.Focus())设置到该按钮。这样面板就可以通过滚轮滚动。

Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.

The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.

Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.

乖乖 2024-12-08 10:31:35

将数字框放在某种面板中,然后

panel1.MouseHover += new EventHandler(panel1_MouseHover);

private void panel1_MouseHover(object sender, EventArgs e)
        {
            if (numericUpDown1.Focused)
            {
                panel1.Focus();
            }
        }

我测试它并且它有效。!

Enclose the numeric box within a panel of some sort and then do

panel1.MouseHover += new EventHandler(panel1_MouseHover);

private void panel1_MouseHover(object sender, EventArgs e)
        {
            if (numericUpDown1.Focused)
            {
                panel1.Focus();
            }
        }

I tested it and it works.!

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