简体   繁体   中英

Items Count of listbox on another form

While I'm debugging, the value of ex always comes back as 0.

I can't modify or read ListBox values on different form. I have two forms here. How can I resolve this?

Form1 eski = new Form1();
Form2 yeni = new Form2();

ListBox array = new ListBox();
int ex = eski.listBox5.Items.Count;
for (int ix = 0; ix <= ex; ix++)
{
    array.Items.Add(eski.listBox5.Items[ix]);
}

The problem on your code is this one:

Form1 eski = new Form1();

You are initializing a new form and this code will not call the opened Form1 which is already running on your application.

You need to replace that and do this instead:

var eski = Application.OpenForms.OfType<Form1>().SingleOrDefault();

With that, you can now access the controls that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM