combobox problem
Dear friends i want to add values in combobox in my c# application and want to maintain these values in it when i open application next time.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mayur GujrathiPosted Jan 6, 2011, 5:13 AM
Kevin AungPosted Jan 5, 2011, 4:11 PM
Here, you could set up the variables you want to store. Choose your var type or click browse if you don't see one there.
For a combo box, you could store in System.Collections.Specialized.StringCollection.
Say we set up a var called "comboValues" of System.Collections.Specialized.StringCollection type in Settings.settings
in your code (example project name "TestApp"), you could have something like:
using System;
using System.Collections.Specialized;
using TestApp.Properties:
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Settings mySettings = new Settings();
foreach (string val in mySettings.comboValues)
myComboBox.Items.Add(val);
}
private void SaveComboBoxValues()
{
Settings mySettings = new Settings();
StringCollection myValues = new StringCollection();
foreach (var cVal in myComboBox.Items)
myValues.Add(cVal.ToString());
mySettings.comboValues = myValues;
mySettings.Save();
}
}
Mayur GujrathiPosted Jan 4, 2011, 11:54 PM
Krishna GaradPosted Jan 4, 2011, 11:40 PM