【发布时间】:2014-09-10 19:10:44
【问题描述】:
我有一个自定义的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);
}
}
【问题讨论】:
-
确保您没有在父控件或表单中抑制 keydown 事件。 IE。
e.SuppressKey = false;