Link to home
Start Free TrialLog in
Avatar of mihu332
mihu332

asked on

Visual Basic 2008 - Organise TabControl, using information from a textfile

Hi,
I am looking to add a new function to my software.
I have the following list ( servers.txt ). The format of the file is :

COLGATE ASNBTT1 172.22.34.12
BLENDAMED ASNBTT2 172.22.34.13
COLGATE ASNBTT3 172.22.34.14
HUGOBOSS ASNBTT4 172.22.34.15

The first input represents customer name, the next is hostname, and the next is IP address.
When the button SortBtn is clicked, the software must grab the info from the text file and arrange it in TabControl1 in the following way:

CUSTOMERS should be used as TABS, so in this case, TabControl1 should have the following TABS : COLGATE , BLENDAMED , HUGOBOSS

Inside each TAB, there must be created buttons, and the caption of each button must be the server name. For example inside the COLGATE TAB, there should be the following buttons button1.text = "ASNBTT1"
button2.text = "ASNBTT3"

The buttons inside the tabs must be backcolor=color.red ( by default ).
When the user will right click on the tab ( for example on COLGATE ), the software will ping ( by ip, not by hostname ) all the servers in the respective TAB, and the responsive servers will be marked as backcolor=color.green

I know it is a lot to ask but if someone can give any information it would help me a lot.
thank you
Avatar of bromy2004
bromy2004
Flag of Australia image

I'm working on something at the moment.

What sort of numbers are we working with?
How many Customers?
How many Servers per Customer?
Insert the Attached into a new Module

call the Module from Form1_Load

Form1 must have an Empty Tab Control and a Progress Bar
Imports PingInfo = System.Net.NetworkInformation
Imports IPAddress = System.Net.IPAddress

Module Module1
  Public Structure Customer
    Public CustomerName As String
    Public ServerName As String
    Public ipAdd As String
  End Structure
  Private WithEvents PingButton As Button
  Public CustomerComplete(0 To 0) As Customer
  Sub GetServerList()
    Const Delim As String = " "

    Dim CustomerData As New Collection
    Dim CustomerUnique As New Collection

    Dim CustTemp
    Dim i As Long
    Dim j As Long
    Dim TabMade As TabPage
    Dim Tab As TabPage
    Dim Label1 As Label
    Dim LabelLast As Label
    Dim Loc As Point
    '    Public PingButton As Button


    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Documents and Settings\NathanB\Desktop\servers.txt")
      MyReader.TextFieldType = FileIO.FieldType.Delimited
      MyReader.SetDelimiters(Delim)

      'Read Data
      While Not MyReader.EndOfData
        Try
          CustomerData.Add(MyReader.ReadLine)
        Catch ex As  _
        Microsoft.VisualBasic.FileIO.MalformedLineException
          MsgBox("Line " & ex.Message & _
          "is not valid and will be skipped.")
        End Try
      End While

      'Get unique Customers
      'For Tabs
      ReDim CustomerComplete(0 To CustomerData.Count - 1)
      For i = 0 To CustomerData.Count - 1
        Try
          CustTemp = Split(CustomerData(i + 1), Delim)

          CustomerComplete(i).CustomerName = CustTemp(0)
          CustomerComplete(i).ServerName = CustTemp(1)
          CustomerComplete(i).ipAdd = CustTemp(2)

          CustomerUnique.Add(CustTemp(0), CustTemp(0))
        Catch ex As Exception

        End Try
      Next i
    End Using

    'Insert Tabs
    For i = 1 To CustomerUnique.Count
      TabMade = New TabPage
      With TabMade
        .Text = CustomerUnique(i)
        .Name = CustomerUnique(i)
      End With
      Form1.TabControl1.TabPages.Insert(i - 1, TabMade)
    Next i

 


    'Insert Servers
    For Each Tab In Form1.TabControl1.TabPages
      For i = LBound(CustomerComplete) To UBound(CustomerComplete)
        If CustomerComplete(i).CustomerName = Tab.Text Then

          Label1 = New Label
          With Label1
            .Text = CustomerComplete(i).ServerName
            .BackColor = Color.Orange
            Loc.X = 5
            Loc.Y = 5
            .Location = Loc
            If Not LabelLast Is Nothing Then
              Loc.X = 5
              Loc.Y = LabelLast.Height + 7
              .Location = Loc
            End If
          End With

          Tab.Controls.Add(Label1)
          LabelLast = Label1
        End If
      Next i
      LabelLast = Nothing


      'Set up Ping Button
      PingButton = New Button
      'Insert Ping Button
      With PingButton
        'Location
        Loc.X = 20
        Loc.Y = Form1.TabControl1.Height - 80
        .Location = Loc

        'Size
        Loc.X = Form1.TabControl1.Width - 60
        Loc.Y = 40
        .Size = Loc
        .Text = "Ping"
      End With


      Tab.Controls.Add(PingButton)
      AddHandler PingButton.Click, AddressOf PingButton_Click

    Next Tab

  End Sub


  Private Sub PingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingButton.Click
    Dim NewPing As PingInfo.Ping
    Dim ipAdd As IPAddress
    Dim ipReply As PingInfo.PingReply
    Dim tbLabel
    Dim i As Long
    Dim TotalControls As Long
    Dim CurrentControl As Long
    Dim ProgressStep As Double

    For Each tbLabel In sender.Parent.Controls
      If TypeOf tbLabel Is Label Then
        TotalControls = TotalControls + 1
        ProgressStep = 100 / TotalControls / 2
      End If
    Next tbLabel


    For Each tbLabel In Sender.Parent.Controls
      If TypeOf tbLabel Is Label Then
        CurrentControl = CurrentControl + 1
        For i = 0 To CustomerComplete.Count - 1
          If tbLabel.Text = CustomerComplete(i).ServerName Then
            ipAdd = IPAddress.Parse(CustomerComplete(i).ipAdd)

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            NewPing = New PingInfo.Ping
            ipReply = NewPing.Send(ipAdd)

            If ipReply.Status = PingInfo.IPStatus.Success Then
              tbLabel.BackColor = Color.Green
            Else
              tbLabel.BackColor = Color.Red
            End If

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            Form1.Update()

          End If

        Next i
      End If
    Next tbLabel

    Form1.ProgressBar1.Value = 0
  End Sub
