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
C# 我想从循环中的另一个表单打开一个表单并点击该表单上的按钮。我试过下面的代码_C# - Fatal编程技术网

C# 我想从循环中的另一个表单打开一个表单并点击该表单上的按钮。我试过下面的代码

C# 我想从循环中的另一个表单打开一个表单并点击该表单上的按钮。我试过下面的代码,c#,C#,第一种形式的代码: int i=0; while(i < 5) { this.hide(); form2 obj_form2 = new form2(); obj_form2.show(); i++; } inti=0; 而(i

第一种形式的代码:

int i=0;
while(i < 5)
{
    this.hide();
    form2 obj_form2 = new form2();
    obj_form2.show();
    i++;
} 
inti=0;
而(i<5)
{
this.hide();
form2 obju form2=新form2();
obj_form2.show();
i++;
} 

我必须在form2中的文本框中输入一些值,然后单击按钮,我想将该文本框的值从form2传递给form1五次。虽然上面的代码我一次获得了所有五个窗口,但我希望它们在点击form2的按钮时一个接一个地显示。是的,您可以删除while循环并保留一个类变量来检测您按下按钮的次数:

private pressimes = 0;

private void btnOpenForm_Click(object sender, EventArgs e)
{
   this.hide();
   form2 obj_form2=new form2();
   obj_form2.PassedValue = pressTimes;
   obj_form2.show();
   pressTimes++;

}
在表格2中,添加:

public int PassedValue  { set; get; }

并在表单中使用它作为传递值。或者您可以更改表单2构造函数并使用以下命令传递值:
form2 obj_form2=new form2(按次)
这是要放入表单1的代码:

this.hide();
List<string> results = new List<string>(); // List of all the returned results

for (int i = 0; i < 5; i++)
{
    form2 obj_form2 = new form2();
    obj_form2.ShowDialog();

    results.Add(obj_form2.textBox.Text);
}

this.Show();
MessageBox.Show(string.Join("\n", results)); // Show all the results
并将前面提到的代码更改为

this.hide();
List<string> results = new List<string>(); // List of all the returned results

for (int i = 0; i < 5; i++)
    results.Add(form2.GetInput());

this.Show();
MessageBox.Show(string.Join("\n", results)); // Show all the results
this.hide();
列表结果=新列表();//所有返回结果的列表
对于(int i=0;i<5;i++)
results.Add(form2.GetInput());
this.Show();
MessageBox.Show(string.Join(“\n”,results));//显示所有结果

仍然要更改前面提到的
修饰符
属性

请格式化您的代码。
this.hide();
List<string> results = new List<string>(); // List of all the returned results

for (int i = 0; i < 5; i++)
    results.Add(form2.GetInput());

this.Show();
MessageBox.Show(string.Join("\n", results)); // Show all the results