Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnok_Click(object sender, EventArgs e)
{
    string name = tbxname.Text;
    Form2 ff = new Form2();
    ff.comboBox1.Items.Add(name);
    ff.ShowDialog();
}


The above given code is present in form1. btnok.
I want to add some item to form2.combobox1 through this code(combobox in form2 is declared as public).it updated in the combobox items.But when we close the solution and again run it,again there is only one item,the old item is gone.how do i achieve it.
Your help is needed.
Posted
Updated 16-Jan-20 5:33am
v6

As in previously stated answers, dynamically inserted elements will disappear if the program restarts. This is simply because they are not recorded to a physical drive and will be removed as soon as the garbage collector realizes their work is done.
Simplest way to avoid this is to write contents of a combobox to a text file (if you do not want to meddle with databases) and save that file (as comboboxName.txt, for example). The second time program opens and shows form2, first add the items in the text document to the combobox.
/*write contents of a combobox to a file in a directory. the file location can be a predefined one or you may prompt the user for location by savefiledialog */
//this one saves to MyDocuments
//use on OnClosing event of the form - first delete existing file by system.IO [File.Delete - not in this code]
StringBuilder sb = new StringBuilder();
foreach(var item in ComboboxName.Items)
{
    sb.AppendLine(item.ToString());
}
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using(StreamWriter _sw = new StreamWriter(folderPath+@"\ComboboxName.txt"))
{
    _sw.Write(sb.ToString());
}

//this one reads it from my documents and fills the combobox
//use on loading event of the form
string filePath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\ComboboxName.txt";
using (StreamReader sr = new StreamReader(filePath)
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    ComboboxName.Items.Add(line);
                }
            }


One point is, if you try to save the file into sensitive folders, your program might need to be executed as elevated (i.e. run as administrator)
Of course these codes are almost pseudocodes. For better performance compare existing lines in the text file and combobox and write/delete only if necessary.
 
Share this answer
 
Comments
Dalek Dave 2-Dec-10 17:13pm    
Good Answer.
If I understand correctly, you're using a button on form1 to dynamically add an additional item to form2.combobox at runtime. However, when you "close the solution and run again" the combobox on form2 only displays a single item (that you presumably added at design time or application load)?

If you want to dynamically add an item to the second drop down and have the data persist across an application restart you'll need to store the data in a file or database, then re-load or bind the comboboxes to said file/database each time the appplication loads.

Please clarify your question...
 
Share this answer
 
Comments
John Sathish Tamilarasu 1-Dec-10 1:26am    
What you understood is exactly correct,But i dont get your solution clearly.If possible give me example with code.Thank you!
ROGDEV 1-Dec-10 1:32am    
Hello, you need to decide how you want to persist the new item you are adding to the combo box. Every time you start/stop your application those dynamically added items will be erased from memory unless you store it to disk or database. Decide if you need to store it in a database (Access, SQL Server) or on disk (XML, Dataset, flat text, CSV, etc). This is up to you and how you decide to build your application...

Here are a few links to point you in the right direction..

http://dotnet.itags.org/dotnet-c-sharp/200224/
http://www.vbdotnetforums.com/windows-forms/27422-load-combo-box-text-file.html

http://stackoverflow.com/questions/376622/c-how-do-you-save-a-list-of-items-like-a-combobox-to-the-net-settings-file

http://msdn.itags.org/visual-csharp/52804/

http://bytes.com/topic/c-sharp/answers/252379-combo-box-load-table

Good luck
In addition to John's answer, the ComboBox on Form 2 needs to have its accessor changed to public.

It might help you to get a more specific answer if you tell us what 'But its not working.' actually means. In what way is it not working?
 
Share this answer
 
You are aware that you actually have to set the selected item in the combobox before it shows up in the edit field, right?

ff.comboBox1.SelectedIndex = 0;

or

ff.comboBox1.SelectedItem = name;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900