簡體   English   中英

從另一個表單清除列表框

[英]Clearing ListBox from another Form

我一直堅持這個,不知道我在這里做錯了什么。 我正在嘗試通過按鈕從另一個Form清除ListBox

在我有ListBoxForm ,我有這個功能:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM