dcsimg
Results 1 to 7 of 7

Thread: Is there a way to inject names into code?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Is there a way to inject names into code?

    Code:
    For i As Integer = 0 To StoreDevicesComputer.Count() - 1
       Dim labelname As String = "lblS" & i
       PingRange.labelname.Text = "Online"
       PingRange.lblS1.ForeColor = Color.Green
    Next
    Is it possible to do something like this? PingRange is a form.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,285

    Re: Is there a way to inject names into code?

    use the forms controls collection

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Is there a way to inject names into code?

    This code doesn't make sense!
    In words: Inside the For Next you are creating new String variable during each iteration with a name that gets the of the iteration. After that you setting the text and color of a control, however it is always the same control set to same setting!
    On the second look, do intent to use that string-variable as a control name (lblS...)?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Is there a way to inject names into code?

    Quote Originally Posted by opus View Post
    This code doesn't make sense!
    In words: Inside the For Next you are creating new String variable during each iteration with a name that gets the of the iteration. After that you setting the text and color of a control, however it is always the same control set to same setting!
    On the second look, do intent to use that string-variable as a control name (lblS...)?
    That's because you misunderstood what I tried to illustrate. It's part of a bigger loop, but what I tried to ask/show here is, if I can have controls it sweeps to change the control name in the code as it goes one by one.

    In other words on the pingrange form I have many labels lblS1, lblS2, lblS3 ... lblS8

    Code:
    For Each StoreDevice As String In StoreDevicesComputer
                    If OnOff(StoreDevice, True) Then
                        For i As Integer = 0 To StoreDevicesComputer.Count() - 1
                            Dim labelname As String = "lblS" & i
                            PingRange.lblS1.Text = "Online"
                            PingRange.lblS1.ForeColor = Color.Green
                        PingRange.lblS1.Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
                        Next
    
                    Else
                        PingRange.lblS1.Text = "Offline"
                        PingRange.lblS1.ForeColor = Color.Red
                        PingRange.lblS1.Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
                    End If
                Next
    I know the above doesn't work do exactly what I want, but I hope you're trying to understand what I've got or I am trying to do.

    Essentially that function goes through certain devices which are in a list.

    I have lblD1 through lblD8 for device names and right next to them in same numbering I have lblS1 through lblS8 for the online/offline results.
    Last edited by a_ahmed; Jul 3rd, 2014 at 07:29 PM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Is there a way to inject names into code?

    Okay I figured it out with 'control arrays'. Not sure if this is the best or most efficient way but it worked, open to suggestions on how to improve this:

    Code:
    Dim StoreDevicesComputer() As String = {ipRouter.Text, ipDtTimer.Text, ipDVR.Text, ipPOSpart1.Text & ipPOSpart2.Text, ipMWS.Text, ipTWSCustom1.Text & ipTWSCustom2.Text}
    \Dim DeviceLabels() As Label = {PingRange.lblD1, PingRange.lblD2, PingRange.lblD3, PingRange.lblD4, PingRange.lblD5, PingRange.lblD6, PingRange.lblD7, PingRange.lblD8}
    \Dim ResultLabels() As Label = {PingRange.lblS1, PingRange.lblS2, PingRange.lblS3, PingRange.lblS4, PingRange.lblS5, PingRange.lblS6, PingRange.lblS7, PingRange.lblS8}
    If Not ipRouter.Text = "" Then
      Me.Cursor = Cursors.WaitCursor
        For Each StoreDevice As String In StoreDevicesComputer
          For r As Integer = 0 To StoreDevicesComputer.Count()
            If OnOff(StoreDevice, True) Then
                ResultLabels(r).Text = "Online"
                ResultLabels(r).ForeColor = Color.Green
                ResultLabels(r).Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
            Else
                ResultLabels(r).Text = "Offline"
                ResultLabels(r).ForeColor = Color.Red
                ResultLabels(r).Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
            End If
        Next
      Next
    PingRange.Show()
    Me.Cursor = Cursors.Arrow

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Is there a way to inject names into code?

    Simple example.
    Code:
    For i As Integer = 1 To 3
        ' the name of lable we want ( lblS1, lblS2, lblS3 )
        Dim labelname As String = "lblS" & i.ToString
        ' the lable control on form named PingRange
        Dim label As Label = DirectCast(PingRange.Controls(labelname), Label)
        ' make changes to this control
        label.Text = "Online"
        label.ForeColor = Color.Green
        label.Font = New Font(label.Font, FontStyle.Bold)
    Next
    Last edited by Edgemeal; Jul 3rd, 2014 at 08:03 PM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Is there a way to inject names into code?

    ^Very cool

    This is what my final solution was but I think I'll implement your above into the code

    Code:
    Private Sub CheckAll_Click(sender As Object, e As EventArgs) Handles CheckAll.Click
            Dim POSPingResults As New PingRange()
            Dim StoreDevicesLabels() As String = {"Router: ", "DT: ", "DVR: ", "POS: ", "MWS: ", "TWS: "}
            Dim StoreDevicesIPs() As String = {ipRouter.Text, ipDtTimer.Text, ipDVR.Text, _
                                                    ipPOSpart1.Text & ipPOSpart2.Text, ipMWS.Text, _
                                                    ipTWSCustom1.Text & ipTWSCustom2.Text}
            Dim DeviceLabels() As Label = {PingRange.lblD1, PingRange.lblD2, PingRange.lblD3, _
                                           PingRange.lblD4, PingRange.lblD5, PingRange.lblD6, _
                                           PingRange.lblD7, PingRange.lblD8, PingRange.lblD9}
            Dim ResultLabels() As Label = {PingRange.lblS1, PingRange.lblS2, PingRange.lblS3, _
                                           PingRange.lblS4, PingRange.lblS5, PingRange.lblS6, _
                                           PingRange.lblS7, PingRange.lblS8, PingRange.lblS9}
            If Not ipRouter.Text = "" Then
                Me.Cursor = Cursors.WaitCursor
    
                For r As Integer = 0 To StoreDevicesIPs.Count() - 1
                    If OnOff(StoreDevicesIPs(r), True) Then
                        DeviceLabels(r).Text = StoreDevicesLabels(r)
                        ResultLabels(r).Text = "Online"
                        ResultLabels(r).ForeColor = Color.Green
                        ResultLabels(r).Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
                    Else
                        DeviceLabels(r).Text = StoreDevicesLabels(r) & " [ " & StoreDevicesIPs(r) & " ]:"
                        ResultLabels(r).Text = "Offline"
                        ResultLabels(r).ForeColor = Color.Red
                        ResultLabels(r).Font = New Font(PingRange.lblS1.Font, FontStyle.Bold)
                    End If
                Next
                For r As Integer = StoreDevicesIPs.Count() To DeviceLabels.Count() - 1
                    DeviceLabels(r).Visible = False
                    ResultLabels(r).Visible = False
                Next
                PingRange.lblTitle.Text = "Default Devices on [" & storeID & "]" & vbCrLf & "Ranged Ping Results"
                PingRange.lblTitle.ForeColor = Color.White
                PingRange.lblTitle.BackColor = Color.Black
                PingRange.Dock = DockStyle.Bottom
                PingRange.Height = ResultLabels(StoreDevicesIPs.Count()).Bottom + PingRange.CloseMe.Height
                PingRange.Show()
                Me.Cursor = Cursors.Arrow
            End If
        End Sub
    It worked out awesome just as I wanted it. Output is gorgeous and gets the results. The earlier for loop was a loop within a loop which just looped too much lol and ended with the final deviceIP only.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width