我有一个主按钮功能,我解析文本框文本值,然后将这些值传递给另一个方法。 执行第一次计算后,我想将这些参数传递给另一个按钮功能。
我想从主按钮为此方法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;
}
答案 0 :(得分:4)
在你的函数中,calculatePS改变最后一行如下
Button2.Click += new EventHandler(delegate
{
// within this delegate you can use your value0F1 and value0F2
MessageBox.Show("you...!");
Button2.Enabled = false;
});
答案 1 :(得分: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
,您可能希望以某种方式处理解析错误,而不是继续正常流程。