Hi,
In my app I have a group of checkboxes which represent different settings. I want to be able to load and save these settings into the program, and that works just fine. However I'm having trouble updating the checkboxes to show the settings. If I load the settings, only the first checkbox is updated. However I have a defaultsettings button, and when I click it the checkboxes are updated one at a time (I need to click the button 7 times to get the checkboxes to update).
Here is my code:

//Default settings constructor inside Settings class
public Settings()
         {
             acon = true;
             addnew = true;
             defNPC = "";
             shoscr = true;
             copscr = false;
             savscr = false;
             defpath = "C:\\Program Files\\Gothic\\_Work\\Scripts";
         }

//my update code
private void button11_Click(object sender, EventArgs e)
        {
            AppSet = new Settings();
            log.wtl("Domyślne ustawienia załadowane");
            SetUI();
        }

private void SetUI()
        {
            checkBox1.Checked = AppSet.acon;
            checkBox2.Checked = AppSet.addnew;
            textBox1.Text = AppSet.defNPC;
            checkBox3.Checked = AppSet.shoscr;
            checkBox4.Checked = AppSet.copscr;
            checkBox5.Checked = AppSet.savscr;
            textBox2.Text = AppSet.defpath;
            log.wtl("Interfejs zaktualizowany");
        }

Can anyone help me with this? Do I have to redraw the form or something? BTW AppSet is the global settings object.

Recommended Answers

All 11 Replies

Nothing obvious standing out that I can see. What happens if you comment out that log.wtl("...") statement? Also, if there are other locations in the code where these Settings members or CheckBox.Checked states get modified, I would watch out for that too.

as ddoubled said, the code looks sound.
If you are still having problems you can attach your project here or post the full code so we can check if something is being changed elsewhere :)

OK, I've attached my project. Sorry the program is in Polish, but here's what the button labels mean:
Zapisz Ustawienia - Save Settings
Domyślne Ustawienia - Default Settings

Thanks

