hello everyone!!! i have a problem in keeping the value of my combobox, i have a form with a combobox, what i want is when i select from my combobox, it will retain or keep its text value even if i close the program, the combobox will only change the text value when i select another again. whats happening in my program is, when i close the form the value of the combobox in blank again. please help me...

Hello sjn21682,
Defiantly you have to serialize your form before close it. Do you do it?

Cheers

dear rogachev,

i guess i didnt, what do you mean by serialize?

Hello again
If we are talking about windows i can give you simple example:
1. Add references to your form:

using System.Xml;
using System.Xml.Serialization;
using System.IO;

2. Create class (You can add another properties for settings)

[XmlRootAttribute("MySettingsClass")]
    public class MySettingsClass
    {
        [XmlElementAttribute("ComboboxSelectedItem")]
        public string ComboboxSelectedItem { get; set; }
    }

3. Add next code:

public partial class Form1 : Form
    {

        private string fileName = "settings.xml";

        public Form1()
        {
            InitializeComponent();
            this.applyMySettings();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void applyMySettings()
        {

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MySettingsClass));
                FileStream stream = new FileStream(this.fileName, FileMode.Open);
                MySettingsClass settings = (MySettingsClass)serializer.Deserialize(stream);

                if (settings != null && !string.IsNullOrEmpty(settings.ComboboxSelectedItem))
                {
                    this.comboBox1.SelectedItem = settings.ComboboxSelectedItem;
                }

                stream.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                MySettingsClass settings = new MySettingsClass { ComboboxSelectedItem = (string)this.comboBox1.SelectedItem };
                XmlSerializer serializer = new XmlSerializer(typeof(MySettingsClass));
                StreamWriter writer = new StreamWriter(this.fileName);

                serializer.Serialize(writer, settings);
                writer.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

This is simple example. You can read more about Serialization in MSDN.

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.