Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

dynamically adding labels

How can I assign  a value into my label that i dynamically created at runtime?

lblreadings.Text = indvalue
'creates the active reading boxes
        For intloop = 0 To ActiveCOM.Count - 1
            positionindex = 1
            lblreadings = New System.Windows.Forms.Label
            If intloop = 0 Then
                intx = 290
                inty = 85
            End If
            With lblreadings
                .Name = "lblreadings" & positionindex
                .Size = New System.Drawing.Size(100, 50)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "----4096-----"
                .Visible = True
            End With
 
            Me.Controls.Add(lblreadings)
            inty = inty + 50
            positionindex = positionindex + 1
        Next

Open in new window

Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

If you mean you wish to change the Text of the label in a separate routine, use this....

    Me.Controls("lblreadings1").Text = "new text"

BTW, that will only work for VB.Net 2005 and later. In VB.Net 2003, you need to use the Controls.Find method....

    Me.Controls.Find("lblreadings1", True)(0).Text = "new text"

Wayne
Avatar of cmdolcet

ASKER

Thanks again I think it will work my only additional question is this every label I create down in the for loop has a name of "lblreadings" and the positionindex which I increment say for example I wanted to use the above code how would I assign the positionindex to a value

so that
'creates the active reading boxes
        For intloop = 0 To ActiveCOM.Count - 1
            positionindex = 1
            lblreadings = New System.Windows.Forms.Label
            If intloop = 0 Then
                intx = 290
                inty = 85
            End If
            With lblreadings
                .Name = "lblreadings" & positionindex
                .Size = New System.Drawing.Size(100, 50)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "----4096-----"
                .Visible = True
            End With
 
            Me.Controls.Add(lblreadings)
            inty = inty + 50
            positionindex = positionindex + 1
        Next

Open in new window

Me.Controls("lblreadings" & positionindex).Text = "new text"
>>how would I assign the positionindex to a value

I don't understand this bit, so if I'm wrong, can you try explaining again?
what im trying to do is this my positionindex is declared in the CreateRuntimeControls() sub
what i need to do is show each individual text box an individual value in the TmrIndicatorBitValues_Tick sub
Private Sub CreateRuntimeControls()
        Dim chkbox As System.Windows.Forms.CheckBox
        Dim lblposition As System.Windows.Forms.Label
        Dim lblreadings As System.Windows.Forms.Label
        Dim btnbuttons As System.Windows.Forms.Button
        Dim intloop, intloop2, intx, inty As Integer
        Dim positionindex As Integer = 1
 
        'creates active comports on screen checkbox
        For intloop = 1 To _ActiveUSBPORTs
            chkbox = New System.Windows.Forms.CheckBox
            If intloop = 1 Then
                intx = 20
                inty = 85
            End If
            With chkbox
                .Name = "chkbox" & intloop
                .Size = New System.Drawing.Size(81, 17)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "Active Port"
                .Visible = True
 
            End With
            Me.Controls.Add(chkbox)
            inty = inty + 50
        Next
 
        'create position label on screen
        For intloop = 0 To ActiveCOM.Count - 1
            lblposition = New System.Windows.Forms.Label
            If intloop = 0 Then
                intx = 160
                inty = 85
            End If
            With lblposition
                .Name = "lblposition" & positionindex
                .Size = New System.Drawing.Size(81, 17)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "Position " & positionindex
                .Visible = True
            End With
            Me.Controls.Add(lblposition)
            inty = inty + 50
            positionindex = positionindex + 1
        Next
 
 
        'creates the active reading boxes
        For intloop = 0 To ActiveCOM.Count - 1
            positionindex = 1
            lblreadings = New System.Windows.Forms.Label
            If intloop = 0 Then
                intx = 290
                inty = 85
            End If
            With lblreadings
                .Name = "lblreadings" & positionindex
                .Size = New System.Drawing.Size(100, 50)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "----4096-----"
                .Visible = True
            End With
 
            Me.Controls.Add(lblreadings)
            inty = inty + 50
            positionindex = positionindex + 1
        Next
 
 
        'creates the active buttons
        For intloop = 0 To ActiveCOM.Count - 1
            positionindexbutton = 1
            btnbuttons = New System.Windows.Forms.Button
            If intloop = 0 Then
                intx = 460
                inty = 85
            End If
            With btnbuttons
                .Name = "btnbutton" & positionindexbutton
                .Size = New System.Drawing.Size(100, 30)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "View Information"
                .Visible = True
            End With
            AddHandler btnbuttons.Click, AddressOf Button_Click
            Me.Controls.Add(btnbuttons)
            inty = inty + 50
            positionindexbutton = positionindexbutton + 1
        Next
 
