| | |
Checkboxes won't update
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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:
Can anyone help me with this? Do I have to redraw the form or something? BTW AppSet is the global settings object.
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:
C# Syntax (Toggle Plain Text)
//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.
0
#3 Oct 27th, 2009
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
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
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#5 Oct 29th, 2009
Anybody?
Here is my code:
Here is my code:
C# Syntax (Toggle Plain Text)
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) { } } }
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#6 Oct 29th, 2009
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
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
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
0
#7 Oct 29th, 2009
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"?
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
0
#8 Oct 29th, 2009
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():
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....
C# Syntax (Toggle Plain Text)
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....
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
1
#9 Oct 29th, 2009
Here is one fix:
C# Syntax (Toggle Plain Text)
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"); } }
2
#10 Oct 29th, 2009
You should refactor
to:
Its more fun
C# Syntax (Toggle Plain Text)
log.wtl("Interfejs zaktualizowany");
to:
log.wtf("Interfejs zaktualizowany");Its more fun
![]() |
Similar Threads
- updating Checkbox and Textbox to change price (JavaScript / DHTML / AJAX)
- Masterpage & Application state (C#)
- cancel button problem (VB.NET)
- How to update database from checkboxes? (PHP)
- Checkbox Arrays ARGH!! (PHP)
- svchost.exe - Windows Update - 100% CPU Utilization fix (EASY) (Windows NT / 2000 / XP)
- Update Scoreboard - ASP (ASP)
- How to pre-select checkboxes in form? (PHP)
- counting checkboxes (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Simple Database Handling
- Next Thread: thread.sleep serial port handling
Views: 848 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for checkbox







