I have two forms here and I want to call the function from one to the other in c# winform. I use events to do this, but they do not work. Every time I call changeTheme (), it throws me a System.NullReferenceException error: 'Object reference not set to an instance of an object.'
I tried to call her through the instance of the Main_form form, but it did not work either. I also tried the events, but when I added a function to make it so it was set to null again. But I'd like to do it with an event.
public delegate void statusChange();
public partial class Settings_form : Form
{
public event statusChange changeTheme;
//Here is some function, variables declaration and code
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
//if (changeTheme != null)
changeTheme();
}
}
public partial class Main_form : Form
{
private void Form1_Load(object sender, EventArgs e)
{
callChangeTheme();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
public void chooseOtherTheme()
{
if (SettingsClass.UseDarkMode)
ToDarkMode();
else ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(((int)(((byte)(241)))), ((int)(((byte)(241)))), ((int)(((byte)(241)))));
}
}