TextBoxOnFocus的来源

时间:2013-03-04 02:42:56

标签: c# winforms focus

使用C#WinForms,我正在使用一些额外的功能扩展TextBox。除此之外,我在文本框获得焦点时突出显示文本。当用户选中或单击控件时,此工作正常。但是,当整个窗口获得焦点时(例如alt + tabbing from other app to mine),也会调用OnFocus,这是不可取的。

我有什么想法可以检查onfocus()是否被调用,因为整个表格已经获得了焦点?

谢谢,

麦克

我当前的OnFocus()看起来像:

    protected void mTextBox_OnFocus(object sender, EventArgs e)
    {
        if (MouseButtons == MouseButtons.None)
        {
            mTextBox.SelectAll();
            mHasFocus = true;
        }
    }

3 个答案:

答案 0 :(得分:0)

在GotFocus事件处理程序中,尝试检查TextBox Focused属性是否为true。参见:

Control.Focused Property - MSDN

答案 1 :(得分:0)

尝试使用Control.CaptureContainsFocus属性来确定文本框是捕获鼠标还是包含焦点。

答案 2 :(得分:0)

我建议你这样做:

  public class Text : TextBox
  {
    protected override void OnEnter(EventArgs e)
    {
      base.OnEnter(e);
      //To stuff there
    }
  }

我已多次尝试控制焦点,但它非常“滑”。 此外,很难确定焦点的来源 - 或者正在进行中