调用列表<;双倍>;内部按钮1点击事件进入按钮2点击事件c#
我的C#windows窗体应用程序中有两个按钮。按钮1和按钮2。 我想使用Button1事件中计算的变量和列表作为Button2事件中的输入变量。我该怎么做?例如:调用列表<;双倍>;内部按钮1点击事件进入按钮2点击事件c#,c#,buttonclick,C#,Buttonclick,我的C#windows窗体应用程序中有两个按钮。按钮1和按钮2。 我想使用Button1事件中计算的变量和列表作为Button2事件中的输入变量。我该怎么做?例如: private void button1_Click(object sender, EventArgs e) { int a; // some steps // after these steps, assume a gets the value of 5 so a = 5 at this point. //
private void button1_Click(object sender, EventArgs e)
{
int a;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
// also there is a list which gets its values after these steps
List<double> parameterValues = new List<double> {
i.GetDouble(), S.GetDouble(), L.GetDouble(),B.GetDouble()
};
}
为了在两个按钮中使用int,必须将int设置为全局
public int a;
private void button1_Click(object sender, EventArgs e)
{
a = 5;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
}
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
}
您当前有一个范围问题。要在两种方法中使用,您要在button click 2内部使用的值必须至少是forms类的模块化值。在本例中,“outerValue”是模块化的,两者都可以访问。通读这篇文章,更好地了解变量范围
将
a
设为全局变量(public int a
),并在按钮1\u Click()
之外实例化它。到目前为止,您做了哪些尝试?可能重复感谢,您也可以为双倍列表提供一些建议。谢谢。它适用于int数据类型。我还有一个在button1中的所有步骤之后生成的元素:List parameterValues=new List{I.GetDouble(),S.GetDouble(),L.GetDouble(),B.GetDouble(),TC.GetDouble(),TTc,TTj.GetDouble(),TCBa.GetDouble(),TCBb,TCBc.GetDouble(),TCBd.GetDouble() }; 我不知道如何处理列表,因为如果我全局声明列表(listname),我无法以我在代码包中添加的方式添加值。谢谢,请您也为列表提供一些建议。要在构造函数初始化之外向列表添加项,您需要使用list类中存在的方法。首先看看Add方法……它是最常见的方法,但是如果您想在特定位置插入范围或项目,也存在其他方法。例如,parameterValues.Add(i.GetDouble());另外,作为旁注,global这个词已经在这篇文章中使用了好几次,而且几乎在所有情况下都被错误地使用。全局变量可以在表单类的范围之外访问。模块化变量的作用域仅限于类。99.99999%的情况下,您希望避免使用全局变量。谢谢,请您也为列表提供一些建议。
public int a;
private void button1_Click(object sender, EventArgs e)
{
a = 5;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
}
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
}
public partial class Form1 : Form
{
private int outerValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = 5;
outerValue = a + 5;
}
private void button2_Click(object sender, EventArgs e)
{
int b = outerValue + 5;
}
}