【问题标题】:How can I call different methods when I press and release a button?按下并释放按钮时如何调用不同的方法?
【发布时间】:2012-09-03 21:32:39
【问题描述】:

如何在按下按钮时调用方法并在释放按钮时调用另一个方法?

我正在使用 C# WinForms 应用程序中的 PTT(按下通话)按钮。

【问题讨论】:

    标签: c# winforms button


    【解决方案1】:

    使用MouseDownMouseUp 事件

    【讨论】:

    • @MarkKram:- 哦序列混合! :)
    【解决方案2】:
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
       // button is being pressed
    }
    
    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
       // button was released
    }
    

    【讨论】:

      【解决方案3】:

      也许是这样的:

          private void btn1_MouseDown(object sender, MouseEventArgs e)
          {
              Console.WriteLine("Mouse Button was pressed down on the button");
          }
      
          private void btn1_MouseUp(object sender, MouseEventArgs e)
          {
              Console.WriteLine("Mouse button Button was released");
          }
      

      点击按钮后查看输出窗口。

      更新

      好的,试试这个,在表单中添加一个文本框并将其命名为 TextBox1,然后将此代码添加到后面的代码中:

          private void btn1_MouseDown(object sender, MouseEventArgs e)
          {
              this.TextBox1.Text = "Mouse Button was pressed down on the button";
          }
      
          private void btn1_MouseUp(object sender, MouseEventArgs e)
          {
              this.TextBox1.Text = "Mouse button Button was released";
          }
      

      【讨论】:

      • 我的按钮被称为 buttonRCP 并尝试以下操作: } private void buttonRCP_MouseUp(object sender, MouseEventArgs e) { MessageBox.Show("鼠标按钮按钮被释放"); } `
      • 你检查输出窗口了吗?
      猜你喜欢
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2013-04-01
      • 2012-12-26
      • 2011-06-14
      • 1970-01-01
      相关资源
      最近更新 更多