Sai
Sai

Reputation: 103

How to add items to a listbox from another form in C#

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.

Here is the scenario:

I have a btnAddRooms button in a form named enrollmentAdmin and a LBoxVenue listbox which has a datasource of a list (from database). Whenever btnAddRooms is clicked, another form is displayed with a textbox in it in order to add items into the listbox (btnAdd to add the typed string in the textbox).

Code of enrollmentAdmin (first form) is as follows:

 public partial class enrollmentAdmin : Form
    {
        public List<String> listRooms = new List<String>();

 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;
        }
    }

The code of btnAddRooms is as follows (form 1):

 form2 form = new form2();
 form.Show();
 this.Hide();

While btnAdd code (under form 2):

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;

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.

Any help is appreciated! Thank you!

Edit: Found the workaround in order to display the changes immediately into the UI.

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.

So instead of:

form1.listRooms.add(txtVenue.Text);

form1.LBoxRooms.DataSource = null;
form1.LBoxRooms.DataSource = form1.listRooms;

I made it into:

enrollmentAdmin.listRooms.add(txtVenue.Text);

form1.LBoxRooms.DataSource = null;
form1.LBoxRooms.DataSource = enrollmentAdmin.listRooms;

Upvotes: 1

Views: 1884

Answers (1)

mrjoltcola
mrjoltcola

Reputation: 20842

If you expect to see data bound container changes immediately reflected to a UI element, you need to use something like BindingList or ObservableCollection or another container that implements INotifyCollectionChanged. With a standard List, the ListBox doesn't know there have been any changes.

Change your List to BindingList or ObservableCollection and try again. A List won't reflect it's changes to a UI element that is bound to it, so if you use a list, you'd have to refresh the element or rebind it. But don't do that, just use the right kind of container in the first place.

Try changing:

public List<String> listRooms = new List<String>();

to

public BindingList<String> listRooms = new BindingList<String>();

The other problem is depending on the Form.Load event.It will only fire prior to the first time the form is shown (loaded), and should really be used sort of like a constructor. Actually I use constructor more nowadays. If you want an event to fire everytime the form is activated, try Form.Activated event.

Upvotes: 1

Related Questions