Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Preference form in my application, and whenever it is closed, changes are not made. When a check box is checked in form 2, I want a feature of form 1 to be changed. However, I want this second form to be closed. And when it is closed, the features are not saved. So when I open the preference window again, the defaults are set. I believe I need to save the information into some sort of text file. And have form 1 read off of the text file. Any help?
Posted
Updated 12-Aug-11 20:27pm
v2

 
Share this answer
 
Comments
vlad781 13-Aug-11 2:35am    
Please try the link again, I am just being refreshed back to this page. Thanks.
vlad781 13-Aug-11 2:41am    
Nevermind, I searched your profile for the post. I found it, and I will try the code tomorrow.
If I read your question correctly, you would need a separate class where you store your preferences, This class could be even static which would help in handling the preferences. For example (pseudo code):

C#
public static class Preferences {
   public int FontSize { get; set; }
   ...
}


Now you can modify the preferences directly from your window.
Preferences.FontSize = 123;


If you want to react on the changes in another form you can use events for that. For example (pseudo again):
C#
public static class Preferences {
   public static EventHandler SomethingHasChanged;

   public int FontSize { 
      get {
         ...
      } 
      set {
         ...
         EventHandler theEvent = SomethingHasChanged;
         if (theEvent != null) {
            theEvent();        
         }
      }
   ...


Just remember that when you subscribe the event in an instance (for example in a form), you must also unsubscribe it when the form is closed. Otherwise the reference isn't cleaned because the event publisher is static.

If you wish, you can save the information in your preferences into a file etc. One way is to serialize it to a file. One good example: http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file[^]

Addition (example)
First add a new static class to the project:
C#
public static class Preferences {
   public static event System.EventHandler PrefChanged;
   private static bool _someSetting = false;

   public static bool SomeSetting {
      get {
         return Preferences._someSetting;
      }
      set {
         Preferences._someSetting = value;
         if (PrefChanged != null) {
            PrefChanged(null, new System.EventArgs());
         }
      }
   }
}


Now in the Form1 where you want to receive changes in the preferences add for example to constructor:
C#
Preferences.PrefChanged += new System.EventHandler(Preferences_PrefChanged);


And a method to this form:
C#
public void Preferences_PrefChanged(object sender, System.EventArgs e) {
   this.TextBox1.Text = ("SomeSetting is now " + Preferences.SomeSetting.ToString());
}


And in the form2 where you modify settings, add to the click event of the checkbox:
C#
Preferences.SomeSetting = this.checkBox1.Value;


Also somewhere where Form1 is closed you should add:
C#
Preferences.PrefChanged -= new System.EventHandler(Preferences_PrefChanged);

After this if you run the program (there may be typos in the code blocks), whenever you change the value of the checkbox the textbox should show the current value of the setting.
 
Share this answer
 
v2
Comments
vlad781 13-Aug-11 13:28pm    
I'm not really following you here.. Can you please provide an example in which a check box in form 2 (preferences) is changes a label's text in form 1? Thanks a lot.
Wendelius 13-Aug-11 15:57pm    
No problem. Most likely I didn't explain it clearly. I updated the solution so have a look if it makes any sense :)
vlad781 13-Aug-11 17:43pm    
And exactly where would i be adding the new static class? Into which script?
Wendelius 13-Aug-11 17:51pm    
You can create a new file for it if you like or include it into an existing file. Just make sure that it's nested inside a namespace that's referenced in Form1 and Form2. Otherwise you have to include the namespace definition before the class when you use it, like:
SomeNamespace.Preferences.SomeSetting = this.checkBox1.Value;
vlad781 13-Aug-11 16:25pm    
Thanks, I'll check things out when I get to my computer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900