End Module

Open in new window

Avatar of mihu332
mihu332

ASKER

Hi
Thank you for helping me out.

Number of servers / customers are random.

Regarding the code :

Should I call the module like this ?    
-----
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetServerList()

    End Sub
----

Also VB is warning me that LabelLast is beeing used before it has been assigned a value.
Please advise.
Thanks !
Yep, Calling the Module that way should be fine.

Also the LabelLast should say that (Another one should as well, cant think what one though)
Its because i used that to work out the Location of the second Server.

If Not LabelLast Is Nothing Then
  Loc.X = 5
  Loc.Y = LabelLast.Height + 7
  .Location = Loc
End If

So if we created a Label already, use that as a measurement.
otherwise start from the top left
Avatar of mihu332

ASKER

Now I understand.
But still, there is no action performed by the app.
Nothing happens.
This is how my form looks like.
Do I need to add anything else ?
Thanks
Public Class form1

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetServerList()

    End Sub

 
End Class

Open in new window

Have you put a Tab Control and a ProgressBar on it?
And make sure you remove the first tab that gets added automatically.
Have you put a Tab Control and a ProgressBar on it?
And make sure you remove the first tab that gets added automatically.
Avatar of mihu332

ASKER

Yep
TabControl1 & ProgressBar1 are added to form1

The tab control has no tabs.
Have you set Form1 for startup?
Avatar of mihu332

ASKER

Yes

Form1 is starting when I run the software, but nothing happens, I get no error.
Ive tested it.
the only time it doesn't do anything is if the txt file doesn't Exist

have you changed it to your file Location?

    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\Nathan\Desktop\Servers.txt")
Avatar of mihu332

ASKER

If the file doesn't exist, the program won't start it gives error, so the file is ok.
Can you please attach archive with your project file ?
Thank you
Avatar of mihu332

ASKER

HEY

