Checkboxes won't update

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 5
Reputation: mcmillan0520 is an unknown quantity at this point 
Solved Threads: 0
mcmillan0520 mcmillan0520 is offline Offline
Newbie Poster

Checkboxes won't update

 
0
  #1
Oct 26th, 2009
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:
  1. //Default settings constructor inside Settings class
  2. public Settings()
  3. {
  4. acon = true;
  5. addnew = true;
  6. defNPC = "";
  7. shoscr = true;
  8. copscr = false;
  9. savscr = false;
  10. defpath = "C:\\Program Files\\Gothic\\_Work\\Scripts";
  11. }
  12.  
  13. //my update code
  14. private void button11_Click(object sender, EventArgs e)
  15. {
  16. AppSet = new Settings();
  17. log.wtl("Domyślne ustawienia załadowane");
  18. SetUI();
  19. }
  20.  
  21. private void SetUI()
  22. {
  23. checkBox1.Checked = AppSet.acon;
  24. checkBox2.Checked = AppSet.addnew;
  25. textBox1.Text = AppSet.defNPC;
  26. checkBox3.Checked = AppSet.shoscr;
  27. checkBox4.Checked = AppSet.copscr;
  28. checkBox5.Checked = AppSet.savscr;
  29. textBox2.Text = AppSet.defpath;
  30. log.wtl("Interfejs zaktualizowany");
  31. }

