【问题标题】:Why it is not Possible to call textBox1_KeyPress() event from some Button Click event.?为什么不能从某些按钮单击事件中调用 textBox1_KeyPress() 事件。?
【发布时间】:2014-07-09 19:31:49
【问题描述】:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        MessageBox.Show("called btn 0 click..");
        KeyPressEventArgs e0 = new KeyPressEventArgs('0');
        textBox1_KeyPress(sender, e0);
    }
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show("called txtbox_keypress event...");
    }    
}

如果这是一个愚蠢的问题,我很抱歉,我刚开始学习windows窗体,我仍然觉得网上的资料很混乱。我想实现计算器。因此,当按下数字按钮时,它应该填写在文本框中。所以我认为从按钮单击事件调用 textBox1_keypress() 事件会起作用???但它不起作用,

我可以在按钮单击事件中手动编写逻辑以在文本框中填充文本,但如果这样做,我也必须在 button1_KeyPress 事件中执行相同的操作。所以这将是代码的重复对吗??...所以我认为解决方案是从按钮单击事件和按钮按键事件中调用 textBox1_KeyPress() 事件...但它不起作用。那我该怎么办? ..还有其他我应该遵循的方法吗?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    所以这会是重复代码吗??

    是的,会的。所以你可以做

    private void btn0_Click(object sender, EventArgs e)
    {
        CommonMethod(e);
    }
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        CommonMethod(e);
    }
    
    private void CommonMethod(EventArgs e)
    {
        //Your logic here.
    }
    

    【讨论】:

    • 如果他想使用EventArgsKeyPressEventArgs的不同部分,那么将两者捆绑在一起是没有意义的。
    【解决方案2】:

    TextBox KeyPress 事件处理程序 (textBox1_KeyPress) 在用户按下某个键后调用KeyPressEventArgs 参数包括诸如按下了什么键之类的信息。因此,从您的 btn0_Click 方法调用它不会为 TextBox 设置文本。

    相反,您希望(可能)将用户按下的任何数字附加到已存在于 TextBox 中的文本上。类似的东西

    private void btn0_Click(object sender, EventArgs e)
    {
        textBox1.Text += "0";
    }
    

    可能更接近您想要完成的目标。

    【讨论】:

      【解决方案3】:

      您可以将逻辑放在一个额外的函数中,如下所示:

      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
      
          private void btn0_Click(object sender, EventArgs e)
          {
              NumberLogic(0),
          }
      
          private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
          {
              // I don't know right now if e contains the key pressed. If not replace by the correct argument
              NumberLogic(Convert.ToInt32(e));
          }
      
          void NumberLogic(int numberPressed){
              MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
          }    
      }
      

      【讨论】:

        【解决方案4】:

        您不想像那样将事件联系在一起。
        按键是一回事,以一种方式处理。
        按钮点击是完全不同的事情,应该这样处理。

        基本原因是这样的,
        按钮不知道它是什么数字,你需要告诉它。 另一方面,按键知道按下了什么数字。

        如果您出于某种原因真的想通过按钮could use SendKeys 以迂回的方式触发您的按键事件。

        SendKeys.SendWait("0");
        

        【讨论】:

          【解决方案5】:

          我可以建议您使用按钮的标记属性。将每个按钮在设计模式或构造函数中的值放入其中,为所有按钮创建一个按钮事件处理程序并使用标记值:

          构造函数:

                  button1.Tag = 1;
                  button2.Tag = 2;
          
                  button1.Click += buttons_Click;
                  button2.Click += buttons_Click;
          

          事件处理器:

          private void buttons_Click(object sender, EventArgs e)
              {
                  textBox1.Text = ((Button)sender).Tag.ToString();
              }
          

          【讨论】:

            猜你喜欢
            • 2018-07-06
            • 1970-01-01
            • 1970-01-01
            • 2014-09-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多