Anybody?
Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace Script_Maker
{
   
    public partial class Main : Form
    {
        Log log;
        Settings AppSet;
        public Main()
        {
            InitializeComponent();
            
        }
        public void ch8(string text)
        {
            button8.Text = text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (log.Visible == false)
            {
                log.Show();
                button8.Text = "Schowaj Log";
            }
            else
            {
                log.Hide();
                button8.Text = "Pokaż Log";
            }
        }
        private Settings LoadSettings(string path)
        {
            Settings tempset;
            try
            {
                Stream fs = File.Open(path, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                tempset = (Settings)bf.Deserialize(fs);
                fs.Close();
                log.wtl("Ustawienia załadowane z pliku " + Path.GetFileName(path));
                
            }
            catch(FileNotFoundException e) //Nie ma pliku ustawień, ustawiamy domyślne.
            {
                log.wtl("Plik ustawień nie istnieje");
                tempset = new Settings();
                log.wtl("Załadowano ustawienia domyślne");
                SaveSettings(tempset);
                

                
            }
            
            return tempset;
        }
        private Settings LoadSettings()
        {
            return LoadSettings("settings.dat");
        }
        private void SaveSettings(Settings set)
        {
            SaveSettings(set, "settings.dat");
        }
        private void SaveSettings(Settings set, string path)
        {
            Stream fs = File.Open(path, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, set);
            fs.Close();
            log.wtl("Ustawienia zapisane do pliku " + Path.GetFileName(path));
        }

        private void button10_Click(object sender, EventArgs e)
        {
            SaveSettings(AppSet);
        }

        private void UpdateSettings(object sender, EventArgs e)
        {
            AppSet.acon = checkBox1.Checked;
            AppSet.addnew = checkBox2.Checked;
            AppSet.defNPC = textBox1.Text;
            AppSet.shoscr = checkBox3.Checked;
            AppSet.copscr = checkBox4.Checked;
            AppSet.savscr = checkBox5.Checked;
            AppSet.defpath = textBox2.Text;
            log.wtl("Ustawienia zaktualizowane");
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            log.Dispose();
            Application.Exit();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            log = new Log();
            log.Owner = this;
            log.wtl("Witamy w programie TDM Script Maker");
            AppSet = LoadSettings();
            SetUI();
            
        }
        private void SetUI()
        {
            checkBox1.Checked = AppSet.acon;
            checkBox2.Checked = AppSet.addnew;
            textBox1.Text = AppSet.defNPC;
            checkBox3.Checked = AppSet.shoscr;
            checkBox4.Checked = AppSet.copscr;
            checkBox5.Checked = AppSet.savscr;
            textBox2.Text = AppSet.defpath;
            log.wtl("Interfejs zaktualizowany");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            AppSet = new Settings();
            log.wtl("Domyślne ustawienia załadowane");
            SetUI();
        }
    }
    [Serializable()]
     public class Settings : ISerializable
    {
         
         public Settings(bool ac, bool an, string dn, bool ss, bool cs, bool sss, string dp)
         {
             acon = ac;
             addnew = an;
             defNPC = dn;
             shoscr = ss;
             copscr = cs;
             savscr = sss;
             defpath = dp;
         }
         public Settings()
         {
             acon = true;
             addnew = true;
             defNPC = "";
             shoscr = true;
             copscr = false;
             savscr = false;
             defpath = "C:\\Program Files\\Gothic\\_Work\\Scripts";
         }
         public Settings(SerializationInfo info, StreamingContext ctxt)
         {
             acon = (bool)info.GetValue("ac", typeof(bool));
             addnew = (bool)info.GetValue("an", typeof(bool));
             defNPC = (string)info.GetValue("dn", typeof(string));
             shoscr = (bool)info.GetValue("ss", typeof(bool));
             copscr = (bool)info.GetValue("cs", typeof(bool));
             savscr = (bool)info.GetValue("sss", typeof(bool));
             defpath = (string)info.GetValue("dp", typeof(string));
         }
         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
         {
             info.AddValue("ac", acon);
             info.AddValue("an", addnew);
             info.AddValue("dn", defNPC);
             info.AddValue("ss", shoscr);
             info.AddValue("cs", copscr);
             info.AddValue("sss", savscr);
             info.AddValue("dp", defpath);
         }
         public bool acon;
         public bool addnew;
         public string defNPC;
         public bool shoscr;
         public bool copscr;
         public bool savscr;
         public string defpath;
    }
    public class Items : ISerializable
    {
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {

        }
    }
}

Sorry for triple post, but I think I've just realized what is causing the problem.
With all of the checkboxes, they have updatesettings set as their handler for OnCheckChange, and that changes the settings except the one changed to blank again.
Is there anyway of changing the checboxes' state without calling the event? Or is there a more elegant solution?


Thanks

OK, I've attached my project. Sorry the program is in Polish, but here's what the button labels mean:
Zapisz Ustawienia - Save Settings
Domyślne Ustawienia - Default Settings

Thanks

OK, I downloaded and have gone back to read the thread. You said the Default button (AKA defaultsettings), is something different than what I see. Is this the default settings button you keep having to click: "Załaduj domyślne"?

Yea, I just stepped through the loading of the form, and you are changing the appsettings to be the current checkbox states every time a checkbox state is changed, so the first assignment in SetUI():

private void SetUI()
        {
            checkBox1.Checked = AppSet.acon;
            checkBox2.Checked = AppSet.addnew;
            textBox1.Text = AppSet.defNPC;
            checkBox3.Checked = AppSet.shoscr;
            checkBox4.Checked = AppSet.copscr;
            checkBox5.Checked = AppSet.savscr;
            textBox2.Text = AppSet.defpath;
            log.wtl("Interfejs zaktualizowany");
        }

causes all the appsetting states to be changed to the current checkbox's states because each checkbox is tied to the CheckChanged event, which is call the UpdateSettings method....

Here is one fix:

bool bInSetUI = false;
        private void SetUI()
        {
            bInSetUI = true; // suspend CheckChanged event

            checkBox1.Checked = AppSet.acon;
            checkBox2.Checked = AppSet.addnew;
            textBox1.Text = AppSet.defNPC;
            checkBox3.Checked = AppSet.shoscr;
            checkBox4.Checked = AppSet.copscr;
            checkBox5.Checked = AppSet.savscr;
            textBox2.Text = AppSet.defpath;
            log.wtl("Interfejs zaktualizowany");

            bInSetUI = false; // resume CheckChanged event...
        }

        private void UpdateSettings(object sender, EventArgs e)
        {
            if (!bInSetUI)
            {
                AppSet.acon = checkBox1.Checked;
                AppSet.addnew = checkBox2.Checked;
                AppSet.defNPC = textBox1.Text;
                AppSet.shoscr = checkBox3.Checked;
                AppSet.copscr = checkBox4.Checked;
                AppSet.savscr = checkBox5.Checked;
                AppSet.defpath = textBox2.Text;
                log.wtl("Ustawienia zaktualizowane");
            }
        }
commented: That was going to be my suggestion until you stole my thoughts :X +6

You should refactor

log.wtl("Interfejs zaktualizowany");

to:

log.wt[B]f[/B]("Interfejs zaktualizowany");

Its more fun :)

commented: That's great! hahaha +2

Yea, I did what DdoubleD said straight after i posted the last post :P
except instead of

#
bInSetUI = true; // suspend CheckChanged event
#
 
#
checkBox1.Checked = AppSet.acon;
#
checkBox2.Checked = AppSet.addnew;
#
textBox1.Text = AppSet.defNPC;
#
checkBox3.Checked = AppSet.shoscr;
#
checkBox4.Checked = AppSet.copscr;
#
checkBox5.Checked = AppSet.savscr;
#
textBox2.Text = AppSet.defpath;
#
log.wtl("Interfejs zaktualizowany");
#
 
#
bInSetUI = false; // resume CheckChanged event...

mine was more like

#
bInSetUI = true; // suspend CheckChanged event
#
 
#
checkBox1.Checked = AppSet.acon;
#
checkBox2.Checked = AppSet.addnew;
#
textBox1.Text = AppSet.defNPC;
#
checkBox3.Checked = AppSet.shoscr;
#
checkBox4.Checked = AppSet.copscr;
#
checkBox5.Checked = AppSet.savscr;
bInSetUI = false; // resume CheckChanged event...
textBox2.Text = AppSet.defpath;
#
log.wtl("Interfejs zaktualizowany");
#
 
#

So the interface would get updated at the last textbox.
all the wtl does is write to a log, which is on a separate form.
Thanks anyway

If there are no more questions about this issue, please mark as solved--thanks.

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.