【发布时间】:2017-11-27 18:05:08
【问题描述】:
作为标题,我知道很多人都问过这种问题。我已经阅读了很多,我成功地将列表框中的项目传递给其他表单,但是如果列表框添加了许多新项目,我不知道如何。 列表框仅在我重新打开表单时更新。这是我的代码。我打开新表单的方式是修改 Program.cs 因为列表框是非静态的
.....
static class Program
{
private static Lm f1; //This is my Main form
private static Form1 f2; // This is the second
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
f1 = new Lm();
f1.VisibleChanged += OnLmChanged;
Application.Run(f1);
}
static void OnLmChanged(object sender, EventArgs args)
{
if (!f1.Visible)
{
f2 = new Form1(f1.listBox2, f1.listBox3, f1.label7);
f2.Show();
f2.FormClosing += OnFormChanged;
}
}
static void OnFormChanged(object sender, EventArgs args)
{
f1.Show();
}
我在第二个表单(Form1)上的代码
public partial class Form1 : Form
{
public Form1(ListBox listBox, ListBox list1, Label ll)
{
InitializeComponent();
listBox2.Items.AddRange(listBox.Items);
//listBox.SelectedIndexChanged += (s, args) => //This didn't work
//{
//listBox2.Refresh();
//listBox2.Items.AddRange(listBox.Items);
//};
}
我意识到我不应该将“listBox2.Items.AddRange”放在公共 Form1(..) 中,但我不知道如何.. :( (listBox2 正在更新 Lm 表单,我正在尝试将其传递给 Form1 表单)
【问题讨论】:
-
您的列表框充满了数据集合,传递集合/列表而不是列表框本身