3

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

我想 从主按钮分配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;
}
4

2 回答 2

4

在您的函数 calculatePS 中更改最后一行,如下所示

Button2.Click += new EventHandler(delegate 
{ 
  // within this delegate you can use your value0F1 and value0F2
  MessageBox.Show("you...!");
  Button2.Enabled = false;
});
于 2012-11-18T13:43:15.960 回答
2

您可以将valueOF1和声明valueOF2为类字段,因此您可以从不同的方法访问它们。

代码如下所示:

int valueOF1 = 0;
int valueOF2 = 0;

private void b_calculate_Click(object sender, EventArgs e)
{
    int.TryParse(t_Offset1.Text, NumberStyles.Any,
                 CultureInfo.InvariantCulture.NumberFormat, out valueOF1);

    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();
    }
}

private void calculatePS()
{
    // ** you can use valueOF1 and valueOF2 here **

    MessageBox.Show("You are using : P-S");
    // Do some calculation & go to the next function ///
    Button2.Enabled = true;

    //probably no need to register the Button2.Click event handler
    //except when the form is created
    //
    //Button2.Click += testfunction; 
}

public void testfunction(object sender, EventArgs ea)
{
    // ** you can use valueOF1 and valueOF2 here as well **

    MessageBox.Show("you...!");
    Button2.Enabled = false;
}

附加说明: int.TryParse返回一个bool告诉您解析是否成功的值。如果返回是false,您可能希望以某种方式处理解析错误,而不是继续正常流程。

于 2012-11-18T13:42:32.563 回答