Click here to Skip to main content
14,637,429 members
Rate this:
Please Sign up or sign in to vote.
I have a listbox in which i am adding items from the textbox by clicking a button. What i want is even when i close the form every time when i open the form the list should be displayed which is present or saved earlier in the listbox.

I am unable to display the list in the listbox when the form is closed.

What I have tried:

ADD Btton :
private void Add_PhoneNumber_Click(object sender, RoutedEventArgs e)
               string Country_Code = "+91";
               ListBox_PhnNumbr.Items.Add(Country_Code + Phone);
               MessageBox.Show(String.Format("Phone Number : {0}{1} is Added to List", Country_Code, Phone));

SAVE Button :
<pre>private SOTA_whitelist_Number RetrieveSOTAConfiguration()
              string Number1 = string.Empty;
              string Number2 = string.Empty; 
              string Number3 = string.Empty; 
              string Number4 = string.Empty; 
              string Number5 = string.Empty; 

              if (ListBox_PhnNumbr.Items.Count > 0) Number1 = ListBox_PhnNumbr.Items[0].ToString();
              if (ListBox_PhnNumbr.Items.Count > 1) Number2 = ListBox_PhnNumbr.Items[1].ToString();
              if (ListBox_PhnNumbr.Items.Count > 2) Number3 = ListBox_PhnNumbr.Items[2].ToString();
              if (ListBox_PhnNumbr.Items.Count > 3) Number4 = ListBox_PhnNumbr.Items[3].ToString();
              if (ListBox_PhnNumbr.Items.Count > 4) Number5 = ListBox_PhnNumbr.Items[4].ToString();

              MessageBox.Show("SOTA Configuration Saved Successfully"); 
              return new SOTA_whitelist_Number(Number1, Number2, Number3, Number4, Number5);
Updated 20-Apr-20 20:08pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I would have created a global DataTable and set that for the ListBox DataSource - iirc, you then add/remove from the DataTable and the ListBox is updated automagically

Google for Listbox Binding DataTable

This also means you could load your DataTable from SQL, Static Data, persist the data etc
Member 14672509 21-Apr-20 3:09am
I dont have Database to store listbox item(Listbox items will be stored in xml file), without database how to do it??
Garth J Lancaster 21-Apr-20 3:22am
You don't need a database - you can use the DataTable (or even a list of objects) as the source
Member 14672509 21-Apr-20 5:25am
can you please give me a sample code for that
Garth J Lancaster 21-Apr-20 9:22am
no, but I'll give you a useful link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100