0

I have used label 1 to label 75 is it possible to change the label name? example instead of label7 i want to rename the label name as label3 what is the solution?

  • possible duplicate of [how to do rename the label name from 1 to 75 at run time in vb.net?](http://stackoverflow.com/questions/28657090/how-to-do-rename-the-label-name-from-1-to-75-at-run-time-in-vb-net) – Ňɏssa Pøngjǣrdenlarp Feb 28 '15 at 13:50

4 Answers4

1

It works in the form because you are calling Controls which implies Me.Controls, which is a collection of controls immediately inside Me (your form). Once you put the label in a panel, the label is no longer in the form's control collection, rather the panel's control collection. This is because Controls does not go deeper than the control it is called on. You could do this, which would solve your immediate problem:

' replace Panel1 with the name of your panel
Dim lbl As Label = Panel1.Controls("Label" & Data)
ReceivedFrame = ReceivedFrame + 1
lbl.Text = ReceivedFrame

But if the label is moved out of your panel, this breaks. The solution below is somewhat more costly in processing power but I use it everywhere this is not a concern.

Make a new class file in your project, and put this code in it.

Public Module Support

    <Extension()> _
    Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
        Dim result As New List(Of Control)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is T Then result.Add(ctrl)
            result.AddRange(ctrl.ChildControls(Of T)())
        Next
        Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
    End Function

    <Extension()> _
    Public Function ControlByName(ByVal parent As Control, ByVal name As String) As Control
        For Each c As Control In parent.ChildControls
            If c.Name = name Then
                Return c
            End If
        Next
        Return Nothing
    End Function

End Module

It enables you to get all controls inside a control, which includes forms. Then get the control you want by name.

Then you can call it like so:

Dim lbl As Label = Me.ControlByName("Label" & Data)
ReceivedFrame = ReceivedFrame + 1
lbl.Text = ReceivedFrame

This will work as long as the control is somewhere in your form, in any container, or in any container in another container for that matter.

djv
  • 15,168
  • 7
  • 48
  • 72
0
Dim lbl As New Label
lbl.Parent = Panel1
lbl.Text = "Your string"
djv
  • 15,168
  • 7
  • 48
  • 72
Sniper
  • 1,428
  • 1
  • 12
  • 28
0

You can directly call by Name Property of the Label Control, what ever it may reside on the form. If the Label Control resides on a panel and its name is Label1 then to display a value you can call it programmatically as Label1.Text = "Your String Value".

If you want to access the Label Control's properties through an object then

Dim lbl As Label = CType(Label1, Label)
lbl.Text ="Your String Value"

HTH

Sanu
  • 69
  • 2
0

It is a good code he wrote @Verdolino but is a little buggy because if you're control is nested inside of a nested groupbox that's still is nested inside of other control it will break.

but this is a pretty way to do it. This is a recursivity function and it will look into the end of the nested controls.

Function GetControl(OwnerControl as Control,FindControlName as string) as Control
     If OwnerControl.Controls.Count > 0 Then
        '//===>It will loop in each control of the control container.
        For Each iControl as Control in OwnerControl.Control
             GetControl(OwnerControl ,FindControlName)
        Next
     Else
         '//===>this code is when the control is not a container and will check if the name macth
         If OwnerControl.Name.ToUpper =  FindControlName.ToUpper Then
               '//===>TODO HERE:Add here any code if the control was found
               ' you can register some events handler if you want
               ' AddHandler OwnerControl.Click, AddressOf _MyClickEventSub

                Return OwnerControl
         End If
     End If
End Sub

dim txtControl as control = GetControl(Me,"txtName")

If not txtControl Is Nothing Then
  '//===>TODO HERE:if the control was found
End Sub
MrAlex6204
  • 167
  • 7