I have a combobox called combobox1 and I dont know how to save the selection. For example if i selected Sub Compounds, how would i save this so when i loaded it again from the hd Sub Compounds would be selected.

Recommended Answers

All 2 Replies

Hi!

I made this sample, which saves the combobox item when application terminates and load and select the combobox last saved item when application starts:

private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
	System.IO.StreamWriter wr = new System.IO.StreamWriter("C:\\abc.txt");
	wr.WriteLine(ComboBox1.Text);
	wr.Close();
}

private void Form1_Load(System.Object sender, System.EventArgs e)
{
	if ((System.IO.File.Exists("C:\\abc.txt") && new System.IO.FileInfo("C:\\abc.txt").Length > 0)) {
		System.IO.StreamReader sr = new System.IO.StreamReader("C:\\abc.txt");
		string item = sr.ReadLine();
		int index = ComboBox1.FindString(item);
		if ((index != -1 & !string.IsNullOrEmpty(item))) {
			ComboBox1.SelectedIndex = index;
		}
		sr.Close();
	}
}

Is this helpful ??

yes thank you very much! i appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.