Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to set text alignment of dynamically added label where i am getting name of label in string variable one label is dynamically added in panel

VB
Dim instance As New Label
Dim stringVariable As String

on form load event ::

VB
instance.Location = New Point(15, 15)
  instance.Size = New Size(60, 30)
  instance.BorderStyle = BorderStyle.FixedSingle
  instance.AutoSize = False
  instance.Text = "LABEL1"
  Me.Panel1.Controls.Add(instance)

i am getting name by:

VB
for each c as control in Panel1.Controls
    stringVariable=c.name
    next

but its not possible to

VB
Me.Panel1.Controls(stringVariable).textalign   ??????

it does not show text align property
please help
Posted

1 solution

You need to cast the control to a label:
VB
For Each c As Control In Panel1.Controls
    stringVariable = c.name
    Dim l As Label = TryCast(c, Label)
    If l IsNot Nothing Then
        l.TextAlign = ContentAlignment.MiddleCenter
    End If
Next
 
Share this answer
 
Comments
Omkaara 1-Dec-12 5:25am    
@OriginalGriff thankyou very much
OriginalGriff 1-Dec-12 5:27am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900