Senpai
Senpai

Reputation: 513

Access object from another class

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

Answers (4)

Harald Coppoolse
Harald Coppoolse

Reputation: 30502

It seems you have two classes:

  • A class Form1 that has a timer that you want to change,
  • A settings Form where you want to set the timer in Form1

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.

  • Advantage: the operator can change the value several times, or even press a Cancel button. Only if the OK button is pressed the time value will be set.
  • Advantage: if the settings form controls several settings that relate to each other it is much easier to check for validity of the changed values when the operator expresses he is finishing changing the value when he presses OK
  • Disadvantage: the operator doesn't know the effect of the changed value until he presses OK.
  • This disadvantage can be solved by offering an Apply Now button.
  • Note that the OK / Cancel / Apply Now is standard Windows behaviour. Everyone will know what will happen, while a method that changes some settings of your form immediately and some not might be confusing.

Having said this, I assume the following:

  • Upon some event (probably clicking a button, or selecting a menu item) the form decides that it is time to show its settings Form.
  • The operator changes zero or more settings and presses OK or Cancel
  • If the operator pressed OK the main form is informed. It will ask the settings form for the settings and update his settings accordingly.

So let's make an example:

  • We have a Form1 with a button1 and a timer1, It has several settings, for instance TimerTimeout, Setting1 and Setting2.
  • If the operator clicks button1, then the settings form is shown.
  • Initially the settings form shows the current values of the timerTimeout, Setting1 and Setting2.
  • The operator can change the values of any of these settings.
  • When he finishes, he can click Cancel or Ok

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

IamK
IamK

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

Mohammad Shraim
Mohammad Shraim

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

Sophie Coyne
Sophie Coyne

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

Related Questions