end sub
 
 
 
 Private Sub TmrIndicatorBitValues_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrIndicatorBitValues.Tick
        Dim position As Integer
        TmrIndicatorBitValues.Enabled = False
 
        For COMIndex = 0 To CurrCOMPort.Count - 1
            IndicatorPort = CurrCOMPort.Item(COMIndex)
            IndicatorPort.DiscardInBuffer()
            IndicatorCOMSetup.RequestProbeValues()
            position = COMIndex
 
            Select Case Position
                Case 0
                    Me.Controls("lblreadings1").Text = indvalue
                    '*********************************Sample loop for 1 inch indicator*******************************
                    'Checks to make sure 1 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1" Then
                        If preValue < indvalue And intarrayindex > 0 And Not preValue = 0 Then
                            tmrStageMove.Enabled = True
                        ElseIf (preValue - indvalue) = 1 Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 2
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf _BadResponse = True Then
                            tmrStageMove.Enabled = True
                            _BadResponse = False
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue & COMIndex
                            tmrStageMove.Enabled = True
                        End If
                        TmrIndicatorBitValues.Enabled = True
                    End If
 
                    '**********************Sample loop for 1/2 inch indicator**************************************
                    'Checks to make sure 1/2 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1/2" Then
                        If _BadResponse = True Then
                            _BadResponse = False
                            TmrIndicatorBitValues.Enabled = True
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue < indvalue And intarrayindex > 0 Then
                            If intarrayindex > 1270 Then
                                _ReadingArrayList3(intarrayindex) = indvalue
                                preValue = indvalue
                                tmrStageMove.Enabled = True
                                TmrIndicatorBitValues.Enabled = True
                            Else
                                TmrIndicatorBitValues.Enabled = True
                            End If
 
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
                            TmrIndicatorBitValues.Enabled = True
                        End If
                    End If
 
 
 
                Case 1
                    '*********************************Sample loop for 1 inch indicator*******************************
                    'Checks to make sure 1 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1" Then
                        If preValue < indvalue And intarrayindex > 0 And Not preValue = 0 Then
                            tmrStageMove.Enabled = True
                        ElseIf (preValue - indvalue) = 1 Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 2
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf _BadResponse = True Then
                            tmrStageMove.Enabled = True
                            _BadResponse = False
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue & COMIndex
                            tmrStageMove.Enabled = True
                        End If
                        TmrIndicatorBitValues.Enabled = True
                    End If
 
                    '**********************Sample loop for 1/2 inch indicator**************************************
                    'Checks to make sure 1/2 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1/2" Then
                        If _BadResponse = True Then
                            _BadResponse = False
                            TmrIndicatorBitValues.Enabled = True
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue < indvalue And intarrayindex > 0 Then
                            If intarrayindex > 1270 Then
                                _ReadingArrayList3(intarrayindex) = indvalue
                                preValue = indvalue
                                tmrStageMove.Enabled = True
                                TmrIndicatorBitValues.Enabled = True
                            Else
                                TmrIndicatorBitValues.Enabled = True
                            End If
 
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
                            TmrIndicatorBitValues.Enabled = True
                        End If
                    End If
 
                Case 2
                    '*********************************Sample loop for 1 inch indicator*******************************
                    'Checks to make sure 1 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1" Then
                        If preValue < indvalue And intarrayindex > 0 And Not preValue = 0 Then
                            tmrStageMove.Enabled = True
                        ElseIf (preValue - indvalue) = 1 Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 2
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf _BadResponse = True Then
                            tmrStageMove.Enabled = True
                            _BadResponse = False
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue & COMIndex
                            tmrStageMove.Enabled = True
                        End If
                        TmrIndicatorBitValues.Enabled = True
                    End If
 
                    '**********************Sample loop for 1/2 inch indicator**************************************
                    'Checks to make sure 1/2 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1/2" Then
                        If _BadResponse = True Then
                            _BadResponse = False
                            TmrIndicatorBitValues.Enabled = True
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue < indvalue And intarrayindex > 0 Then
                            If intarrayindex > 1270 Then
                                _ReadingArrayList3(intarrayindex) = indvalue
                                preValue = indvalue
                                tmrStageMove.Enabled = True
                                TmrIndicatorBitValues.Enabled = True
                            Else
                                TmrIndicatorBitValues.Enabled = True
                            End If
 
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
                            TmrIndicatorBitValues.Enabled = True
                        End If
                    End If
 
                Case 3
                    'Me.lblindicatorvalue4.Text = indvalue
                    '*********************************Sample loop for 1 inch indicator*******************************
                    'Checks to make sure 1 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1" Then
                        If preValue < indvalue And intarrayindex > 0 And Not preValue = 0 Then
                            tmrStageMove.Enabled = True
                        ElseIf (preValue - indvalue) = 1 Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 2
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf _BadResponse = True Then
                            tmrStageMove.Enabled = True
                            _BadResponse = False
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue & COMIndex
                            tmrStageMove.Enabled = True
                        End If
                        TmrIndicatorBitValues.Enabled = True
                    End If
 
                    '**********************Sample loop for 1/2 inch indicator**************************************
                    'Checks to make sure 1/2 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1/2" Then
                        If _BadResponse = True Then
                            _BadResponse = False
                            TmrIndicatorBitValues.Enabled = True
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue < indvalue And intarrayindex > 0 Then
                            If intarrayindex > 1270 Then
                                _ReadingArrayList3(intarrayindex) = indvalue
                                preValue = indvalue
                                tmrStageMove.Enabled = True
                                TmrIndicatorBitValues.Enabled = True
                            Else
                                TmrIndicatorBitValues.Enabled = True
                            End If
 
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
                            TmrIndicatorBitValues.Enabled = True
                        End If
                    End If
 
                Case 4
                    'Me.lblindicatorvalue5.Text = indvalue
                    '*********************************Sample loop for 1 inch indicator*******************************
                    'Checks to make sure 1 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1" Then
                        If preValue < indvalue And intarrayindex > 0 And Not preValue = 0 Then
                            tmrStageMove.Enabled = True
                        ElseIf (preValue - indvalue) = 1 Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 2
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf _BadResponse = True Then
                            tmrStageMove.Enabled = True
                            _BadResponse = False
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue & COMIndex
                            tmrStageMove.Enabled = True
                        End If
                        TmrIndicatorBitValues.Enabled = True
                    End If
 
                    '**********************Sample loop for 1/2 inch indicator**************************************
                    'Checks to make sure 1/2 Inch table values collected are al unique and spaced out even
                    If strTestType = "Indicator 1/2" Then
                        If _BadResponse = True Then
                            _BadResponse = False
                            TmrIndicatorBitValues.Enabled = True
                            Exit Sub
                        ElseIf preValue = indvalue Then
                            indvalue = indvalue - 1
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
 
                        ElseIf preValue < indvalue And intarrayindex > 0 Then
                            If intarrayindex > 1270 Then
                                _ReadingArrayList3(intarrayindex) = indvalue
                                preValue = indvalue
                                tmrStageMove.Enabled = True
                                TmrIndicatorBitValues.Enabled = True
                            Else
                                TmrIndicatorBitValues.Enabled = True
                            End If
 
                        Else
                            _ReadingArrayList3(intarrayindex) = indvalue
                            preValue = indvalue
                            tmrStageMove.Enabled = True
                            TmrIndicatorBitValues.Enabled = True
                        End If
                    End If
 
            End Select
        Next
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of parul11183
parul11183

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
cmdolcet,

How does the selected comment answer your question?? It doesn't even have anything to do with VB.Net!

Wayne