I know this has been asked before but I believe my situation is a bit different -- or I don't understand the answers given. I have spent about 4 hours working on this solidly and finally realized, I just don't know what to do.
I have 2 Forms (Form1, Settings) and a class I created called Themes.
I have get/set properties that currently work but are all within Form1 and I would like to move as much code related to themeing as I can OUTSIDE of Form1 and into Themes.cs.
Changing Theme: To change the theme, the user opens up the Settings form and selects a theme from the dropdown menu and presses the 'Set' button -- this all works, but now I want to move it into my own class and I can't get the code to compile.
Here is example code that works before moving -- note that this is only 2 different controls I want to modify but there are about 30 total. I am abridging the code:
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSettings_Click(object sender, EventArgs e)
{
Settings frm = new Settings(this);
frm.Show();
}
private Color txtRSSURLBGProperty;
private Color txtRSSURLFGProperty;
public Color TxtRSSURLBGProperty
{
get { return txtRSSURLBGProperty; }
set { txtRSSURL.BackColor = value; }
}
public Color TxtRSSURLFGProperty
{
get { return txtRSSURLFGProperty; }
set { txtRSSURL.ForeColor = value; }
}
Settings Form:
public partial class Settings : Form
{
public Settings()
{
InitializeComponent();
}
private Form1 rssReaderMain = null;
public Settings(Form requestingForm)
{
rssReaderMain = requestingForm as Form1;
InitializeComponent();
}
private void button2_Click(object sender, EventArgs args)
{
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.TxtRSSURLFGProperty = Color.White;
[......about 25 more of these....]
}
The theme class is currently empty. Again, the goal is to move as much code into the themes class (specifically the get/set statements if at all possible!) and hopefully just use a method similar to this within the Settings form once the proper drowndown item is selected: SetTheme(Default);
I hope someone can help, and I hope I explained it right! I have been racking my brain and I need to have this done fairly soon! Much thanks in advance as I'm sure everyone says. I have teamviewer or logmein if someone wants to remote in -- that is just as easy.
I can also send my project as a zip if needed.
Thanks so much,
Kurt
Modified code for review:
Form1 form:
public partial class Form1 : ThemeableForm
{
public Form1()
{
InitializeComponent();
}
ThemeableForm form:
internal abstract class ThemeableForm : Form
{
private Color rssLabelBGProperty;
private Color rssLabelFGProperty;
public Color RssLabelBGProperty
{
get { return rssLabelBGProperty; }
set { lRSS.BackColor = value; }
}
public Color RssLabelFGProperty
{
get { return rssLabelFGProperty; }
set { lRSS.ForeColor = value; }
}
Settings form:
public Settings(ThemeableForm requestingForm)
{
rssReaderMain = requestingForm as ThemeableForm;
InitializeComponent();
}
private ThemeableForm rssReaderMain = null;
private void button2_Click(object sender, EventArgs args)
{
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.LRSSBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.LRSSFGProperty = Color.White;
}
Now the all the controls in my get/set (lRSS in the example code above) error out with does not exist in the current context. I also get the warning:
Warning 1The designer could not be shown for this file because none of
the classes within it can be designed. The designer inspected the
following classes in the file:
Form1 --- The base class 'RSSReader_BKRF.ThemeableForm' could not be
loaded. Ensure the assembly has been referenced and that all projects
have been built. 0 0
Let the Themes class be composed largely of data that changes when a theme changes: Color, Fonts, etc.
Let the Settings form choose a theme and write it out as the Default Theme. If this is WinForms, then you can just have a static CurrentTheme property of the Themes class which returns the theme chosen on the Settings form.
Let the Form1 and any other forms delegate some of their properties to the current theme:
private Color BackgroundColor
{
get {return Themes.CurrentTheme.BackgroundColor;}
}
private Color TextColor
{
get {return Themes.CurrentTheme.TextColor;}
}
You might then want to push these delegated properties up to a base form class, to be shared by multiple forms.
Ok, I see you are trying to make the Settings form manipulate the values of properties on several (many?) other forms.
One solution is to have every other form inherit from the same abstract class, let's call it ThemeableForm. Now you can define ThemeableForm to have all the common properties.
A short example:
internal abstract class ThemeableForm : Form {
private Color txtRSSURLBGProperty;
private Color txtRSSURLFGProperty;
public Color TxtRSSURLBGProperty
{
get { return txtRSSURLBGProperty; }
set { txtRSSURL.BackColor = value; }
}
public Color TxtRSSURLFGProperty
{
get { return txtRSSURLFGProperty; }
set { txtRSSURL.ForeColor = value; }
}
}
And declare Form1:
public class Form1 : ThemeableForm {
// custom stuff for Form1, no need to write the common properties
}
I declared it as "internal" because you might want to control who/how THemeableForm is inherited. But, you could make it public too. And Settings can work with a ThemeableForm:
public Settings(ThemeableForm requestingForm)
{
rssReaderMain = requestingForm as ThemeableForm;
InitializeComponent();
}
private ThemeableForm rssReaderMain = null;
private void button2_Click(object sender, EventArgs args) {
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.TxtRSSURLFGProperty = Color.White;
[......about 25 more of these....]
}
}
So you don't need to copy any of the Settings code for each and every other form type.
Related
I have multiple forms with the same method called "UpdateTheme" which changes the back colour of the form. I want to be able to call all of these methods from another form.
I tried to make a base form with the "UpdateTheme" method then have all other forms inherit from the base form, But I didnt know how/ if it was possible to then call every instance of the derived forms methods from a separate "Settings" form.
public abstract class CustomForm : Form
{
public void UpdateTheme(string theme)
{
if (theme == "dark")
{
this.BackColor = Color.Black;
}
else if (theme == "light")
{
this.BackColor = Color.White;
}
}
}
In the settings form I would have something like
public void btnSetThemeToDark_Click(object sender, EventArgs e)
{
foreach (instance of derived form)
{
derivedForm.UpdateTheme("dark");
}
}
Whats the best way to do this?
You could create a singleton called StyleManager that contains the global style properties. This singleton has an event called style changed that can be handled by all forms, or a base form. So all of your forms get the information from one source.
StyleManager
public class StyleManager
{
#region singleton
public static StyleManager Instance { get; } = new StyleManager();
private StyleManager()
{
}
#endregion
#region events
public event EventHandler StyleChanged;
private void OnStyleChanged()
{
this.StyleChanged?.Invoke(this, EventArgs.Empty);
}
#endregion
#region properties
public Color BackColor { get; set; }
#endregion
#region methods
public void UpdateBackColor(Color color)
{
this.BackColor = color;
this.OnStyleChanged();
}
#endregion
}
and use it in your forms like this:
public Form()
{
this.InitializeComponent();
//Attach to the event
StyleManager.Instance.StyleChanged += this.StyleChanged;
}
//Handle event
private void StyleChanged(object sender, EventArgs eventArgs)
{
this.BackColor = StyleManager.Instance.BackColor;
}
//set backcolor of all forms
StyleManager.Instance.UpdateBackColor(Color.Yellow);
Assuming this forms are MdiChildren of the form, you would do it this way:
foreach (var form in this.MdiChildren)
{
var castedForm = form as CustomForm;
if (myObjRef != null)
{
castedForm.UpdateTheme("dark");
}
}
This functionality works beyond themes to any common method of the child forms.
I don't think this is the best way to do this. But, you can archive what you want by using this code.
CustomForm mainFrm = (CustomForm)Application.OpenForms["YouCustomFormNameHere"];
mainFrm.UpdateTheme("dark");
Replace 'YouCustomFormNameHere' with your CustomForm form name.
I have a winform called Form1 and a textbox called textBox1
In the Form1 I can set the text by typing:
textBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class?
so I want to change the text for textBox1 in this class.
How can I access the Form1 from this new class?
I would recommend that you don't. Do you really want to have a class that is dependent on how the text editing is implemented in the form, or do you want a mechanism allowing you to get and set the text?
I would suggest the latter. So in your form, create a property that wraps the Text property of the TextBox control in question:
public string FirstName
{
get { return firstNameTextBox.Text; }
set { firstNameTextBox.Text = value; }
}
Next, create some mechanism through which you class can get a reference to the form (through the contructor for instance). Then that class can use the property to access and modify the text:
class SomeClass
{
private readonly YourFormClass form;
public SomeClass(YourFormClass form)
{
this.form = form;
}
private void SomeMethodDoingStuffWithText()
{
string firstName = form.FirstName;
form.FirstName = "some name";
}
}
An even better solution would be to define the possible interactions in an interface, and let that interface be the contract between your form and the other class. That way the class is completely decoupled from the form, and can use anyting implementing the interface (which opens the door for far easier testing):
interface IYourForm
{
string FirstName { get; set; }
}
In your form class:
class YourFormClass : Form, IYourForm
{
// lots of other code here
public string FirstName
{
get { return firstNameTextBox.Text; }
set { firstNameTextBox.Text = value; }
}
}
...and the class:
class SomeClass
{
private readonly IYourForm form;
public SomeClass(IYourForm form)
{
this.form = form;
}
// and so on
}
I was also facing the same problem where I was not able to appendText to richTextBox of Form class. So I created a method called update, where I used to pass a message from Class1.
class: Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void update(string message)
{
textBox1.Text = message;
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 sample = new Class1();
}
}
class: Class1.cs
public class Class1
{
public Class1()
{
Form1._Form1.update("change text");
}
}
You can change the access modifier for the generated field in Form1.Designer.cs from private to public. Change this
private System.Windows.Forms.TextBox textBox1;
by this
public System.Windows.Forms.TextBox textBox1;
You can now handle it using a reference of the form Form1.textBox1.
Visual Studio will not overwrite this if you make any changes to the control properties, unless you delete it and recreate it.
You can also chane it from the UI if you are not confortable with editing code directly. Look for the Modifiers property:
public partial class Form1 : Form
{
public static Form1 gui;
public Form1()
{
InitializeComponent();
gui = this;
}
public void WriteLog(string log)
{
this.Invoke(new Action(() => { txtbx_test1.Text += log; }));
}
}
public class SomeAnotherClass
{
public void Test()
{
Form1.gui.WriteLog("1234");
}
}
I like this solution.
You will need to have some access to the Form's Instance to access its Controls collection and thereby changing the Text Box's Text.
One of ways could be that You can have a Your Form's Instance Available as Public or More better Create a new Constructor For your Second Form and have it receive the Form1's instance during initialization.
Define a property of the form like, then use this in other places it would be available with the form instance
public string SetText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
If your other class inherits Form1 and if your textBox1 is declared public, then you can access that text box from your other class by simply calling:
otherClassInstance.textBox1.Text = "hello world";
Use, a global variable or property for assigning the value to the textbox, give the value for the variable in another class and assign it to the textbox.text in form class.
I Found an easy way to do this,I've tested it,it works Properly.
First I created a Windows Project,on the form I Inserted a TextBox and I named it textBox1
then I inserted a button named button1,then add a class named class1.
in the class1 I created a TextBox:
class class1
{
public static TextBox txt1=new TextBox(); //a global textbox to interfece with form1
public static void Hello()
{
txt1.Text="Hello";
}
}
Now in your Form Do this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
class1.txt1=textBox1;
class1.Hello();
}
}
in the button1_Click I coppied the object textBox1 into txt1,so now txt1 has the properties
of textBox1 and u can change textBox1 text in another form or class.
Form1 form = new Form1();
form.textBox1.Text = "test";
I tried the examples above, but none worked as described. However, I have a solution that is combined from some of the examples:
public static Form1 gui;
public Form1()
{
InitializeComponent();
gui = this;
comms = new Comms();
}
public Comms()
{
Form1.gui.tsStatus.Text = "test";
Form1.gui.addLogLine("Hello from Comms class");
Form1.gui.bn_connect.Text = "Comms";
}
This works so long as you're not using threads. Using threads would require more code and was not needed for my task.
I used this method for updating a label but you could easily change it to a textbox:
Class:
public Class1
{
public Form_Class formToOutput;
public Class1(Form_Class f){
formToOutput = f;
}
// Then call this method and pass whatever string
private void Write(string s)
{
formToOutput.MethodToBeCalledByClass(s);
}
}
Form methods that will do the updating:
public Form_Class{
// Methods that will do the updating
public void MethodToBeCalledByClass(string messageToSend)
{
if (InvokeRequired) {
Invoke(new OutputDelegate(UpdateText),messageToSend);
}
}
public delegate void OutputDelegate(string messageToSend);
public void UpdateText(string messageToSend)
{
label1.Text = messageToSend;
}
}
Finally
Just pass the form through the constructor:
Class1 c = new Class1(this);
Form frm1 = new Form1();
frm1.Controls.Find("control_name",true)[0].Text = "I changed this from another form";
// Take the Active form to a form variable.
Form F1 = myForm1.ActiveForm;
//Findout the Conntrol and Change the properties
F1.Controls.Find("Textbox1", true).ElementAt(0).Text= "Whatever you want to write";
What about
Form1.textBox1.text = "change text";
note:
1. you have to "include" Form1 to your second form source file by
using Form1;
textBox1 should be public
So i have 2 forms.
Form 1 is my main form, and form 2 is where I enter text in a textbox to be displayed on a label on form 1. Also the button to 'confirm' and actually change the entered text of my label is on form 2 which needs to stay that way.
for some reason this does not work.
Form 2 has a text-box and a button, when I press the button, it changes the string value of the designated string.
This string is linked to a label on form 1. the string is being changed so that is not the problem I confirmed this by a adding a button which pops up a message box showing the new string value.
While searching for an answer I found that is must be some sort of refreshing problem, I tried a lot of methods with no success. Only methods that did work where those who would put my button onto form 1 instead of 2.
I've been googling for 3 hours straight on how to fix this problem but either the methods don't work or they change my button from form 2 to my main form (form 1).
Please don't call me lazy I really can't find a method that works!
EDIT:
Code
GameScreen.cs
namespace _2pGame
{
public partial class GameScreen : Form
{
public GameScreen()
{
InitializeComponent();
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
playerinfoload.Show();
}
}
}
PlayerInfo.cs
namespace _2pGame
{
public partial class PlayerInfo : Form
{
public PlayerInfo()
{
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
}
}
}
Refs.cs
namespace _2pGame
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;
}
}
An approach to this very well know situation is through delegates....
In your PlayerInfo form declare
public partial class PlayerInfo : Form
{
// define the delegate type (a parameterless method that returns nothing)
public delegate void OnConfirmPlayer();
// declare a public variable of that delegate type
public OnConfirmPlayer PlayerConfirmed;
.....
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
// Check is someone is interested to be informed of this change
// If someone assign a value to the public delegate variable then
// you have to call that method to let the subscriber know
if (PlayerConfirmed != null)
PlayerConfirmed();
}
}
Then in your GameScreen form, just before showing the PlayerInfo form, set the public PlayerInfo.PlayerConfirmed to a method into the GameScreen form class
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
// Subscribe to the notification from PlayerInfo instance
playerinfoload.PlayerConfirmed += PlayerHasBeenConfirmed;
playerinfoload.Show();
}
// Method that receives the notification from PlayerInfo
private void PlayerHasBeenConfirmed()
{
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
This approach has the advantage to avoid a coupling between the GameScreen and the PlayerInfo. No need to know inside the PlayerInfo the existance of a GameScreen form and the name of its properties. You just publish a delegate that a subscriber could register to be informed of the changes and let the subscriber acts on its own code.
You need a reference to your main form and assign the textbox values each time they need to be updated.
public partial class PlayerInfo : Form
{
private readonly GameScreen _main;
public PlayerInfo(GameScreen main)
{
_main = main;
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
main.P1NameLabel.Text = gm.P1Name;
main.P1ClassLabel.Text = gm.P1Class;
main.P2NameLabel.Text = gm.P2Name;
main.P2ClassLabel.Text = gm.P2Class;
}
}
You also need to pass the reference when the PlayerInfo form is created
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
playerinfoload.Show();
}
Note that there are other better ways to do this, but this is the easiest that I can think of.
You can probably look at events or Mediator pattern if you want something better.
this is not long story!! just it seems to be long ;)
in my app I have user access, it means access to a button relate to its user access scope.
in winform layer: I have a form, it shows all of the determined buttons' name in partitioned checkedListboxes. I dont want fill the form manually. I want create checkedListboxes by code. to get their items'text, I have below planing:
clssMenu_Item: I can save name and text property of one button in this class.
public class clssMenu_Item
{
public string name;
public string text;
}
clssMenu_List: it give me 2D generic List<clssMenu_Item>. all of the buttons in one form will be in a object of this class.
public class clssMenu_List
{
public clssMenu_List ()
{
// I dont know how fill private variables
}
#region private variables
private List<clssMenu_Item> _main ; // buttons in main form
private List<clssMenu_Item> _accountancy; //buttons in accountancy form
private List<clssMenu_Item> _management; //buttons in management form
#endregion
#region public properties
public List<clssMenu_Item> main
{ get { return _main; } }
public List<clssMenu_Item> accountancy
{ get { return _accountancy; } }
public List<clssMenu_Item> management
{ get { return _management; } }
#endregion
}
the buttons in each forms have a common character in their Name property. For example all of the determined buttons in Main form are started with ''Mbtn'', so there isn't any same buttons' Name between forms.
now I dont know how fill private variables in clssMenu_List. then I could use it in my facade layer.
thanks for your attention my friend!! please help me to solve it
I would create a separated helper class that extracts all buttons from a form.
public static class FormHelper
{
public static Button[] GetButtonsFromForm(Form form)
{
// ...
}
}
I would create properties instead of fields:
public class clssMenu_Item
{
public string Name {get;set;}
public string Text {get;set;}
}
A method to create menu_items:
public IEnumerable<clssMenu_Item> GetMenuItemsFromForm(Form form)
{
// convert the buttons to menu_items
return from button in FormHelper.GetButtonsFromForm(form);
select new clssMenu_Item { Name = button.Name, Text = button.Text };
}
Next I would add all buttons to the right list.
public void Fill()
{
clssMenu_List lst = new clssMenu_List();
clssMenu_List.main.AddRange(GetMenuItemsFromForm(mainForm));
clssMenu_List.accountancy.AddRange(GetMenuItemsFromForm(accountancyForm));
clssMenu_List.management.AddRange(GetMenuItemsFromForm(managementForm));
}
Don't forget to create the list in you class:
private List<clssMenu_Item> _main = new List<classMenu_Item>(); // buttons in main form
private List<clssMenu_Item> _accountancy = new List<classMenu_Item>(); //buttons in accountancy form
private List<clssMenu_Item> _management = new List<classMenu_Item>(); //buttons in management form
Personally:
I would store them in a dictionary because you can access them by name. And I would not create properties of List-types. I'd rather create Add/Remove methods.
I have two Forms: MainForm and OptionsForm, wich has a button (OK) that applies the some changes on MainForm. When I open the OptionsForm for the first time everything is ok, with the default values.
After I make some changes and and click OK the options are applied but when I open the OptionsForm for the second time, I wanted to hold the previous values, not the default ones like its happening.
OptionsForm is opened through MainForm like this
OptionsForm formOptions = new OptionsForm();
if (formOptions.ShowDialog(this) == DialogResult.OK)
{
// etc..
}
//...
public string otherLabel
{
get { return formMainLabel.Text; }
set { formMainLabel.Text = value; }
}
In OptionsForm I have a NumericUpDown and want to hold its value
private MainForm mainForm = null;
public OptionsForm(Form callingForm)
{
mainForm = callingForm as MainForm;
InitializeComponent();
}
// ...
private void btnOK_Click(object sender, EventArgs e)
{
this.mainForm.someLabel= someBox.Value.ToString(); // NumericUpDown
this.mainForm.otherLabel = "abc"; //>>> Getting NullReferenceException
this.Close();
}
Now I can hold my settings but I'm getting a NullReferenceException. I tryed this but it's still not working. Any sugestion?
ShowDialog() was already made to support this. It is different from Show(), other than it being modal, it also prevents the form object from being disposed when the user closes it. So you can simply call ShowDialog() again, the controls keep their original values:
private OptionsForm options = new OptionsForm();
private void button1_Click(object sender, EventArgs e) {
if (options.ShowDialog(this) == DialogResult.OK) {
// etc..
}
}
protected override void OnFormClosed(FormClosedEventArgs e) {
options.Dispose();
base.OnFormClosed(e);
}
Well, you should pass these values to OptionsForm, feel free to write custom constructor for OpptionsForm and call it when you need.
One way to do it would be to just always keep a reference to your FormOptions and show the same instance rather than creating a new one every time. Or, if you don't want to do that, you can create an Options class that stores all of your options, which can then be stored and passed into any new instance you create thereafter. There's advantages and disadvantages to both so feel free to choose the option that best suits your needs.
public class MyOptions
{
public String StringOption { get; set; }
public int IntOption { get; set; }
}
Your FormOptions would then have a MyOptions property where you can set all of your options and retrieve them.
public class FormOptions : Form
{
...
private MyOptions _options;
public MyOptions Options
{
get { return _options;}
set
{
_options = value;
// Set the Form's control values accordingly.
}
}
...
}
And finally, you would call it like so in your code:
FormOptions optionsForm = new FormOptions();
MyOptions savedOptions = new MyOptions(); // Probably don't want to create a new instance every time but I'm sure you get the idea here.
optionsForm.Options = savedOptions;
optionsForm.ShowDialog();
// Get the new options after the form is closed.
savedOptions = optionsForm.Options;
if you just want to keep only one numeric up/down control's value, then just pass it as a parameter in the constructor..
public FormOptions(MainForm -mainFrm, int curNumericValue)
{
someBox.value = curNumericValue;
}
and instantiate the form with the value from the MainForm
private void button1_click(....)
{
FormOptions formOptions = new FormOptions(this, Convert.toInt32(someLabel.text));
formOptions.ShowDialog();
}
Why not just make use of the "Settings" that are available within the build properties, and WinForms itself? Just have your settings form populate itself with these variables, and "set" them when you click OK. Then have the MainForm refresh from these settings once the SettingsForm has fully closed. Easy, no mess, and no passing variables between forms.
Overview: http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx
SO Question covering Settings: Save Settings in a .NET Winforms Application
It also has the added bonus that the values will be saved between sessions of the application.
Seems like you are calling an instance of the options form from a click event like this:
button1_click(object sender, EventArgs e)
{
OptionsForm optForm = new OptionsForm();
optForm.showDialog();
}
You have to create variable for the options form class within the Mainform class, instantiate it there, or in the constructor, and only call the ShowDialog() or Show() method within the button_Click event. Like this:
partial class MainForm:Form
{
OptionsForm optForm;
............
............
public MainForm() //Constructor
{
initialiseComponent();
optForm = new OptionsForm();
........
}
.......
private button1_Click(object sender, EventArgs e)
{
optForm.Show(); // or ShowDialog()
}
}
and use this.hide() instead of this.close() in the options form.. or else the form gets disposed..