none
How to address ontherun created control by name RRS feed

  • Question

  • Question is placed in the remarks as the code flows:

    REM First creating five Panels giving them different names For cnt = 0 To 4 Dim XPanel As New Panel With XPanel .BackColor = Color.Yellow .Size = New System.Drawing.Size(50, 40) .Location = New System.Drawing.Point((cnt * 60) + 1, 10) .Name = "XPanel_" & cnt End With Me.Controls.Add(XPanel) Next cnt REM Then creating one Label.... Dim XLabel As New Label With XLabel .BackColor = Color.Red .Size = New System.Drawing.Size(45, 15) .Location = New System.Drawing.Point(10, 10) .Name = "XLbl:" & Now.Minute End With REM ...wanting to be placed on the panel named "XPanel_2" as follows... XPanel_2.Controls.Add(XLabel) '<<<...but this is not correct/not working way 'How to place this Label on the any choosen panel (using its name)

    How to convert or use the string (panel name) to be used as Control?

    T.



    Thursday, November 16, 2017 1:58 PM

Answers

  • Try this.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            REM First creating five Panels giving them different names
            For cnt = 0 To 4
                Dim XPanel As New Panel
                With XPanel
                    .BackColor = Color.Yellow
                    .Size = New System.Drawing.Size(50, 40)
                    .Location = New System.Drawing.Point((cnt * 60) + 1, 10)
                    .Name = "XPanel_" & cnt
                End With
                Me.Controls.Add(XPanel)
            Next cnt
    
            REM Then creating one Label.... 
            Dim XLabel As New Label
            With XLabel
                .BackColor = Color.Red
                .Size = New System.Drawing.Size(45, 15)
                .Location = New System.Drawing.Point(10, 10)
                .Name = "XLbl:" & Now.Minute
            End With
            REM ...wanting to be placed on the panel named "XPanel_2"  as follows...
            'XPanel_2.Controls.Add(XLabel)      '<<<...but this is not correct/not working way
            Dim p As Panel = Me.Controls.OfType(Of Panel).Where(Function(c) c.Name = "XPanel_2").FirstOrDefault
            If p IsNot Nothing Then
                p.Controls.Add(XLabel)
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Frank L. Smith Thursday, November 16, 2017 2:06 PM
    • Marked as answer by Tibro Thursday, November 16, 2017 2:23 PM
    Thursday, November 16, 2017 2:05 PM
    Moderator

All replies

  • Try this.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            REM First creating five Panels giving them different names
            For cnt = 0 To 4
                Dim XPanel As New Panel
                With XPanel
                    .BackColor = Color.Yellow
                    .Size = New System.Drawing.Size(50, 40)
                    .Location = New System.Drawing.Point((cnt * 60) + 1, 10)
                    .Name = "XPanel_" & cnt
                End With
                Me.Controls.Add(XPanel)
            Next cnt
    
            REM Then creating one Label.... 
            Dim XLabel As New Label
            With XLabel
                .BackColor = Color.Red
                .Size = New System.Drawing.Size(45, 15)
                .Location = New System.Drawing.Point(10, 10)
                .Name = "XLbl:" & Now.Minute
            End With
            REM ...wanting to be placed on the panel named "XPanel_2"  as follows...
            'XPanel_2.Controls.Add(XLabel)      '<<<...but this is not correct/not working way
            Dim p As Panel = Me.Controls.OfType(Of Panel).Where(Function(c) c.Name = "XPanel_2").FirstOrDefault
            If p IsNot Nothing Then
                p.Controls.Add(XLabel)
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Frank L. Smith Thursday, November 16, 2017 2:06 PM
    • Marked as answer by Tibro Thursday, November 16, 2017 2:23 PM
    Thursday, November 16, 2017 2:05 PM
    Moderator
  • Hi Karen,

    Wow, what a speedy response:)

    Thank you very much for your hint!

    With a enormous Point counter like yours,
    I wonder if you use your skills and knowledge only to daily help people like me...
    or are you working on some coding projects....in your free time(?)  ...Do you have any free time?...considering your points counter... :)))))

    T. 

    Thursday, November 16, 2017 2:33 PM
  • I work full time developing enterprise systems for the web and sometimes work on legacy desktop applications.  My current workload keeps me from being here as often as in the past yet do monitor these forums throughout the day when possible.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 16, 2017 3:08 PM
    Moderator
  • Here is one last idea then I'm busy at work. Consider moving your code out of the form into a Class.

    Create controls in the class, pass back a list of the panels

    Public Class FormDemo
        Private PanelList As List(Of Panel)
        Private Sub FormDemo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New FormOperations
            PanelList = ops.CreatePanels(Me, "Hello")
        End Sub
    End Class

    So now PanelList has your panels to work on. Here is the class.

    Public Class FormOperations
        ''' <summary>
        ''' Create our controls and pass the panel list back to the caller
        ''' </summary>
        ''' <param name="control">The parent control or form to create the controls on</param>
        ''' <param name="LabelText">Text for label</param>
        ''' <returns></returns>
        Public Function CreatePanels(ByVal control As Control, ByVal LabelText As String) As List(Of Panel)
            Dim panelList As New List(Of Panel)
            For cnt = 0 To 4
                Dim XPanel As New Panel
                With XPanel
                    .BackColor = Color.Yellow
                    .Size = New Size(50, 40)
                    .Location = New Point((cnt * 60) + 1, 10)
                    .Name = "XPanel_" & cnt
                End With
    
                control.Controls.Add(XPanel)
                panelList.Add(XPanel)
    
            Next
    
            '
            ' Add label to desire panel
            '
            panelList.OfType(Of Panel).Where(Function(c) c.Name = "XPanel_2").FirstOrDefault.Controls.Add(New Label With
                                  {
                                    .BackColor = Color.Red,
                                    .Size = New Size(45, 15),
                                    .Location = New Point(10, 10),
                                    .Name = "XLbl:" & Now.Minute,
                                    .Text = LabelText,
                                    .ForeColor = Color.White
                                  })
    
            Return panelList
    
        End Function
    
    End Class
    
    The class was coded using Framework 4.5 so if you were to try this and using a lesser version of the Framework you could run into compile issues.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 16, 2017 3:16 PM
    Moderator
  • Aha! So solving problems of the others is your free time pleasure:)

    I understand and appreciate!

    A was allways fascinated by people like you, spending huge amout of time reading these "forum nightmares of the strangers" and helping them out for ...pleasure (Doesnt matter if it is even payed..  I am talking about inner state).

    I also Thank You for improved solution for my problem, but my Panel problem shown here was just simplified and extracted from 20 000 line source code and It will be used back in much "complicated" way :)

    Thank You!
    Have a nice day!

    T.

    Thursday, November 16, 2017 6:53 PM
  • Finally, my project is really not under Framework 4.5, but 4.  ...and that code is not accepted there...

    So I figured out, that one of the solutions under Framework4 is to place those 5 panels into an array of panels

    and address them from the position in the array.

    T.

    Friday, November 17, 2017 4:31 PM