Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a program, where I need to add a new item to a combobox sometimes. I did this with this simple code but the problem is, everytime I restart the program, its not there anymore.How can I add somethings so it will be there the next times I open the program too? without a database .

C#
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    Random r = new Random();
    int aa=r.Next(comboBox1.Items.Count);
    comboBox1.Text = comboBox1.Items[aa].ToString();
}

private void button2_Click(object sender, EventArgs e)
{
    if (!(textBox1.Text==""))
    {
        comboBox1.Items.Add(textBox1.Text);

    }
    if (textBox1.Text == "")
    {
        MessageBox.Show("gsdgsdgs");
    }
}
Posted

When you start your program, the combo box is filled with the items you specify in the designer. If you want to have other items (say that are saved on program exit and loaded again on start up) then you have to save them somewhere and restore them yourself in the code.

The easiest way to do that may be to put them into the Settings file:
1) Open your project in Visual Studio, and look at the Solution Explorer pane.
2) Open the Properties dropdown, and double click the Settings.Settings branch.
3) In the page that results, add your new item: Give it a sensible name, set the "Type" to "String", and leave the "Scope" at "User". Set the Value to "Option1|Option2"
4) Close the page.
5) In your Load event, read the values:
C#
string setting = Properties.Settings.Default.MySettingString;
string[] comboRows = setting.Split('|');
You can now use the array to load your combobox.
6) You can save the current values by building a single string from them (separated by '|' characters) and then:
C#
Properties.Settings.Default.MySettingString = setting;
Properties.Settings.Default.Save();
 
Share this answer
 
Comments
Member 9522119 6-Dec-12 14:55pm    
"You can now use the array to load your combobox."

How ? :)
OriginalGriff 7-Dec-12 3:39am    
Ooo! That's a hard one! :laugh:
How about:
myComboBox.AddRange(comboRows);
Jonathan Sumilang 14-Oct-18 10:09am    
I am actually trying this on my windows form now, but I got confused to on the number 5 part, I applied the same instructions as you did but should I still put the values I wanted to store in the Split(|)?
Depends on your definition of "database".

You have to use some sort of persistent storage to store a flag or list of items to add to your combo box. When the application starts up it needs to read from the persistent storage and populate the combo box. When a new item is added the persistent storage needs to be updated.

The "persistent storage" can be anything that persists when the application closes (and the pc shuts down).

You could send an email to stone mason who chisels the data into a block of granite when you send him a write a message and who types it back in to an email in response to a read message -- but the latency and expense of that makes it questionable.

The most typical choices for this are:

(a) a file that you maintain in the application data directory or program directory. It could be plain text or xml or binary depending on how you wan to store and retrieve the data, but you do it yourself.

(b) a registry setting

(c) a traditional database (overkill for this application).
 
Share this answer
 
Comments
Adam R Harris 6-Dec-12 14:31pm    
'You could send an email to stone mason who chisels the data into a block of granite when you send him a write a message and who types it back in to an email in response to a read message -- but the latency and expense of that makes it questionable.'

That seriously made me LOL and now everyone in the office is looking at me funny. Well played good sir, you win the internets today.
TRK3 6-Dec-12 15:19pm    
:)

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