-2

I use vb.net 2008 to build an application. I have a form with 50 textboxes containing the ip address of the remote device, if the ping device is good then the background color of the textbox is green, otherwise red. I use the If function as follows:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If My.Computer.Network.Ping(TextBox1.Text) Then
            TextBox1.BackColor = Color.Green
        Else
            TextBox1.BackColor = Color.Red
        End If

      If My.Computer.Network.Ping(TextBox2.Text) Then
            TextBox2.BackColor = Color.Green
        Else
            TextBox2.BackColor = Color.Red
        End If
      .
      .’ The if functions of the Textbox3 to the Textbox49
      .
      If My.Computer.Network.Ping(TextBox50.Text) Then
            TextBox50.BackColor = Color.Green
        Else
            TextBox50.BackColor = Color.Red
        End If

    End Sub
End Class

For 50 textboxes, I have to use 50 If functions as this makes the code very long, can you help me to shorten code with For ... Next loop. Thank you for your help.

  • This looks like a job for a function. – Carcigenicate May 25 '17 at 02:48
  • 1
    Write the loop that you think you should and then we can help you fix it if it doesn't work. If you don't know how to write a `For` or `For Each` loop, do some reading on the subject. We're here to help with specific issues, not to write your code for you. If you haven't tried to write the code then you haven't encountered a specific issue yet. – jmcilhinney May 25 '17 at 02:57

1 Answers1

0

Thank you for your help and advice, I solved my problem with this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tbArray() As TextBox = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10, TextBox11, TextBox12, TextBox13, TextBox14, TextBox15, TextBox16, TextBox17, TextBox18, TextBox19, TextBox20, TextBox21, TextBox22, TextBox23, TextBox24, TextBox25, TextBox26, TextBox27, TextBox28, TextBox29, TextBox30, TextBox31, TextBox32, TextBox33, TextBox34, TextBox35, TextBox36, TextBox37, TextBox38, TextBox39, TextBox40, TextBox41, TextBox42, TextBox43, TextBox44, TextBox45, TextBox46, TextBox47, TextBox48, TextBox49, TextBox50}
    Dim i As Short
    For i = 0 To 49

        If My.Computer.Network.Ping(tbArray(i).Text) Then
            tbArray(i).BackColor = Color.Green
        Else
            tbArray(i).BackColor = Color.Red
        End If
    Next
End Sub

But another problem arises, the entire code takes about 35 seconds, I think it's a pretty long time. Is there a way to reduce code execution time?

  • If you asked that as a question, it would be a duplicate of [Modifying VB.Net program that uses normal ping method to use Async ping method](https://stackoverflow.com/q/30400219/1115360). – Andrew Morton May 25 '17 at 08:47