用c#更新或刷新控件



我做了一个winforms项目,它允许我使用SQLite搜索和添加到SQL数据库中。一个窗体具有包含与疾病相关的症状或物种的树视图和复选框列表。这些控件以编程方式用它们自己的公共非静态函数填充。另一个表单允许我编辑数据库,特别是向TreeView或症状列表添加节点。编辑完数据库后,它激活更新上述控件的函数。我希望它能立即更新控件,但它没有。
我尝试过的一些解决方案:
this.Refresh();
this.Invalidate();
this.BeginUpdate();
this.EndUpdate();
我分别做过或一次全部做过;我试了试"这个","表格","表格"。和";treeView1 !";但是没有一个成功地更新表单或控件。

然后我想'也许从Form2更新Form1上的控件不同于在Form1上更新自己',所以我添加了Form1. treeview1。节点,以确保它集中在正确的控件上。这仍然没有显示新添加的节点或checkBoxList项。

中Form1:

using System.Data.SQLite;
// ///
public void UpdateCheckedBoxList(SQLiteConnection conn) {
SQLiteDataReader sqlite_datareader;
SQLiteCommand sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = "SELECT * FROM Species; ";
sqlite_datareader = sqlite_cmd.ExecuteReader();
List<string> myreader = new List<string> { };
while (sqlite_datareader.Read()) {
myreader.Add(sqlite_datareader.GetString(1));
}
string[] Species = myreader.ToArray();
checkedListBox1.Items.AddRange(Species);
this.Refresh();
}

Form2:

SQLiteCommand sqlite_conn = new SQLiteConnection("Data Source=" +
"C:\Users\" + Environment.UserName + "\Documents\ddx project\ddx-database.db; Version = 3; New = True; Compress = True;");
sqlite_conn.Open();
new Form1().UpdateCheckedBoxList(sqlite_conn);

感谢dr. null的评论,我设法更新了控件。他们提到我使用new Form1();,创建一个新的Form1,而不是试图更新打开的Form1。这是一个解决方案:

Application.OpenForms.OfType<Form1>().First().UpdateComboBox(sqlite_conn);
Application.OpenForms.OfType<FormName>().First().formSpecificFunction(args);