Solved Trying to update form1 with a timer control

November 14, 2012 at 12:11:17
Specs: XP\7

I am trying to update or reload a windows form based on a timer, but so far I have not had any luck. Below is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Ping pingClass = new Ping();
PingReply pingReply = pingClass.Send("10.209.44.123");
label4.Text = (pingReply.RoundtripTime.ToString());
//+ "ms");
label5.Text = (pingReply.Status.ToString());

if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
this.pictureBox1.Load("greenLAT.png");

if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
this.pictureBox1.Load("yellowLAT.png");

if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
this.pictureBox1.Load("redLAT.png");

ToolTip tt = new ToolTip();
tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");

timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();

}

private void timer1_Tick(object sender, EventArgs e)
{

Invalidate();
//need to update or reload the windows form


}


}
}



See More: Trying to update form1 with a timer control

Report •


#1
November 14, 2012 at 15:26:19

label4.Text = (pingReply.RoundtripTime.ToString());
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)

Why are you converting a number into a string and back into a number multiple times?

Anyways, your window is being repainted every second-ish, but since the data never changes, you probably don't notice the redrawing.

How To Ask Questions The Smart Way


Report •

#2
November 19, 2012 at 09:21:57
✔ Best Answer

The code below is now doing what I'd like for it to do except I would like to store every 10 rtt's and then display an average of the 10 rtt's within the tool tip. Any help is appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{

private Ping pingClass;
public Form1()
{
InitializeComponent();
pingClass = new Ping();
}

public void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();

}

private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("Timeout!");
RefreshPing();

}

private void RefreshPing()
{
PingReply pingReply = pingClass.Send("10.209.123.123");
label4.Text = (pingReply.RoundtripTime.ToString());
//+ "ms");
label5.Text = (pingReply.Status.ToString());

if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
this.pictureBox1.Load("greenLAT.png");

if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
this.pictureBox1.Load("yellowLAT.png");

if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
this.pictureBox1.Load("redLAT.png");

ToolTip tt = new ToolTip();
tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");
Refresh();
}

}
}


Report •

Related Solutions


Ask Question