The problem was in the servers.txt file !
Now everything works !
Great job.
I will perform further testing, then I will accept your solution.
God bless you :)
Is the File always in the same Location?
If it is Make the Following Changes
1. In Module1 add "Public fPath As String" Below "Public CustomerComplete(0 To 0) As Customer"
2. In Module1, Change the File Path to "fPath"
3. In Form1 change to the Following Code

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    fPath = "C:\Users\Nathan\Desktop\servers.txt"

    'Check File
    If My.Computer.FileSystem.FileExists(fPath) Then
      GetServerList()
    Else
      MsgBox("File is Missing" & vbNewLine & fPath)
    End If
  End Sub

The File Path will be entered on Form1_Load

NOTE:
If the File path is varies frequently,
i could edit the Code to include a File Select Dialog

Let Me know.
Form1 Code
Public Class Form1


  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    fPath = "C:\Users\Nathan\Desktop\servers.txt"

    'Check File
    If My.Computer.FileSystem.FileExists(fPath) Then
      GetServerList()
    Else
      MsgBox("File is Missing" & vbNewLine & fPath)
    End If
  End Sub
End Class

Open in new window

Module1 Code
Imports PingInfo = System.Net.NetworkInformation
Imports IPAddress = System.Net.IPAddress

Module Module1
  Public Structure Customer
    Public CustomerName As String
    Public ServerName As String
    Public ipAdd As String
  End Structure
  Private WithEvents PingButton As Button
  Public CustomerComplete(0 To 0) As Customer
  Public fPath As String
  Sub GetServerList()
    Const Delim As String = " "

    Dim CustomerData As New Collection
    Dim CustomerUnique As New Collection

    Dim CustTemp
    Dim i As Long
    Dim TabMade As TabPage
    Dim Tab As TabPage
    Dim Label1 As Label
    Dim LabelLast As Label
    Dim Loc As Point
    '    Public PingButton As Button


    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fpath)
      MyReader.TextFieldType = FileIO.FieldType.Delimited
      MyReader.SetDelimiters(Delim)

      'Read Data
      While Not MyReader.EndOfData
        Try
          CustomerData.Add(MyReader.ReadLine)
        Catch ex As  _
        Microsoft.VisualBasic.FileIO.MalformedLineException
          MsgBox("Line " & ex.Message & _
          "is not valid and will be skipped.")
        End Try
      End While

      'Get unique Customers
      'For Tabs
      ReDim CustomerComplete(0 To CustomerData.Count - 1)
      For i = 0 To CustomerData.Count - 1
        Try
          CustTemp = Split(CustomerData(i + 1), Delim)

          CustomerComplete(i).CustomerName = CustTemp(0)
          CustomerComplete(i).ServerName = CustTemp(1)
          CustomerComplete(i).ipAdd = CustTemp(2)

          CustomerUnique.Add(CustTemp(0), CustTemp(0))
        Catch ex As Exception

        End Try
      Next i
    End Using

    'Insert Tabs
    For i = 1 To CustomerUnique.Count
      TabMade = New TabPage
      With TabMade
        .Text = CustomerUnique(i)
        .Name = CustomerUnique(i)
      End With
      Form1.TabControl1.TabPages.Insert(i - 1, TabMade)
    Next i




    'Insert Servers
    For Each Tab In Form1.TabControl1.TabPages
      For i = LBound(CustomerComplete) To UBound(CustomerComplete)
        If CustomerComplete(i).CustomerName = Tab.Text Then

          Label1 = New Label
          With Label1
            .Text = CustomerComplete(i).ServerName
            .BackColor = Color.Orange
            Loc.X = 5
            Loc.Y = 5
            .Location = Loc
            If Not LabelLast Is Nothing Then
              Loc.X = 5
              Loc.Y = LabelLast.Height + 7
              .Location = Loc
            End If
          End With

          Tab.Controls.Add(Label1)
          LabelLast = Label1
        End If
      Next i
      LabelLast = Nothing


      'Set up Ping Button
      PingButton = New Button
      'Insert Ping Button
      With PingButton
        'Location
        Loc.X = 20
        Loc.Y = Form1.TabControl1.Height - 80
        .Location = Loc

        'Size
        Loc.X = Form1.TabControl1.Width - 60
        Loc.Y = 40
        .Size = Loc
        .Text = "Ping"
      End With


      Tab.Controls.Add(PingButton)
      AddHandler PingButton.Click, AddressOf PingButton_Click

    Next Tab

  End Sub


  Private Sub PingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingButton.Click
    Dim NewPing As PingInfo.Ping
    Dim ipAdd As IPAddress
    Dim ipReply As PingInfo.PingReply
    Dim tbLabel
    Dim i As Long
    Dim TotalControls As Long
    Dim CurrentControl As Long
    Dim ProgressStep As Double

    For Each tbLabel In sender.Parent.Controls
      If TypeOf tbLabel Is Label Then
        TotalControls = TotalControls + 1
        ProgressStep = 100 / TotalControls / 2
      End If
    Next tbLabel


    For Each tbLabel In Sender.Parent.Controls
      If TypeOf tbLabel Is Label Then
        CurrentControl = CurrentControl + 1
        For i = 0 To CustomerComplete.Count - 1
          If tbLabel.Text = CustomerComplete(i).ServerName Then
            ipAdd = IPAddress.Parse(CustomerComplete(i).ipAdd)

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            NewPing = New PingInfo.Ping
            ipReply = NewPing.Send(ipAdd)

            If ipReply.Status = PingInfo.IPStatus.Success Then
              tbLabel.BackColor = Color.Green
            Else
              tbLabel.BackColor = Color.Red
            End If

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            Form1.Update()

          End If

        Next i
      End If
    Next tbLabel

    Form1.ProgressBar1.Value = 0
  End Sub
