c# - 将参数从一种方法传递到 button.click 的另一种方法

标签 c# winforms parameters parameter-passing

我有一个主按钮函数,我在其中解析文本框文本值,然后将这些值传递给另一个方法。 执行第一次计算后,我想将这些参数传递给另一个按钮功能。

我想从主按钮将 int valueOF1, int valueOF2 分配给此方法 public void testfunction(object sender, EventArgs ea)

我该怎么做??感谢您的帮助。

这是代码:

private void b_calculate_Click(object sender, EventArgs e)
    {

        int valueOF1;
        int.TryParse(t_Offset1.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out valueOF1);

        int valueOF2;
        int.TryParse(t_Offset2.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out valueOF2);

        int pRows = PrimaryRadGridView.Rows.Count;
        int sRows = SecondaryRadGridView.Rows.Count;

        if (pRows == 1 && sRows == 1)
        {
            calculatePS(valueOF1, valueOF2);
        }


}

private void calculatePS(int valueOF1, int valueOF2)
{
    MessageBox.Show("You are using : P-S");
    // Do some calculation & go to the next function ///
    Button2.Enabled = true;
    Button2.Click += testfunction; // Here i want to pass the valueOF1 & valueOF2

}

public void testfunction(object sender, EventArgs ea)
{
    MessageBox.Show("you...!");
    Button2.Enabled = false;
}

最佳答案

在您的函数 calculatePS 中,按如下方式更改最后一行

Button2.Click += new EventHandler(delegate 
{ 
  // within this delegate you can use your value0F1 and value0F2
  MessageBox.Show("you...!");
  Button2.Enabled = false;
});

关于c# - 将参数从一种方法传递到 button.click 的另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440646/

相关文章:

c# - 多线程时是否必须锁定数据库连接?

c# - 如何使用 C# 禁用 Windows 文本框上的右键单击上下文菜单?

c# - Image.Dispose() 上的 ArgumentException

parameters - Rust 标准库闭包参数 : run time or compile time?

c# - Exception.ToString() 抛出异常

c# - 空 ItemSource 上的 Linq ToList

c# - 签署 winforms exe 时出现加密失败 - "The Parameter is incorrect"

c# - 无法创建 Excel.Application 的对象

javascript - 在 JavaScript 中哪里执行参数验证?

c# - 在 ASPX 页面上获取 'too many parameters passed' 到存储过程