如何确定哪些控制的形式具有焦点? [英] How to determine which control on form has focus?

查看:186
本文介绍了如何确定哪些控制的形式具有焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里读别处捕捉文本框输入键击,并使用它,如果按下一个按钮,我应该设置的形式,真正的关键preVIEW财产和检查的价值的KeyDown。

I've read elsewhere on here that to capture "Enter" key stroke in a text box and use it as if pushing a button I should set the KeyPreview property of the form to true and check the value of KeyDown.

我希望能够使用在其上每个都具有一个不同的按钮相关联的若干TextBox控件这种功能

I want to be able to use this functionality on several TextBox controls which each are associated with a different Button.

我的问题是我怎么知道它的控制造成了关键preSS事件? 发件人被列为窗体本身。

My question is how do I know which control caused the KeyPress event? The sender is listed as the form itself.

推荐答案

我发现这似乎是工作的解决方案。

I've found a solution which appears to be working.

    private void DeviceForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13 && tstxtDeviceFilter.Focused)
        {
            filterByDeviceSN();
        }
    }

我不禁想一定虽然更好的办法!

I can't help but think there must be a better way though!

- 编辑 - 编辑 - 编辑 - 编辑 - 编辑 -

--EDIT--EDIT--EDIT--EDIT--EDIT--

好了,看着下面的建议后,(谢谢),我在这种情况下找到了一个'好'的方式对我来说。

Well, after looking at the suggestions below (thank you) I've found a 'better' way for me in this circumstance.

    this.tstxtDeviceFilter.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tstxtDeviceFilter_KeyDown);

    private void tstxtDeviceFilter_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13)
        {
            filterByDeviceSN();
        }
    }

显然,通过捕获事件上的文本框本身而不是形式我并不需要担心的焦点。我再一次感到哑巴不考虑这事了这么久!

Obviously by trapping the event on the textbox itself rather than the form I don't need to worry about focus. Once again I feel dumb for not thinking of that for so long!

这篇关于如何确定哪些控制的形式具有焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