[英]Clearing ListBox from another Form
我一直堅持這個,不知道我在這里做錯了什么。 我正在嘗試通過按鈕從另一個Form
清除ListBox
。
在我有ListBox
主Form
,我有這個功能:
public void test()
{
this.DeviceList.Items.Clear();
}
在我有我的按鈕的另一個Form
,我有:
Form1 mainform = new Form1();
mainform.test();
但是當我按下按鈕時,什么也沒有發生。 現在,如果我切換出this.DeviceList.Items.Clear();
到MessageBox.Show("test");
這工作得很好。 但不是如果我使用this.DeviceList.Items.Clear();
.
我嘗試在沒有this
情況下使用,但仍然是同樣的問題。
在您當前的代碼中:
Form1 mainform = new Form1();
mainform.test();
您創建了一個新表單,不要Show
它,而是清除它的DeviceList
。 您應該找出現有的表格,例如:
using System.Linq;
...
var mainform = Application
.OpenForms
.OfType<Form1>() //TODO: put the right type if required
.LastOrDefault(); // if you have several intances, let's take the last one
if (mainform != null) // if MainForm instance has been found...
mainform .test(); // ... we clear its DeviceList
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.