本文介绍了如何在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
推荐答案
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;
}
这篇关于如何在c#中将数据从一个表单传递给另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!