user198003
user198003

Reputation: 11161

How to stop Timer on parent form (C#)

I have Timer control (and a Button) on a first form.

Idea is that when user clicks on Button, new modal window with new button is showed, with question if he wants to stop a timer when he click on modal form's button.

Can you help me how i can achieve that?

Upvotes: 1

Views: 1026

Answers (4)

Espen Burud
Espen Burud

Reputation: 1881

You can also set the DialogResult on the form with the question according to the answer and check the result in the main dialog.

ConfirmationForm confirmationForm = new ConfirmationForm();

DialogResult dialogResult = confirmationForm.ShowDialog();
if (dialogResult == DialogResult.OK)
{
  // Stop timer here
}

or use a standard dialog

DialogResult dialogResult = MessageBox.Show("Are you sure you want to stop the timer?", "Stop timer", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
  // Stop timer here
}

EDIT: Changed confirmationForm.Show to confirmationForm.ShowDialog.

Upvotes: 3

2GDev
2GDev

Reputation: 2466

You can add a property on the 2nd Form to receive the Timer of first Form :

    private Timer timer;
    public frmModal(Timer tmrref)
    {
        this.timer = tmrref;
        InitializeComponent();
    }

Then when you show the Modal Form you can pass the timer :

frmModal frm = new frmModal( this.timer1);
frm.ShowDialog();

and on button click of the modal form simply stop the timer :

timer.Stop();

Upvotes: 1

De ruige
De ruige

Reputation: 347

Declare a Public Boolean property in the modal form. From the main form you use a loop UNTIL the boolean value is True

While not MyVal = true
   ModalForm.show
End While

Using this method, you can also start or stop your timer from the main and the modal form. You'll get the point :)

Greetings!

Upvotes: 0

Shai
Shai

Reputation: 25595

Declare a Boolean property in the 2nd form called bStopTimer

If the user pressed "Yes", set bStopTimer to True else, False

When you close the child form, check childForm.bStopTimer and act according to it.

Upvotes: 0

Related Questions