Results 1 to 9 of 9

Thread: Dynamic Labels [RESOLVED]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Resolved Dynamic Labels [RESOLVED]

    I have searched this forum for hours for an answer to this question. There are many posts that are related to this but not exactly what i need to know...
    What i am trying to do is after making labels dynamically how can i change the caption/text of each label indipendently from a button? The name of the dynamic labels is always changing and that info is retrieved from an xml file that can be changed so nothing can be static except for the button that initializes that label.text change...


    here is the code i used to create the dynamic labels.

    Code:
            Dim lbl As New Label
            Me.Controls.Add(lbl)
            With lbl
                .Name = ChangingVariable
                .Parent = Me.Image1
                .BackColor = Color.Transparent
                .BringToFront()
                .Top = 0
                .Left = 0
                .Width = 25
                .Height = 55
            End With
    Last edited by Halisco; Apr 23rd, 2008 at 05:40 PM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dynamic Labels

    Loop through the form's Control collection, until you find the one that you want, then you can re-set its caption.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic Labels

    Quote Originally Posted by techgnome
    Loop through the form's Control collection, until you find the one that you want, then you can re-set its caption.

    -tg

    I'm not sure exactly what you mean. Can you please give me an example?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dynamic Labels

    roughly speaking, like this:

    Code:
    for each ctl as control in yourform.Controls
      If ctl.NAme = "Some control's name" Then
          ctl.caption = "Set your new caption
      end 
    next
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic Labels

    Quote Originally Posted by techgnome
    roughly speaking, like this:

    Code:
    for each ctl as control in yourform.Controls
      If ctl.NAme = "Some control's name" Then
          ctl.caption = "Set your new caption
      end 
    next

    Thanks.... That doesnt work but i figured it out....

    Code:
     For Each ControlShowing In Image1.Controls
                On Error Resume Next
                If TypeOf ControlShowing Is Label Then
                    If ControlShowing.Name = "VolShow" Then
                        ' DO NOTHING
                        ControlShowing.Text = "YES"
                    Else
                        ControlShowing.Text = " NO"
                    End If
                End If
            Next ControlShowing

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dynamic Labels

    Good... it wasn't supposed to work. It was supposed to show the basics of ripping through the controls.

    There's a problem with your code by they way....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic Labels

    Quote Originally Posted by techgnome
    Good... it wasn't supposed to work. It was supposed to show the basics of ripping through the controls.

    There's a problem with your code by they way....

    -tg

    Whats wrong with it? Its working....

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Dynamic Labels

    1. Turn Option Strict ON. It's not just helping your program run faster but also helping your write better code.
    2. Do not use On Error Resume Next. Use Try/Catch block instead. But generally, you should avoid exceptions as much as you can.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic Labels

    Quote Originally Posted by stanav
    1. Turn Option Strict ON. It's not just helping your program run faster but also helping your write better code.
    2. Do not use On Error Resume Next. Use Try/Catch block instead. But generally, you should avoid exceptions as much as you can.

    Thankyou very much,

    I am just learning .NET I'm liking it so far. allot less code then VB6

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