如何在c#中将数据从一个表单传递给另一个表单 [英] How to pass data from one form to other in c#

查看:58
本文介绍了如何在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;
   }


这篇关于如何在c#中将数据从一个表单传递给另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