我为自己的边界属性定制了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事件。即。 'e.SuppressKey = false;' – Shell 2014-09-11 05:20:38