Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据您在form1中单击的按钮在form2中显示数据_C# - Fatal编程技术网

C# 根据您在form1中单击的按钮在form2中显示数据

C# 根据您在form1中单击的按钮在form2中显示数据,c#,C#,您知道如何根据您在form1中单击的按钮在form2中显示数据吗?例如,表单1中有三个按钮,您必须单击三个按钮中的一个。单击按钮后,将出现一个form2,其中包含有关在form1中单击的按钮的信息 我还是用了VisualStudio。谢谢您可以在Form1中创建字符串变量,并在Form1上按钮的事件处理程序中设置字符串变量,其中包含要发送到Form2的所需信息 然后,每当您想要显示Form2时,就将该字符串变量发送给Form2的构造函数。当然,您必须在Form2中添加一个带有字符串参数的构造函数

您知道如何根据您在form1中单击的按钮在form2中显示数据吗?例如,表单1中有三个按钮,您必须单击三个按钮中的一个。单击按钮后,将出现一个form2,其中包含有关在form1中单击的按钮的信息


我还是用了VisualStudio。谢谢

您可以在Form1中创建字符串变量,并在Form1上按钮的事件处理程序中设置字符串变量,其中包含要发送到Form2的所需信息

然后,每当您想要显示Form2时,就将该字符串变量发送给Form2的构造函数。当然,您必须在Form2中添加一个带有字符串参数的构造函数

您的代码应该如下所示:

string info;

private void Button1_Click(object sender, EventArgs e)
{
    info = "Information you need to send";
}

private void ShowForm_Click(object sender, EventArgs e)
{
    Form2 form = new Form2(info);
    form.Show();
}
public partial class Form2 : Form
{
    private string info;

    public Form2(string s)
    {
         // "s" is the string sent from form1 containing the needed information
         info = s;
    }

    // Use the string info to show in any event handler in Form2 let's say the Form_Load
    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show(info, "Data from Form1");
    }
}
您的Form2应该如下所示:

string info;

private void Button1_Click(object sender, EventArgs e)
{
    info = "Information you need to send";
}

private void ShowForm_Click(object sender, EventArgs e)
{
    Form2 form = new Form2(info);
    form.Show();
}
public partial class Form2 : Form
{
    private string info;

    public Form2(string s)
    {
         // "s" is the string sent from form1 containing the needed information
         info = s;
    }

    // Use the string info to show in any event handler in Form2 let's say the Form_Load
    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show(info, "Data from Form1");
    }
}

您必须定义两个表单,在第一个按钮事件中,调用第二个表单的生成器并传递有关按钮的信息

private void button1_Click(object sender, EventArgs e)  
{ 
    string data1= "data button 1";
    List<int>  data2 = new list<int>();
    data2.add(1);
    data2.add(3);
    Form2 frm2 = new Form2(data1,data2);  
    frm2.Show();  
}  

定义第二个构造函数并通过它传递所需的参数。