2014-09-10 40 views
0

我为自己的边界属性定制了TextBox,但OnKeyDown事件未被触发 与原始文本框类似。自定义文本框事件未触发

public class BorderedTextBox : UserControl 
{ 
    System.Windows.Forms.TextBox textBox; 

    public BorderedTextBox() 
    { 
     textBox = new TextBox() 
     { 
      BorderStyle = BorderStyle.FixedSingle, 
      Location = new Point(-1, -1), 
      Anchor = AnchorStyles.Top | AnchorStyles.Bottom | 
        AnchorStyles.Left | AnchorStyles.Right 
     }; 

     Control container = new ContainerControl() 
     { 
      Dock = DockStyle.Fill, 
      Padding = new Padding(-1) 
     }; 
     container.Controls.Add(textBox); 
     this.Controls.Add(container); 


     Padding = new Padding(1); 
     Size = textBox.Size; 
    } 


    public override string Text 
    { 
     get { return textBox.Text; } 
     set { textBox.Text = value; } 
    } 

    public CharacterCasing CharacterCasing 
    { 
     get { return textBox.CharacterCasing; } 
     set { textBox.CharacterCasing = value; } 
    } 

    protected override void OnKeyDown(KeyEventArgs e) 
    { 
     base.OnKeyDown(e); 
    } 

    protected override void SetBoundsCore(int x, int y, 
     int width, int height, BoundsSpecified specified) 
    { 
     base.SetBoundsCore(x, y, width, textBox.PreferredHeight, specified); 
    } 
} 
+0

确保您没有禁止父控制或窗体中的keydown事件。即。 'e.SuppressKey = false;' – Shell 2014-09-11 05:20:38

回答

2

不,它不会被解雇。因为重点将在TextBoxKeyDown文本框事件将被解雇。

如果您需要处理这些事件,你有几种选择

  • 继承TextBox而不是UserControlBorderedTextBox
  • 订阅textBox.KeyDown事件并处理它。
+0

它不工作,此外,边界不工作:( 我只需要一个简单的有边框文本框 – user1797147 2014-09-10 10:39:13

+1

@ user1797147说它不工作是没有用的,而是解释发生了什么。也发布你现在尝试的东西。 – 2014-09-10 10:51:25