Reputation: 513
I'm trying to change the interval value of a timer (in Form1) from my settings form.
I'm declaring the form1 with static Form1 formInstance;
But it throws null exception on forminstance when i change the value.
I've also tried Form1 form1 = new Form1();
but the application crashes when i open settings form. Note that i've already have timer modifier set to public.
public partial class SettingsForm : Form
{
static Form1 formInstance;
...
if (Properties.Settings.Default.sync != Convert.ToInt32(textBox1.Text))
{
formInstance.timer1.Stop();
formInstance.timer1.Interval = 60000;
formInstance.timer1.Start();
}
Upvotes: 1
Views: 2423
Reputation: 30502
It seems you have two classes:
In windows it is more common to let the form remember all changed settings, until the operator presses OK or Cancel, instead of updating the setting as soon as the operator changes the value.
Having said this, I assume the following:
So let's make an example:
The code is as follows:
public class SettingsForm : Form
{
public TimeSpan TimerTime {get; set;}
public int Setting1 {get; set;}
public string Setting2 {get; set;}
public void OnButtonOk_Clicked(object sender, ...)
{
if (!this.AllValuesOk())
{ // error in one of the values
// show message box
}
else
{
this.Close();
}
}
}
Now Let's show the settings form when button1 is clicked on Form1:
private void OnButton1_Clicked(object sender, ...)
{
using (var settingsForm = new SettingsForm)
{
settingsForm.TimerTime = this.TimerTime;
settingsForm.Setting1 = this.Setting1;
settingsForm.Setting2 = this.Setting2;
// now that the settings form is initialized
// we can show it and wait until the user closes the form
var dlgResult = settingsForm.ShowDialog(this);
// only use the new settings if the operator pressed OK:
if (dlgResult == DialogResult.OK)
{
this.TimerTime = settingsForm.TimerTime;
this.Setting1 = settingsForm.Setting1;
this.Setting2 = settingsForm.Setting2;
this.ProcessChangedSettings();
}
}
}
Note that the settings form doesn't have to know anything about Form1. This enables it to use the settings form for any form that has a TimerTimer, a Setting1 and a Setting2.
Do you also notice how easy it is to use the original settings if the user presses Cancel, or presses the cross in the upper right corner of the settings form or presses alt-F4, or whatever method he can use to cancel.
Also note that during changing, the operator can have incompabible settings, for instance a halve finished string, an incorrect timer time etc. Only the moment he presses OK the software checks if all values are filled in correctly.
Upvotes: 2
Reputation: 2994
Use properties in Form1 and access it from another form.. In Setting form create a private variable for example:
private int interval;
create a getter setter method:
public int Interval
{
get { return interval; }
set { interval = value; }
}
Then in SettingsForm return a this.DialogResult = DialogResult.OK;
dialog result at the method end, for example at the end of the button click event.
In Form1 when you open the SettingsForm you can access the value set in SettingsForm:
if(settingsForm.ShowDialog() == DialogResults.OK)
{
timer1.Interval = settingsForm.Interval;
}
Upvotes: 0
Reputation: 1313
pass form1 as parameter on the constructor of settingForm
private Form1 _objForm1;
public SettingForm (Form1 objForm1)
{
_objForm1 = objForm1;
}
Private ButtonClicked(sender,...)
{
_objForm1.timer1.Stop();
_objForm1.timer1.Interval = 60000;
_objForm1.timer1.Start();
}
Upvotes: 0
Reputation: 1006
If you want to access the timer but not from an instance,
Form1.timer.Interval = 4;
you would have to go into "Form1.designer.cs" and find where it says
public Timer timer1;
and change it to
public static Timer timer1;
Upvotes: -1