【问题标题】:custom textbox event not fired自定义文本框事件未触发
【发布时间】: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;

标签: c# winforms events


【解决方案1】:

不,它不会被解雇。因为焦点将在TextBoxKeyDown 文本框的事件将被触发。

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

  • TextBox 继承BorderedTextBox 而不是UserControl
  • 订阅textBox.KeyDown事件并处理。

【讨论】:

  • 它不起作用,此外,边框也不起作用:(我只需要一个简单的带边框的文本框。
  • @user1797147 说它不起作用是没有用的。而是解释正在发生的事情。同时发布您现在尝试的内容。
猜你喜欢
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
相关资源
最近更新 更多