如何在c#中将数据从一个表单传递给另一个表单

如何在c#中将数据从一个表单传递给另一个表单

问题描述:

大家好



i想知道如何将Form2中的列表框项目传递到Form2中的列表框。



假设我在form1中有两个列表框,另一个在form2中。还有一个名为button1的按钮,它调用Form2。



Hi all

i wanna know how to pass listbox items from Form1 to listbox in Form2.

suppose i have two listbox one in form1 and other in form2.And a button named button1 that calls Form2.

private void button1_Click(object sender, EventArgs e)
         {
             Form2 f = new Form2();
             f.Show();
         }







现在在这个实例中我希望传递form1的列表框项目form2中的列表框。



谢谢




Now at this instance i want listbox items of form1 to be passed in listbox in form2.

Thank you





看看这个提示:

在两种表格之间传递信息,第1部分:父母对儿童 [ ^ ]



在这种情况下,你可以试试这个:



items 参数添加到Form2的构造函数中并复制列表框项:

Hi,

Have a look at this tip:
Transferring information between two forms, Part 1: Parent to Child[^]

In this case, you can try this:

Add a items parameter to the constructor of Form2 and copy the listbox items:
public Form2(object[] items)
{
    InitializeComponent();
    listboxOnForm2.Items.AddRange(items);
}



并将项目从Form1传递到Form2:


And to pass the items from Form1 to Form2:

object[] itemsToPass = new object[yourListBox.Items.Count];
yourListBox.Items.CopyTo(itemsToPass, 0);
Form2 f = new Form2(itemsToPass);
f.Show();


您好,



查看此链接!

在表单之间传递数据 [ ^ ]


private void button1_Click(object sender, EventArgs e)
         {
             Form2 f = new Form2("Pass your value as string");
             f.Show();
         }














and

public Form2(string strValue)
   {
   InitializeComponent();
   string GetData = strValue;
   }