I’m using MainWindow and Settings. MainWindow is the startup window from which I can open Settings. I’m trying to share some properties between both windows. Right now, I have the public properties declared in Settings:
public partial class Settings : Form
{
private string property1
public Settings()
{
InitializeComponent();
this.changeSettings();
}
public string property1
{
get { return property1; }
set { property1 = value; }
}
public void changeSettings()
{
textbox.Text = property1;
}
}
I can create an instance of Settings in MainWindow and change the properties from there:
public partial class Mainwindow : Form
{
private Settings settings;
public MainWindow()
{
InitializeComponent();
settings = new Settings();
this.changeSettings();
}
private void changeSettings()
{
settings.property1 = "value";
textbox.Text = settings.property1;
}
private void openSettings_Click(object sender, EventArgs e)
{
settings.ShowDialog();
}
}
Say, I want to change the contents of the textboxes in both forms. For MainWindow this works, i.e. I can store the value in the property and access it again. However, I open up Settings and try to change its textbox, the property is empty!
What could explain this?
The flaw is in Settings.property1, it doesn’t update the text box that displays its value. A simple solution is:
You’ll also need to update your MainWindow’s text box after displaying the dialog: