繁体   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