Results 1 to 4 of 4

Thread: [RESOLVED] Accessing label property in controls collection

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    165

    Resolved [RESOLVED] Accessing label property in controls collection

    Was dynamically adding a row of buttons each with label beneath in VB6 using control arrays. No arrays in VB.net, so it seemed a neat approach just to add direct to the controls collection, eg.

    For cnt = 1 To NumButt
    .Controls.Add(New Button) : cptr = cptr + 1
    With .Controls(cptr)
    .Width = bwidth
    .Height = bheight
    .left = etc etc
    End With
    Next cnt
    etc etc, similar code to add NumButt labels

    This seems pretty neat and works well, I can also set text and background images. Unfortunately, in just one case I need to set TextAlign of a label and it seems that one can only access those properties that are common to all controls in the controls collection, I can't see how to access those specific to a label, even though I can see in quickwatch that it is a label.

    Is there a way of doing it or do I have to rethink the entire approach?

    Alternatively, any way of ensuring all labels get created with TextAlign of MiddleCenter by default or better still copy all properties from an existing control?

    Cheers for any answers.
    Last edited by xoggoth; May 30th, 2007 at 11:37 AM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Accessing label property in controls collection

    First off, control arrays exist in .NET

    They aren't called control arrays though, they are just arrays like any other array. The only difference between VB6 and .NET is that the controls in the array can't have the same name, and you create them dynamically...

    that being said, you can only access properties of the Control class since that is what you are casting everything to by using the controls collection..

    you could just cast the control to the correct type and access its specific properties that way...


    Code:
    if typeof .Controls(cptr) is Label then
        DirectCast(.Controls(cptr),Label).TextAlign = ContentAlignment.MiddleRight
    elseif typeof .controls(cptr) is Button then
        'DO SPECIFIC BUTTON STUFF HERE
    end if

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Accessing label property in controls collection

    There's no point adding the new control to the Controls collection, then getting it back and setting its properties. Create the control, set its properties first, then add it to the collection, e.g.:
    vb.net Code:
    1. Dim btn As Button
    2.  
    3. For i As Integer = 1 To buttonCount Step 1
    4.     btn = New Button
    5.  
    6.     'Set properties of btn here.
    7.  
    8.     Me.Controls.Add(btn)
    9. Next i
    That way you are already accessing the object via a variable of the correct type so there's no casting required.

    Also, I can see from your code that you are using nested With blocks. While it's legal that is very bad practice.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    165

    Re: Accessing label property in controls collection

    Those work! Cheers.

Posting Permissions

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



Click Here to Expand Forum to Full Width