1

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;

Mario
  • 2,942
  • 1
  • 25
  • 38
Katherina
  • 2,153
  • 7
  • 26
  • 34

2 Answers2

2

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?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • hi thanks to the reply, my code is as simple like the one you have given.. can you explain please the line... Visible = false; whos getting visible = false? the timer? sorry im getting confused .. i just switch from vb6 to C# – Katherina May 03 '11 at 06:17
  • `Visible` is a property of `Form1` class. You can also do `this.Visible = false;`, but this would be kind of redundant. – Alex Aza May 03 '11 at 06:21
  • @Alex - I think @Katherina's question is a good argument for using `this.`; it's evident that it relates to `this` but just like sensibly named variables, it should help other programmers see what's going on. – Kirk Broadhurst May 03 '11 at 06:51
  • @Kirk - It is easy to tell that Visible is a property of the current class, if you follow Microsoft naming guidelines. It is in Pascal notation. Fields and local variables should have follow camel notation. – Alex Aza May 03 '11 at 06:58
  • @Katherina - How come I gave you working code sample, but you marked as an answer the answer that had nothing to do with your problem? Just trying to understand how that answer helped you. – Alex Aza May 16 '11 at 06:06
-1

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.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Not sure I understand how this is relevant. Fist of all, it is not mentioned in the question if control is accessed as public field or property. Secondly, how does this affect timer functionality? Do you mean that if timer is accessed via property then it will function as expected, but if it is accessed via field it will not stop? – Alex Aza May 03 '11 at 06:38
  • i would like to see some answers to this also.. i dont know yet how .net reacts to this kinds of codings. thanks for the raise up alex – Katherina May 03 '11 at 06:58