1

How can I use the method ChangeText in my static method timer_Elapsed?

public Load()
{
    InitializeComponent();

    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;

    // I can't transfer parameters here
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    timer.Start();
}

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    //Its underlined in red. I need a object reference?
    ChangeText(); 
}

public void ChangeText()
{
    label1.Text = label1.Text + ".";
}
Carsten
  • 11,287
  • 7
  • 39
  • 62
Karl_Schuhmann
  • 1,272
  • 5
  • 17
  • 37

6 Answers6

5

I don't see any reason why timer_Elapsed should be static. So simply remove it.

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    ChangeText(); //Its not underlined anymore, you have an object reference
}

Another way would be to make ChangeText static. But that won't work since you want to set a Label's Text, so you need an instance of the Form anyway.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

As first, your method (timer_Elapsed) could not me static, in order to use an instance property (label1)

There is an other problem in your code: Timer create an other thread, an most of windows control properties can be modified only by UI thread. Your code will throw a CrossThreadException. In order to resolve your problem , you should modify your code with this:

if(this.InvokeRequired) {
   BeginInvoke(
       new MethodInvoker(delegate { label.Text+="."; }));
} else {
    label.Text+="."; 
}

Regards

stefano m
  • 4,094
  • 5
  • 28
  • 27
  • 2
    Ooh, good spot - an easier fix for this, though, would be to use a `System.Windows.Forms.Timer` (since the example looks like winforms), which would call back on the correct thread automatically – Marc Gravell Mar 07 '13 at 09:49
  • ty for this! (it was an InvalidOperationException) – Karl_Schuhmann Mar 07 '13 at 09:55
1

Make ChangeText a static method.

public static void ChangeText()
Breavyn
  • 2,232
  • 16
  • 29
1

You cannot call instance methods in static ones without creating an instance first. You have to create an instance of the class this method belongs to. like below:

var instance = new Load();
instance.ChangeText();  

Update: As other answers suggested, you should reconsider defining timer_Elapsed as static.

Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • since the timer is started by the `Load` constructor, this would be a very bad idea; how many do you want? – Marc Gravell Mar 07 '13 at 09:35
  • @MarcGravell I know this is not a good idea. Just simply answered OP's question and updated my answer with a warning. – Kamyar Mar 07 '13 at 09:36
1

Only static methods are called from a static method, Either make your ChangeText() method to static or make your time_Elapsed method to non-static

Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28
  • the ElapsedEventHandler need a static method so i cant change it. The text from a label is changed in the ChangeText method, when i make it to static i cant access to the label – Karl_Schuhmann Mar 07 '13 at 09:42
1

Hi Can you try like below:

public Load()
{
    InitializeComponent();

    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;

    // I can't transfer parameters here
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    timer.Start();
}
 private delegate void ChangeLabel();
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var ChangeLabel = new ChangeLabel(ChangeText);
            this.BeginInvoke(ChangeLabel);

        }
        private void ChangeText()
        {
            label1.Text = label1.Text + ".";
        }
Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28