Katherina
Katherina

Reputation: 2223

C#.net Stoping a Timer of a form, using the other form?

How can I stop a timer inside..(lets say form1) whem im in form2?

i have tried timer1.enabled = false; before i go to form2

but i dont know why it is still running?

before i got to form2.. i hide the form1 with this.Visible = false;

Upvotes: 1

Views: 1944

Answers (2)

Alex Aza
Alex Aza

Reputation: 78487

Made a test project with two forms. I have a timer and a button on a Form1. Timer is started in Form1 constructor. When you hit the button, the timer stops and new Form2 is open and Form1 is hidden.

Here is the code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        timer1.Enabled = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        Visible = false;
        new Form2().Show();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Text = DateTime.Now.ToString();
    }
}

Timer stops without problems.

Can you provide the code that you have?

Upvotes: 2

Soner Gönül
Soner Gönül

Reputation: 98840

First you should look this article;

Best way to access a control on another form in Windows Forms?

And see @Jon Limjap answer;


Instead of making the control public, you can create a property controlling its visibility:

public boolean ControlIsVisible
{
     get { return control.Visible; }
     set { control.Visible = value; }
}

This creates a proper accessor to that control that won't expose the control's whole set of properties.

Upvotes: -1

Related Questions