3
Answers

Whats wrong with my code?

Tristen Shaw

Tristen Shaw

12y
1.8k
1
I'm trying to make a label change text and color based on whether my application can detect an active internet connection.

private void lblConnection_Click(object sender, EventArgs e)
        {
            bool ConnCheck = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
            if (ConnCheck == true)
            {
                lblConnection.Text = "Available";
                lblConnection.ForeColor = Color.LimeGreen;
            }
            else
            {
                lblConnection.Text = "Unavailable";
                lblConnection.ForeColor = Color.Red; 
            }//End Conncheck if statement//
        }//End lblConnection Class//


I would also like to make the application watch this connection dynamically and update the label as things change.
Answers (3)
0
Vulpes
NA 98.3k 1.5m 12y
I'd add a Timer to your form and set its Interval property to (say) 500 milliseconds and Enabled property to true.

If you then double-click on it to open up a skeleton handler for the Tick event you can call your label click handler:

private timer1_Tick (object sender, EventArgs e) 
{
   lblConnection_Click(lblConnection, EventArgs.Empty);
}  
Accepted
0
Tristen Shaw
NA 13 17.4k 12y
HAHA! Thank you man!

Works like a charm! :)
0
Tristen Shaw
NA 13 17.4k 12y
The program now checks for the network on runtime and it seems to work well. But I still haven't been able to figure out how to get it to check for this constantly.