将listBox传递到新表单 [英] Passing listBox to a new form

查看:65
本文介绍了将listBox传递到新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的构造函数有问题.我将其设置如下:

I have a problem with my constructor. I got it set up like this:

Form1:

private void button10_Click(object sender, EventArgs e)
{
    var form2 = new Form2(listBox1);
    form2.Show();
    this.Hide();
}  

Form2:

public Form2(ListBox listBox)
{
    InitializeComponent();
    listBox1.Items=listBox.Items;
} 

我只想将数据从Form1上的listBox发送到Form2 listBox,但这给了我这个错误:

I just want to send my data from listBox on the Form1 to my Form2 listBox but it's giving me this error:

属性或索引器'System.Windows.Forms.ListBox.Items'不能分配给它-只读.

Property or indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only.

推荐答案

您可以使用Sampath的答案,这是完全正确的.但是出于可读性和较短的代码的目的,您可以使用 ListBox.ObjectCollection.AddRange方法 :

You could use the answer from Sampath, which is completely correct. But for readability and shorter code, you can use the ListBox.ObjectCollection.AddRange Method:

public Form2(ListBox listBox)
{
    InitializeComponent();
    listBox1.Items.AddRange(listBox.Items);
}

这篇关于将listBox传递到新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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