End Module

Open in new window

Avatar of mihu332

ASKER

There is a new problem now.
Even if I insert 10 servers from one customer, it will only show me the first 2 servers.
What could be the reason for that ?
ah. ill re-test.
I've only used the Sample data you provided.
I also haven't tested if you have too many Labels for the Form. without making a Huge form to start.
Avatar of mihu332

ASKER

I guess that if they are a lot of servers it could be a scroll or something....
I really don't know what to say :(
Ive updated the Module1 Code
The form fit 5 Servers per Column.
So this will make a new column.

The only problem you will have is if there is more than 10 Servers per Customer.
Imports PingInfo = System.Net.NetworkInformation
Imports IPAddress = System.Net.IPAddress

Module Module1
  Public Structure Customer
    Public CustomerName As String
    Public ServerName As String
    Public ipAdd As String
  End Structure
  Private WithEvents PingButton As Button
  Public CustomerComplete(0 To 0) As Customer
  Public fPath As String
  Sub GetServerList()
    Const Delim As String = " "

    Dim CustomerData As New Collection
    Dim CustomerUnique As New Collection

    Dim CustTemp
    Dim i As Long

    Dim TabMade As TabPage
    Dim Tab As TabPage

    Dim Label1 As Label

    Dim Loc As Point
    Dim LocNew As Point

    Dim LabelCount As Integer

    'Public PingButton As Button

    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fpath)
      MyReader.TextFieldType = FileIO.FieldType.Delimited
      MyReader.SetDelimiters(Delim)

      'Read Data
      While Not MyReader.EndOfData
        Try
          CustomerData.Add(MyReader.ReadLine)
        Catch ex As  _
        Microsoft.VisualBasic.FileIO.MalformedLineException
          MsgBox("Line " & ex.Message & _
          "is not valid and will be skipped.")
        End Try
      End While

      'Get unique Customers
      'For Tabs
      ReDim CustomerComplete(0 To CustomerData.Count - 1)
      For i = 0 To CustomerData.Count - 1
        Try
          CustTemp = Split(CustomerData(i + 1), Delim)

          CustomerComplete(i).CustomerName = CustTemp(0)
          CustomerComplete(i).ServerName = CustTemp(1)
          CustomerComplete(i).ipAdd = CustTemp(2)

          CustomerUnique.Add(CustTemp(0), CustTemp(0))
        Catch ex As Exception

        End Try
      Next i
    End Using

    With Form1
      'Resize Form
      Loc.X = 300
      Loc.Y = 300
      .Size = Loc

      With .TabControl1
        'Move Tab Control
        Loc.X = 12
        Loc.Y = 12
        .Location = Loc
        'Resize Tab Control
        Loc.X = 260
        Loc.Y = 208
        .Size = Loc
      End With

      With .ProgressBar1
        'Move Progress bar
        Loc.X = 12
        Loc.Y = 226
        .Location = Loc
        'Resize Progress Bar
        Loc.X = 260
        Loc.Y = 24
        .Size = Loc
      End With

    End With


    'Insert Tabs
    For i = 1 To CustomerUnique.Count
      TabMade = New TabPage
      With TabMade
        .Text = CustomerUnique(i)
        .Name = CustomerUnique(i)
      End With
      Form1.TabControl1.TabPages.Insert(i - 1, TabMade)
    Next i

    'Insert Servers
    For Each Tab In Form1.TabControl1.TabPages
      LocNew.X = 5
      LocNew.Y = 5
      For i = LBound(CustomerComplete) To UBound(CustomerComplete)
        If CustomerComplete(i).CustomerName = Tab.Text Then
          LabelCount = LabelCount + 1
          Label1 = New Label
          With Label1
            .Text = CustomerComplete(i).ServerName
            .BackColor = Color.Orange

            Loc = LocNew

            If ((LabelCount) / 5) = Math.Round((LabelCount) / 5) Then
              'New Row
              LocNew.X = LocNew.X + 105
              LocNew.Y = 5
            Else
              LocNew.X = LocNew.X
              LocNew.Y = LocNew.Y + 27
            End If

            .Location = Loc
          End With

          Tab.Controls.Add(Label1)
        End If
      Next i

      LabelCount = 0

      'Set up Ping Button
      PingButton = New Button
      'Insert Ping Button
      With PingButton
        'Location
        Loc.X = 5
        Loc.Y = Form1.TabControl1.Height - 70
        .Location = Loc

        'Size
        Loc.X = Form1.TabControl1.Width - 20
        Loc.Y = 40
        .Size = Loc
        .Text = "Ping"
      End With


      Tab.Controls.Add(PingButton)
      AddHandler PingButton.Click, AddressOf PingButton_Click

    Next Tab

  End Sub


  Private Sub PingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingButton.Click
    Dim NewPing As PingInfo.Ping
    Dim ipAdd As IPAddress
    Dim ipReply As PingInfo.PingReply
    Dim tbLabel
    Dim i As Long
    Dim TotalControls As Long
    Dim CurrentControl As Long
    Dim ProgressStep As Double

    For Each tbLabel In sender.Parent.Controls
      TotalControls = 0
      ProgressStep = 0
      If TypeOf tbLabel Is Label Then
        TotalControls = TotalControls + 1
        ProgressStep = 100 / TotalControls / 2
      End If
    Next tbLabel


    For Each tbLabel In Sender.Parent.Controls
      If TypeOf tbLabel Is Label Then
        CurrentControl = CurrentControl + 1
        For i = 0 To CustomerComplete.Count - 1
          If tbLabel.Text = CustomerComplete(i).ServerName Then
            ipAdd = IPAddress.Parse(CustomerComplete(i).ipAdd)

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            NewPing = New PingInfo.Ping
            ipReply = NewPing.Send(ipAdd)

            If ipReply.Status = PingInfo.IPStatus.Success Then
              tbLabel.BackColor = Color.Green
            Else
              tbLabel.BackColor = Color.Red
            End If

            With Form1.ProgressBar1
              .Value = .Value + ProgressStep
              .Update()
            End With

            Form1.Update()

          End If

        Next i
      End If
    Next tbLabel

    Form1.ProgressBar1.Value = 0
  End Sub
End Module

Open in new window

Avatar of mihu332

ASKER

Hi,
I have customers even with 50 servers. even more....
Isn't there any solution for this problem ? :(
I'll look into it and re-post.
How is the Size?
is the Label size too big or small?
and would it be OK to increase the overall size?
Avatar of mihu332

ASKER

I work with the following configuration now :

Form size : 788, 414
TabControl Size : 754, 334

Based on this dimensions, the screen can hold 70 labels.

<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
<> <> <> <> <> <> <>
ASKER CERTIFIED SOLUTION
Avatar of bromy2004
bromy2004
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mihu332

ASKER

Thank you for your help.
God bless you !
Avatar of mihu332

ASKER

Very able technician.!