In my projects main form I establish a timer which calls on an object of a different class to set its intervals.
Clock.Interval = formSettings.GetTimeInTicks();
Clock.Tick += new EventHandler(TimerEventProcessor);
Clock.Start();
The time intervals is a setting in my Settings class. When the user chooses a new time from the combobox of times, I want it to restart the Clock that is established in my Main form/class, and use the newly selected time. How can I access the established “Clock” object from my main class?
First I thought I could make the clock be its own class and instantiate an object of that class from the main class and the settings class, however I think that leaves me with two timers active which is not what I want. Could I pass the Clock object from the main class to the settings class? What is the best route to take here?
EDIT:
My main form creates the Timer
Timer Clock = new Timer();
Then I establish the time each timer cycle should be, and start the timer.
public void MakeTheClockTimer()
{
Clock.Interval = formSettings.GetTimeInTicks();
Clock.Tick += new EventHandler(TimerEventProcessor);
Clock.Start();
}
My second form is referenced as “formSettings”. This form contains a combobox containing different time selections such as 1 minute, 15 minutes etc. When the program starts it performs the following start up:
public Form1()
{
InitializeComponent();
MakeTheClockTimer();
}
So the program starts, and looks at the settings form, returns what time is selected, and starts the timer. What I want to do is if the user chooses a different time, I want the program to basically call the “MakeTheClockTimer()” again. I need it to restart the timer if the time selection has changed. So something like:
private void cbxTime_SelectedIndexChanged(object sender, EventArgs e)
{
MainFormObject.MakeTheClockTimer();
}
This would make it so that when the combobox was changed, it would re-establish the timers settings and start the timer again based on those settings.
Yes, but you will not be passing the object, you will be passing a reference to the object.