I've made a winforms project that lets me search and add into an SQL database using SQLite.
One form has a TreeView and CheckedBoxList that contain the symptoms or species related to an illness. These controls are filled programmatically with their own public non-static function.
Another form lets me edit the database, specifically adding a node to the TreeView or list of symptoms. After editing the database, it activates the function to update the mentioned controls.
I hoped it would immediately update the control, but it doesn't.
A few solutions I've tried:
this.Refresh();
this.Invalidate();
this.BeginUpdate();
and
this.EndUpdate();
I've done them separately or all at once; tried it for "this.", "Form1." and "treeView1."; but none managed to update the form or control.
Then I thought 'maybe updating the control on Form1 from Form2 is different from updating itself on Form1', so I added Form1.treeView1.Nodes to make sure it focussed on the correct control. This still did not show the newly added node or checkBoxList Item.
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);
Thanks to dr. null's comment, I managed to update the controls. They mentioned my use of new Form1();
, creating a new Form1 instead of trying to update the opened Form1. This is a solution:
Application.OpenForms.OfType<Form1>().First().UpdateComboBox(sqlite_conn);
Application.OpenForms.OfType<FormName>().First().formSpecificFunction(args);