如何将form1中listbox1中的项目传递给form2中的listbox2#c [英] How to pass items from listbox1 in form1 to listbox2 in form2 c#

查看:84
本文介绍了如何将form1中listbox1中的项目传递给form2中的listbox2#c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在form1中,我有多个TextBox和两个按钮,用于显示输入了listbox1中每个TextBox的输入和一个移动到form2的输入。 

在form2中我有一个按钮和一个名为的列表框listbox2。

我想知道如何将form1中listbox1中的项目传递给form2中的listbox2,这样当我点击按钮时在form2中,listbox1中的项目显示。 


In form1 I have more than one TextBox and two buttons one that displays the entered inputs of each TextBox in listbox1 and one that moves to form2. 
In form2 I have one button and one listbox that is called listbox2.
I want to know how to pass the items from listbox1 in form1 to listbox2 in form2 so that when I click the button that is in form2 the items from listbox1 shows up. 

推荐答案

快速/ dirty方法是为L选择属性窗口Form1上的istBox,将Modifier属性设置为Public。

A quick/dirty method is to select the property window for the ListBox on Form1, set the Modifier property to Public.

显示Form2

var f = new Form2();
f.Show(this);

现在在Form2中,因为您按照上面的设置修改了属性属性所有者属性Form1的形式,它提供对Form1上的ListBox的访问。

Now in Form2, since you set the modifier property as per above cast the Owner property of the form to Form1, this provides access to the ListBox on Form1.

private void button1_Click(object sender, EventArgs e)
{
    var owner = (Form1) Owner;
    listBox1.Items.AddRange(owner.listBox1.Items);            
}


这篇关于如何将form1中listbox1中的项目传递给form2中的listbox2#c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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