When I have two non-static forms, how to make an event between them
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)))));
}
}
c# winforms events
add a comment |
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)))));
}
}
c# winforms events
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13
add a comment |
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)))));
}
}
c# winforms events
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)))));
}
}
c# winforms events
c# winforms events
edited Jan 2 at 1:09
EJoshuaS
7,308102949
7,308102949
asked Jan 1 at 19:30
MineFuf LPMineFuf LP
42
42
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13
add a comment |
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13
add a comment |
2 Answers
2
active
oldest
votes
One big issue I notice is here:
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
You're creating a new instance of Settings_form
every time you call this method, but you never actually do anything with the instance - the only time you call Show()
is on a completely different instance:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
If you want to act Settings_form
from your main form, you should be sure that you're always acting on the same instance.
Also, please do be mindful of subscribing and unsubscribing events in a way that guarantees that you don't accidentally end up with a memory leak. You can see more details on this here: Why and How to avoid Event Handler memory leaks?
add a comment |
The null reference exception is happening because nothing is subscribed to your event.
Generally you'd create an event like this:
public event EventHandler<EventArgs> MyEvent;
It's a lot easier to read than the older event/delegate style but works in exactly the same way.
And fire it like this:
MyEvent?.Invoke(this, EventArgs.Empty);
That way it will only fire if you have subscribers. Again analogous to the if != null fire older style.
With that out of the way, there is a simpler way you can achieve what you want without events.
If you create a derived form and have the change code implemented there you can create the rest of your forms out of that then just call the change code directly:
Your new base form:
public MyDerivedForm : Form
{
public MyDerivedForm()
{
InitializeComponent();
}
public void ChangeTheme(bool usedarkmode)
{
if (usedarkmode)
ToDarkMode();
else
ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(28, 28, 28);
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(241, 241, 241);
}
}
I've simplified your FromArgb calls. All your casting was completely superfluous.
Now derive all your other forms from MyDerivedForm
:
public MainForm : MyDerivedForm
{
}
Just add a new form to the project and change the : Form
to : MyDerivedForm
In your Settings_form you can just loop over all your opened forms and call the ChangeTheme function on each one which will then switch between light and dark mode.
public Settings_form: MyDerivedForm
{
public Settings_form()
{
InitializeComponent();
}
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
foreach(MyDerivedForm form in Application.OpenForms.OfType<MyDerivedForm>())
{
form.ChangeTheme(SettingsClass.UseDarkMode);
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998331%2fwhen-i-have-two-non-static-forms-how-to-make-an-event-between-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One big issue I notice is here:
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
You're creating a new instance of Settings_form
every time you call this method, but you never actually do anything with the instance - the only time you call Show()
is on a completely different instance:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
If you want to act Settings_form
from your main form, you should be sure that you're always acting on the same instance.
Also, please do be mindful of subscribing and unsubscribing events in a way that guarantees that you don't accidentally end up with a memory leak. You can see more details on this here: Why and How to avoid Event Handler memory leaks?
add a comment |
One big issue I notice is here:
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
You're creating a new instance of Settings_form
every time you call this method, but you never actually do anything with the instance - the only time you call Show()
is on a completely different instance:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
If you want to act Settings_form
from your main form, you should be sure that you're always acting on the same instance.
Also, please do be mindful of subscribing and unsubscribing events in a way that guarantees that you don't accidentally end up with a memory leak. You can see more details on this here: Why and How to avoid Event Handler memory leaks?
add a comment |
One big issue I notice is here:
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
You're creating a new instance of Settings_form
every time you call this method, but you never actually do anything with the instance - the only time you call Show()
is on a completely different instance:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
If you want to act Settings_form
from your main form, you should be sure that you're always acting on the same instance.
Also, please do be mindful of subscribing and unsubscribing events in a way that guarantees that you don't accidentally end up with a memory leak. You can see more details on this here: Why and How to avoid Event Handler memory leaks?
One big issue I notice is here:
public void callChangeTheme()
{
Settings_form settings_Form = new Settings_form();
settings_Form.changeTheme += new statusChange(chooseOtherTheme);
}
You're creating a new instance of Settings_form
every time you call this method, but you never actually do anything with the instance - the only time you call Show()
is on a completely different instance:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings_form settings_Form = new Settings_form();
settings_Form.Show();
}
If you want to act Settings_form
from your main form, you should be sure that you're always acting on the same instance.
Also, please do be mindful of subscribing and unsubscribing events in a way that guarantees that you don't accidentally end up with a memory leak. You can see more details on this here: Why and How to avoid Event Handler memory leaks?
answered Jan 2 at 1:14
EJoshuaSEJoshuaS
7,308102949
7,308102949
add a comment |
add a comment |
The null reference exception is happening because nothing is subscribed to your event.
Generally you'd create an event like this:
public event EventHandler<EventArgs> MyEvent;
It's a lot easier to read than the older event/delegate style but works in exactly the same way.
And fire it like this:
MyEvent?.Invoke(this, EventArgs.Empty);
That way it will only fire if you have subscribers. Again analogous to the if != null fire older style.
With that out of the way, there is a simpler way you can achieve what you want without events.
If you create a derived form and have the change code implemented there you can create the rest of your forms out of that then just call the change code directly:
Your new base form:
public MyDerivedForm : Form
{
public MyDerivedForm()
{
InitializeComponent();
}
public void ChangeTheme(bool usedarkmode)
{
if (usedarkmode)
ToDarkMode();
else
ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(28, 28, 28);
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(241, 241, 241);
}
}
I've simplified your FromArgb calls. All your casting was completely superfluous.
Now derive all your other forms from MyDerivedForm
:
public MainForm : MyDerivedForm
{
}
Just add a new form to the project and change the : Form
to : MyDerivedForm
In your Settings_form you can just loop over all your opened forms and call the ChangeTheme function on each one which will then switch between light and dark mode.
public Settings_form: MyDerivedForm
{
public Settings_form()
{
InitializeComponent();
}
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
foreach(MyDerivedForm form in Application.OpenForms.OfType<MyDerivedForm>())
{
form.ChangeTheme(SettingsClass.UseDarkMode);
}
}
}
add a comment |
The null reference exception is happening because nothing is subscribed to your event.
Generally you'd create an event like this:
public event EventHandler<EventArgs> MyEvent;
It's a lot easier to read than the older event/delegate style but works in exactly the same way.
And fire it like this:
MyEvent?.Invoke(this, EventArgs.Empty);
That way it will only fire if you have subscribers. Again analogous to the if != null fire older style.
With that out of the way, there is a simpler way you can achieve what you want without events.
If you create a derived form and have the change code implemented there you can create the rest of your forms out of that then just call the change code directly:
Your new base form:
public MyDerivedForm : Form
{
public MyDerivedForm()
{
InitializeComponent();
}
public void ChangeTheme(bool usedarkmode)
{
if (usedarkmode)
ToDarkMode();
else
ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(28, 28, 28);
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(241, 241, 241);
}
}
I've simplified your FromArgb calls. All your casting was completely superfluous.
Now derive all your other forms from MyDerivedForm
:
public MainForm : MyDerivedForm
{
}
Just add a new form to the project and change the : Form
to : MyDerivedForm
In your Settings_form you can just loop over all your opened forms and call the ChangeTheme function on each one which will then switch between light and dark mode.
public Settings_form: MyDerivedForm
{
public Settings_form()
{
InitializeComponent();
}
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
foreach(MyDerivedForm form in Application.OpenForms.OfType<MyDerivedForm>())
{
form.ChangeTheme(SettingsClass.UseDarkMode);
}
}
}
add a comment |
The null reference exception is happening because nothing is subscribed to your event.
Generally you'd create an event like this:
public event EventHandler<EventArgs> MyEvent;
It's a lot easier to read than the older event/delegate style but works in exactly the same way.
And fire it like this:
MyEvent?.Invoke(this, EventArgs.Empty);
That way it will only fire if you have subscribers. Again analogous to the if != null fire older style.
With that out of the way, there is a simpler way you can achieve what you want without events.
If you create a derived form and have the change code implemented there you can create the rest of your forms out of that then just call the change code directly:
Your new base form:
public MyDerivedForm : Form
{
public MyDerivedForm()
{
InitializeComponent();
}
public void ChangeTheme(bool usedarkmode)
{
if (usedarkmode)
ToDarkMode();
else
ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(28, 28, 28);
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(241, 241, 241);
}
}
I've simplified your FromArgb calls. All your casting was completely superfluous.
Now derive all your other forms from MyDerivedForm
:
public MainForm : MyDerivedForm
{
}
Just add a new form to the project and change the : Form
to : MyDerivedForm
In your Settings_form you can just loop over all your opened forms and call the ChangeTheme function on each one which will then switch between light and dark mode.
public Settings_form: MyDerivedForm
{
public Settings_form()
{
InitializeComponent();
}
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
foreach(MyDerivedForm form in Application.OpenForms.OfType<MyDerivedForm>())
{
form.ChangeTheme(SettingsClass.UseDarkMode);
}
}
}
The null reference exception is happening because nothing is subscribed to your event.
Generally you'd create an event like this:
public event EventHandler<EventArgs> MyEvent;
It's a lot easier to read than the older event/delegate style but works in exactly the same way.
And fire it like this:
MyEvent?.Invoke(this, EventArgs.Empty);
That way it will only fire if you have subscribers. Again analogous to the if != null fire older style.
With that out of the way, there is a simpler way you can achieve what you want without events.
If you create a derived form and have the change code implemented there you can create the rest of your forms out of that then just call the change code directly:
Your new base form:
public MyDerivedForm : Form
{
public MyDerivedForm()
{
InitializeComponent();
}
public void ChangeTheme(bool usedarkmode)
{
if (usedarkmode)
ToDarkMode();
else
ToLightMode();
}
public void ToDarkMode()
{
this.BackColor = Color.FromArgb(28, 28, 28);
}
public void ToLightMode()
{
this.BackColor = Color.FromArgb(241, 241, 241);
}
}
I've simplified your FromArgb calls. All your casting was completely superfluous.
Now derive all your other forms from MyDerivedForm
:
public MainForm : MyDerivedForm
{
}
Just add a new form to the project and change the : Form
to : MyDerivedForm
In your Settings_form you can just loop over all your opened forms and call the ChangeTheme function on each one which will then switch between light and dark mode.
public Settings_form: MyDerivedForm
{
public Settings_form()
{
InitializeComponent();
}
private void UseDarkMode_chk_CheckedChanged(object sender, EventArgs e)
{
//Some code
SettingsClass.UseDarkMode = this.UseDarkMode_chk.Checked;
foreach(MyDerivedForm form in Application.OpenForms.OfType<MyDerivedForm>())
{
form.ChangeTheme(SettingsClass.UseDarkMode);
}
}
}
edited Jan 2 at 8:55
answered Jan 1 at 21:12
Handbag CrabHandbag Crab
1,3271312
1,3271312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998331%2fwhen-i-have-two-non-static-forms-how-to-make-an-event-between-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which line of code gives you the error ?
– Chetan Ranpariya
Jan 1 at 21:13