none
Save CheckBox on Settings??? RRS feed

  • Question

  • Hi.


    If a user checks a checkbox on my app how can i save that option to my settings file (Config.settings) , when next time the user loads the app the checkbox is check.

    How can i do that?

    Thx in advance.

    Monday, July 2, 2007 11:43 PM

Answers

  • Hi,

    Here is a Saving Form Settings sample code,Hope it helps

    public class SettingsSaver

    {

        private static string _settingsKey = @"Software\Toub";

     

        public static string SettingsKey { get { return _settingsKey; } set { _settingsKey = value; } }

     

        public static void Register(Form f)
        {
            if (f != null)
            {

                f.Load += new EventHandler(LoadSettings);

                f.Closing += new System.ComponentModel.CancelEventHandler(SaveSettings);

            }

        }

     

        private static void LoadSettings(object sender, EventArgs e) 
        {

            Form f = sender as Form;

            if (f == null) return;

            try

            {

                using (RegistryKey parentKey = Registry.CurrentUser.OpenSubKey(_settingsKey, false))

                {

                    if (parentKey != null)

                    {

                        using (RegistryKey key = parentKey.OpenSubKey(Assembly.GetEntryAssembly().GetName().Name))

                        {

                            if (key != null) LoadSettings(key, f);

                        }

                    }

                }

            }

            catch(Exception exc)

            {

                Debug.Write(exc.ToString());

            }

        }

     

        private static void LoadSettings(RegistryKey key, Control parent)

        {

            foreach(Control c in parent.Controls)

            {

                if (c is CheckBox) ((CheckBox)c).Checked = bool.Parse((string)key.GetValue(c.Name, ((CheckBox)c).Checked.ToString()));

                else if (c is TextBox) ((TextBox)c).Text = (string)key.GetValue(c.Name, ((TextBox)c).Text);

                else if (c is RadioButton) ((RadioButton)c).Checked = bool.Parse((string)key.GetValue(c.Name, ((RadioButton)c).Checked.ToString()));

                else if (c is TrackBar) ((TrackBar)c).Value = int.Parse((string)key.GetValue(c.Name, ((TrackBar)c).Value.ToString()));

                else if (c.Controls.Count > 0) LoadSettings(key, c);

            }

        }

     

        private static void SaveSettings(object sender, System.ComponentModel.CancelEventArgs e)

        {

            Form f = sender as Form;

            if (f == null) return;

            try

            {

                using (RegistryKey parentKey = Registry.CurrentUser.CreateSubKey(_settingsKey))

                {

                    using (RegistryKey key = parentKey.CreateSubKey(Assembly.GetEntryAssembly().GetName().Name))

                    {

                        if (key != null) SaveSettings(key, f);

                    }

                }

            }

            catch(Exception exc)

            {

                Debug.Write(exc.ToString());

            }

        }

     

        private static void SaveSettings(RegistryKey key, Control parent)
        {

            foreach(Control c in parent.Controls)

            {

                if (c is CheckBox) key.SetValue(c.Name, ((CheckBox)c).Checked.ToString());

                else if (c is TextBox) key.SetValue(c.Name, ((TextBox)c).Text);

                else if (c is RadioButton) key.SetValue(c.Name, ((RadioButton)c).Checked.ToString());

                else if (c is TrackBar) key.SetValue(c.Name, ((TrackBar)c).Value.ToString());

                else if (c.Controls.Count > 0) SaveSettings(key, c);

            }

        }

    }

    For more information ,check here

    With Regards

    Wednesday, July 4, 2007 1:36 AM

All replies

  • Hi,

    Here is a Saving Form Settings sample code,Hope it helps

    public class SettingsSaver

    {

        private static string _settingsKey = @"Software\Toub";

     

        public static string SettingsKey { get { return _settingsKey; } set { _settingsKey = value; } }

     

        public static void Register(Form f)
        {
            if (f != null)
            {

                f.Load += new EventHandler(LoadSettings);

                f.Closing += new System.ComponentModel.CancelEventHandler(SaveSettings);

            }

        }

     

        private static void LoadSettings(object sender, EventArgs e) 
        {

            Form f = sender as Form;

            if (f == null) return;

            try

            {

                using (RegistryKey parentKey = Registry.CurrentUser.OpenSubKey(_settingsKey, false))

                {

                    if (parentKey != null)

                    {

                        using (RegistryKey key = parentKey.OpenSubKey(Assembly.GetEntryAssembly().GetName().Name))

                        {

                            if (key != null) LoadSettings(key, f);

                        }

                    }

                }

            }

            catch(Exception exc)

            {

                Debug.Write(exc.ToString());

            }

        }

     

        private static void LoadSettings(RegistryKey key, Control parent)

        {

            foreach(Control c in parent.Controls)

            {

                if (c is CheckBox) ((CheckBox)c).Checked = bool.Parse((string)key.GetValue(c.Name, ((CheckBox)c).Checked.ToString()));

                else if (c is TextBox) ((TextBox)c).Text = (string)key.GetValue(c.Name, ((TextBox)c).Text);

                else if (c is RadioButton) ((RadioButton)c).Checked = bool.Parse((string)key.GetValue(c.Name, ((RadioButton)c).Checked.ToString()));

                else if (c is TrackBar) ((TrackBar)c).Value = int.Parse((string)key.GetValue(c.Name, ((TrackBar)c).Value.ToString()));

                else if (c.Controls.Count > 0) LoadSettings(key, c);

            }

        }

     

        private static void SaveSettings(object sender, System.ComponentModel.CancelEventArgs e)

        {

            Form f = sender as Form;

            if (f == null) return;

            try

            {

                using (RegistryKey parentKey = Registry.CurrentUser.CreateSubKey(_settingsKey))

                {

                    using (RegistryKey key = parentKey.CreateSubKey(Assembly.GetEntryAssembly().GetName().Name))

                    {

                        if (key != null) SaveSettings(key, f);

                    }

                }

            }

            catch(Exception exc)

            {

                Debug.Write(exc.ToString());

            }

        }

     

        private static void SaveSettings(RegistryKey key, Control parent)
        {

            foreach(Control c in parent.Controls)

            {

                if (c is CheckBox) key.SetValue(c.Name, ((CheckBox)c).Checked.ToString());

                else if (c is TextBox) key.SetValue(c.Name, ((TextBox)c).Text);

                else if (c is RadioButton) key.SetValue(c.Name, ((RadioButton)c).Checked.ToString());

                else if (c is TrackBar) key.SetValue(c.Name, ((TrackBar)c).Value.ToString());

                else if (c.Controls.Count > 0) SaveSettings(key, c);

            }

        }

    }

    For more information ,check here

    With Regards

    Wednesday, July 4, 2007 1:36 AM
  • thx alot
    Wednesday, July 4, 2007 3:30 AM