Results 1 to 8 of 8

Thread: [RESOLVED] how to use label(x).text with 2008

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    24

    Resolved [RESOLVED] how to use label(x).text with 2008

    I want to change text on certain labels, eg
    label2.text
    label7.text

    what is the syntax to use variable x instead of number in label name
    something like-
    x=2
    label(x).text="hello"
    x=7
    label(x).text="hello"

    I know this does not work, but cant find right way to do it.

    Thanks in advance.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: how to use label(x).text with 2008

    vb Code:
    1. me.controls("label" & x.tostring).text ="hello"

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: how to use label(x).text with 2008

    Code:
    x=2
    Me.Controls("label" & x.ToString()).Text = "hello"

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: how to use label(x).text with 2008

    There are no control arrays in .NET. You can do other things like create an array and add your controls to it and loop through it. You can use the forms controls collection and check the type of control to filter out he controls not being updated. Or you can also search the Forums for other solutions/variations
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    24

    Re: how to use label(x).text with 2008

    Thanks for answering so quick.

  6. #6
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: [RESOLVED] how to use label(x).text with 2008

    extending RobDog888's Post with a sample:

    Code:
    For each Ctl as Label in new Label(){Label1, Label2, Label3}
       Ctl.Text="Hallo"
    next
    Thats a better practice than access Controls by their Name-Property.
    Because the Name-Property may be changed.

    Furthermore it's nogood practice not to name Controls explicitely, and leave them as the IDE did generate (Label1, Label2, Label3).
    Especially if there are more than one Control, especially especially if they are accessed by Code (a tumb, never-changed Caption needs not to be named explicitely).
    Every Control should be named "speaking", like "blStart", "lbEnd" to make clear for what they are used for.
    whats the benefit of beeing rated?


  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] how to use label(x).text with 2008

    Well thats another discussion which has been beaten to death

    Using humgarian notation in .NET is useless since ther are so many controls you can not easily manage the prefixes like you could in VB6. Plus .NET can handle long object names better then VB6 so you can have "MySuperDuperUserControlThatIsntNamedWithHungarianNotation" or such
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: [RESOLVED] how to use label(x).text with 2008

    Ok, the way u name the controls maybe that beaten discussion. But that they have to be named explicitely (according to any convention) - thats not to discuss (i think).
    whats the benefit of beeing rated?


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