Can anyone help me with this? Do I have to redraw the form or something? BTW AppSet is the global settings object.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
Oct 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 470
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 93
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
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
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: mcmillan0520 is an unknown quantity at this point 
Solved Threads: 0
mcmillan0520 mcmillan0520 is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
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
Attached Files
File Type: zip Script Maker.zip (793.2 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: mcmillan0520 is an unknown quantity at this point 
Solved Threads: 0
mcmillan0520 mcmillan0520 is offline Offline
Newbie Poster
 
0
  #5
Oct 29th, 2009
Anybody?
Here is my code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using System.IO;
  12.  
  13.  
  14. namespace Script_Maker
  15. {
  16.  
  17. public partial class Main : Form
  18. {
  19. Log log;
  20. Settings AppSet;
  21. public Main()
  22. {
  23. InitializeComponent();
  24.  
  25. }
  26. public void ch8(string text)
  27. {
  28. button8.Text = text;
  29. }
  30.  
  31. private void button8_Click(object sender, EventArgs e)
  32. {
  33. if (log.Visible == false)
  34. {
  35. log.Show();
  36. button8.Text = "Schowaj Log";
  37. }
  38. else
  39. {
  40. log.Hide();
  41. button8.Text = "Pokaż Log";
  42. }
  43. }
  44. private Settings LoadSettings(string path)
  45. {
  46. Settings tempset;
  47. try
  48. {
  49. Stream fs = File.Open(path, FileMode.Open);
  50. BinaryFormatter bf = new BinaryFormatter();
  51. tempset = (Settings)bf.Deserialize(fs);
  52. fs.Close();
  53. log.wtl("Ustawienia załadowane z pliku " + Path.GetFileName(path));
  54.  
  55. }
  56. catch(FileNotFoundException e) //Nie ma pliku ustawień, ustawiamy domyślne.
  57. {
  58. log.wtl("Plik ustawień nie istnieje");
  59. tempset = new Settings();
  60. log.wtl("Załadowano ustawienia domyślne");
  61. SaveSettings(tempset);
  62.  
  63.  
  64.  
  65. }
  66.  
  67. return tempset;
  68. }
  69. private Settings LoadSettings()
  70. {
  71. return LoadSettings("settings.dat");
  72. }
  73. private void SaveSettings(Settings set)
  74. {
  75. SaveSettings(set, "settings.dat");
  76. }
  77. private void SaveSettings(Settings set, string path)
  78. {
  79. Stream fs = File.Open(path, FileMode.Create);
  80. BinaryFormatter bf = new BinaryFormatter();
  81. bf.Serialize(fs, set);
  82. fs.Close();
  83. log.wtl("Ustawienia zapisane do pliku " + Path.GetFileName(path));
  84. }
  85.  
  86. private void button10_Click(object sender, EventArgs e)
  87. {
  88. SaveSettings(AppSet);
  89. }
  90.  
  91. private void UpdateSettings(object sender, EventArgs e)
  92. {
  93. AppSet.acon = checkBox1.Checked;
  94. AppSet.addnew = checkBox2.Checked;
  95. AppSet.defNPC = textBox1.Text;
  96. AppSet.shoscr = checkBox3.Checked;
  97. AppSet.copscr = checkBox4.Checked;
  98. AppSet.savscr = checkBox5.Checked;
  99. AppSet.defpath = textBox2.Text;
  100. log.wtl("Ustawienia zaktualizowane");
  101. }
  102.  
  103. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  104. {
  105. log.Dispose();
  106. Application.Exit();
  107. }
  108.  
  109. private void Main_Load(object sender, EventArgs e)
  110. {
  111. log = new Log();
  112. log.Owner = this;
  113. log.wtl("Witamy w programie TDM Script Maker");
  114. AppSet = LoadSettings();
  115. SetUI();
  116.  
  117. }
  118. private void SetUI()
  119. {
  120. checkBox1.Checked = AppSet.acon;
  121. checkBox2.Checked = AppSet.addnew;
  122. textBox1.Text = AppSet.defNPC;
  123. checkBox3.Checked = AppSet.shoscr;
  124. checkBox4.Checked = AppSet.copscr;
  125. checkBox5.Checked = AppSet.savscr;
  126. textBox2.Text = AppSet.defpath;
  127. log.wtl("Interfejs zaktualizowany");
  128. }
  129.  
  130. private void button11_Click(object sender, EventArgs e)
  131. {
  132. AppSet = new Settings();
  133. log.wtl("Domyślne ustawienia załadowane");
  134. SetUI();
  135. }
  136. }
  137. [Serializable()]
  138. public class Settings : ISerializable
  139. {
  140.  
  141. public Settings(bool ac, bool an, string dn, bool ss, bool cs, bool sss, string dp)
  142. {
  143. acon = ac;
  144. addnew = an;
  145. defNPC = dn;
  146. shoscr = ss;
  147. copscr = cs;
  148. savscr = sss;
  149. defpath = dp;
  150. }
  151. public Settings()
  152. {
  153. acon = true;
  154. addnew = true;
  155. defNPC = "";
  156. shoscr = true;
  157. copscr = false;
  158. savscr = false;
  159. defpath = "C:\\Program Files\\Gothic\\_Work\\Scripts";
  160. }
  161. public Settings(SerializationInfo info, StreamingContext ctxt)
  162. {
  163. acon = (bool)info.GetValue("ac", typeof(bool));
  164. addnew = (bool)info.GetValue("an", typeof(bool));
  165. defNPC = (string)info.GetValue("dn", typeof(string));
  166. shoscr = (bool)info.GetValue("ss", typeof(bool));
  167. copscr = (bool)info.GetValue("cs", typeof(bool));
  168. savscr = (bool)info.GetValue("sss", typeof(bool));
  169. defpath = (string)info.GetValue("dp", typeof(string));
  170. }
  171. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  172. {
  173. info.AddValue("ac", acon);
  174. info.AddValue("an", addnew);
  175. info.AddValue("dn", defNPC);
  176. info.AddValue("ss", shoscr);
  177. info.AddValue("cs", copscr);
  178. info.AddValue("sss", savscr);
  179. info.AddValue("dp", defpath);
  180. }
  181. public bool acon;
  182. public bool addnew;
  183. public string defNPC;
  184. public bool shoscr;
  185. public bool copscr;
  186. public bool savscr;
  187. public string defpath;
  188. }
  189. public class Items : ISerializable
  190. {
  191. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  192. {
  193.  
  194. }
  195. }
  196. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: mcmillan0520 is an unknown quantity at this point 
Solved Threads: 0
mcmillan0520 mcmillan0520 is offline Offline
Newbie Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #7
Oct 29th, 2009
Originally Posted by mcmillan0520 View Post
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"?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
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():

  1. private void SetUI()
  2. {
  3. checkBox1.Checked = AppSet.acon;
  4. checkBox2.Checked = AppSet.addnew;
  5. textBox1.Text = AppSet.defNPC;
  6. checkBox3.Checked = AppSet.shoscr;
  7. checkBox4.Checked = AppSet.copscr;
  8. checkBox5.Checked = AppSet.savscr;
  9. textBox2.Text = AppSet.defpath;
  10. log.wtl("Interfejs zaktualizowany");
  11. }

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....
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
1
  #9
Oct 29th, 2009
Here is one fix:

  1. bool bInSetUI = false;
  2. private void SetUI()
  3. {
  4. bInSetUI = true; // suspend CheckChanged event
  5.  
  6. checkBox1.Checked = AppSet.acon;
  7. checkBox2.Checked = AppSet.addnew;
  8. textBox1.Text = AppSet.defNPC;
  9. checkBox3.Checked = AppSet.shoscr;
  10. checkBox4.Checked = AppSet.copscr;
  11. checkBox5.Checked = AppSet.savscr;
  12. textBox2.Text = AppSet.defpath;
  13. log.wtl("Interfejs zaktualizowany");
  14.  
  15. bInSetUI = false; // resume CheckChanged event...
  16. }
  17.  
  18. private void UpdateSettings(object sender, EventArgs e)
  19. {
  20. if (!bInSetUI)
  21. {
  22. AppSet.acon = checkBox1.Checked;
  23. AppSet.addnew = checkBox2.Checked;
  24. AppSet.defNPC = textBox1.Text;
  25. AppSet.shoscr = checkBox3.Checked;
  26. AppSet.copscr = checkBox4.Checked;
  27. AppSet.savscr = checkBox5.Checked;
  28. AppSet.defpath = textBox2.Text;
  29. log.wtl("Ustawienia zaktualizowane");
  30. }
  31. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
2
  #10
Oct 29th, 2009
You should refactor
  1. log.wtl("Interfejs zaktualizowany");

to:
log.wtf("Interfejs zaktualizowany");

Its more fun
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
checkbox

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 848 | Replies: 11
Thread Tools Search this Thread



Tag cloud for checkbox
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC