Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# combobox
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 .
 
        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 6-Dec-12 8:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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:
Properties.Settings.Default.MySettingString = setting;
Properties.Settings.Default.Save();
  Permalink  
Comments
Member 9522119 at 6-Dec-12 14:55pm
   
"You can now use the array to load your combobox."
 
How ? :)
OriginalGriff at 7-Dec-12 3:39am
   
Ooo! That's a hard one! :laugh:
How about:
myComboBox.AddRange(comboRows);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Comments
Adam R Harris at 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 at 6-Dec-12 15:19pm
   
:)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 643
1 OriginalGriff 550
2 CPallini 415
3 George Jonsson 231
4 ChauhanAjay 150
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,032
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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