title: How to add items to a listbox from another form in C#
body: <p>I'm creating a simple enrollment system in C#, and I'm stuck where I have to define the list of venues/rooms. I tried doing all the other similar codes, but nothing seemed to work.</p>
  
  <p>Here is the scenario:</p>
  
  <p>I have a <code>btnAddRooms</code> button in a form named <code>enrollmentAdmin</code> and a <code>LBoxVenue</code> listbox which has a <code>datasource</code> of a list (from database). Whenever <code>btnAddRooms</code> is clicked, another form is displayed with a textbox in it in order to add items into the listbox (<code>btnAdd</code> to add the typed string in the textbox).</p>
  
  <p>Code of enrollmentAdmin (first form) is as follows: </p>
  
  <pre><code> public partial class enrollmentAdmin : Form
      {
          public List&lt;String&gt; listRooms = new List&lt;String&gt;();
  
   private void enrollmentAdmin_Load(object sender, EventArgs e)
          {  
  
              Connection.OpenConnection();
  
              string query1 = "select distinct Venue from tblVenueListing;";
              SqlCommand command1 = new SqlCommand(query1, Connection.conn);
  
              using (SqlDataReader reader = command1.ExecuteReader())
              {
                  while (reader.Read())
                  {
                      listVenue.Add(reader.GetString(0));
                  }
  
              }
              Connection.CloseConnection();
  
              LBoxRooms.DataSource = listRooms;
          }
      }
  </code></pre>
  
  <p>The code of btnAddRooms is as follows (form 1):</p>
  
  <pre><code> form2 form = new form2();
   form.Show();
   this.Hide();
  </code></pre>
  
  <p>While btnAdd code (under form 2):</p>
  
  <pre><code>enrollmentAdmin form1 = new enrollmentAdmin();
  
  Connection.OpenConnection();
  string insertRoom = "insert into tblVenueListing values ('" + txtVenue.Text     + "','" + txtRoom.Text + "');";
  SqlCommand insert = new SqlCommand(insertRoom, Connection.conn);
  insert.ExecuteNonQuery();
  
  form1.listRooms.add(txtVenue.Text);
  
  form1.LBoxRooms.DataSource = null;
  form1.LBoxRooms.DataSource = form1.listRooms;
  </code></pre>
  
  <p>The added item won't popup right after I add an item and I still have to reload form1 in order for the item to display.</p>
  
  <p>Any help is appreciated! Thank you!</p>
  
  <p>Edit: Found the workaround in order to display the changes immediately into the UI.</p>
  
  <p>I made the list into a BindingList, declared it as static and accessed it by the form's name instead of the object instance created on form2.</p>
  
  <p>So instead of:</p>
  
  <pre><code>form1.listRooms.add(txtVenue.Text);
  
  form1.LBoxRooms.DataSource = null;
  form1.LBoxRooms.DataSource = form1.listRooms;
  </code></pre>
  
  <p>I made it into:</p>
  
  <pre><code>enrollmentAdmin.listRooms.add(txtVenue.Text);
  
  form1.LBoxRooms.DataSource = null;
  form1.LBoxRooms.DataSource = enrollmentAdmin.listRooms;
  </code></pre>