Results 1 to 28 of 28

Thread: How to obtain the name of the dynamically created label your clicking on

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Question How to obtain the name of the dynamically created label your clicking on

    Hello

    I have labels dynamically created for which i have this code to run when one is double clicked

    Code:
    Private Sub label_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            
        For Each Labelname As Label In Me.Controls.OfType(Of Label)().ToArray
                If Labelname.Name.Substring(0, 3) = ID Then
                    If Labelname.BackColor = Color.Yellow Then
                        openbooking = True
                        Dim f2 As BookingForm
                        f2 = New BookingForm
                        f2.Visible = True
                        openbooking = True
    
                    Else
    
                        If MessageBox.Show("Mark as collected?", " ", _
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) Then
                            Labelname.BackColor = Color.Yellow
    
                        Else
                        End If
                    End If
    
                End If
            Next
    
    
        End Sub
    
    End Class
    I need to set the variable "ID" to the name of the dynamically created label that is clicked on so that it changes its color and the name is then passed to the next form but cant figure how i get it!

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

    Re: How to obtain the name of the dynamically created label your clicking on

    interesting thing about events... not only does it send in the event arguments... but it also sends in the control that invoked the event... so in your event handler, you know the control that is the sender of the event...

    oh sure... it seems like an ordinary object... but that's because all controls inherit from object... and they can be cast to their more specific type if you know what it is...

    -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
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Re: How to obtain the name of the dynamically created label your clicking on

    Use DirectCast to get the sender:
    Code:
    Dim myLable As Label = DirectCast(sender, Label)
    From there to access the control you'd use myLabel.<property>
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to obtain the name of the dynamically created label your clicking on

    That's assuming that you gave the label a name in the first place, of course. You'd be amazed how many people forget!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by dunfiddlin View Post
    That's assuming that you gave the label a name in the first place, of course. You'd be amazed how many people forget!
    That's true, I can't tell you how many times I've created a dynamic control and forgot to name them!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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

    Re: How to obtain the name of the dynamically created label your clicking on

    I'd further point out that we're talking about the .Name property control....
    Dim myLabel as New Label ... that's not a name... that's just the variable...

    yeah... I think we've all been caught by that before... I'll even admit to falling for it more than once...

    -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
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    @tg - I'm curious...

    If you are going to have "centralized" events for handling lots of labels that you added to a form - what is the downfall of not naming them. You access them through the SENDER anyway - right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by szlamany View Post
    @tg - I'm curious...

    If you are going to have "centralized" events for handling lots of labels that you added to a form - what is the downfall of not naming them. You access them through the SENDER anyway - right?
    What if you're wanting the name property. In this case, the OP set the label's name as an "ID". I must admit, I'd rather store that information in the .Tag property, but in this specific case the OP needs the name property. Besides, it's just good practice to name your objects as opposed to letting VS giving them a default name.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    I thought he was adding them dynamically - thus the need for a NAME at runtime is kind of useless - right? The name is for the IDE and you to get along - right?

    And I agree - the TAG property is where you put [edit]additional info about the control[/edit].

    When I did VB.Net coding I always named my LABELS and TEXTBOX to exactly match my FIELD names from tables, since I auto-binded these to datasource dynamically at runtime.

    Personally if I'm going to add a silly label at design time for a prompt on the screen I never really cared if I left the default name of LABEL1 or LABEL123 in place.

    nvm...
    Last edited by szlamany; May 17th, 2013 at 03:25 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: How to obtain the name of the dynamically created label your clicking on

    The Tag is type Object. If you want to use it to store the control's name, that's fine but you'll have to cast it back to string when retrieving it where as if you use the Name property, no casting is needed. Besides, it always feels better when you use the right tool for the job
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    I certainly wasn't saying to put the NAME value in the TAG property!! I was indicating that I would use the TAG for it's purpose of storing additional info about the control.

    Sorry if I was too vague.

    I back out of this thread

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by stanav View Post
    The Tag is type Object. If you want to use it to store the control's name, that's fine but you'll have to cast it back to string when retrieving it where as if you use the Name property, no casting is needed. Besides, it always feels better when you use the right tool for the job
    Yeah, I'm with szlamany. I meant that I'd store the ID value(or any other info like that) in the Tag, not the Name.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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

    Re: How to obtain the name of the dynamically created label your clicking on

    Sorry I misunderstood your idea in the last post... I'm so ashamed of myself for being such a sloppy reader. I'll try to read more carefully next time.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    No problem! We are all talking big-stuff around a little problem of the OP not realizing SENDER is the object already available to serve the need.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by dday9 View Post
    Use DirectCast to get the sender:
    Code:
    Dim myLable As Label = DirectCast(sender, Label)
    From there to access the control you'd use myLabel.<property>
    Thankyou. What do i define sender as?

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by Stuw View Post
    Thankyou. What do i define sender as?
    Ignore me, too early!

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    SENDER come in to you as the PARAMETER of the event function already

    Code:
    Private Sub label_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    You simply use the DirectCast statement to take the "generic" object of SENDER and cast it into the type of object that you are already aware that it must be

    Code:
    Dim myLabel As Label = DirectCast(sender, Label)
    - notice the second parameter of DirectCast? That is the "type" to cast it into - and that would be LABEL in this case.

    Now you have a new reference to the "generic" object that is no longer generic - it is now a specific object.

    Use MyLabel.xxxxx to reference any of the method or properties of the LABEL that was actually clicked.

    So - you never need to know the NAME of the label clicked - you already have the reference you need.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by szlamany View Post
    SENDER come in to you as the PARAMETER of the event function already

    Code:
    Private Sub label_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    You simply use the DirectCast statement to take the "generic" object of SENDER and cast it into the type of object that you are already aware that it must be

    Code:
    Dim myLabel As Label = DirectCast(sender, Label)
    - notice the second parameter of DirectCast? That is the "type" to cast it into - and that would be LABEL in this case.

    Now you have a new reference to the "generic" object that is no longer generic - it is now a specific object.

    Use MyLabel.xxxxx to reference any of the method or properties of the LABEL that was actually clicked.

    So - you never need to know the NAME of the label clicked - you already have the reference you need.
    Thanks, realised later i had paste it into the wrong bit!!

    How do i now change the color of the dynamically created label from a timer? Ive tried this but it dosent accept the name of the label

    Code:
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    
            Timer1.Enabled = False
            Timer1.Stop()
    
            SqlConnection1.Open()
    
            'Enters the sql command to run'
    
            SqlCommand1.CommandText = "SELECT * FROM Equiploans WHERE Equiploans.Dateout = ('" + Date.Today + "') "
    
            'Runs command'
    
            SqlCommand1.ExecuteNonQuery()
    
            'Dims the process that reads the database'
    
            Dim myreader As SqlClient.SqlDataReader
    
            myreader = SqlCommand1.ExecuteReader()
    
            Dim x As Object
    
            'From that row get these values'
    
            For Each x In myreader
    
                Timein = myreader.GetString(6)
    
                Dim ODID As String
    
                ODID = myreader.GetString(0)
    
                item1time = (dateout + " " + Timein)
    
                currentdatetime = Date.Now
    
                If currentdatetime > item1time Then
    
                    MessageBox.Show("Overdue")
    
                    Dim myLabelOD As Object
    
                    myLabelOD.Name = ODID
    
                    myLabelOD.BackColor = Color.Red
    
    
                End If
    
            Next
    
            myreader.Close()
    
            SqlConnection1.Close()
    
        End Sub

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    When you are in the label click - put sender into the TIMER1.TAG

    Timer1.Tag = Sender

    Now when you are in the timer event - you have the SENDER from the original label click

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Im going to have labels which have been created but not necessarily clicked on, is there another way?

  21. #21
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to obtain the name of the dynamically created label your clicking on

    If ODID has the name of a control then

    Code:
            Me.Controls(ODID).BackColor = Color.Red
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  22. #22

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by dbasnett View Post
    If ODID has the name of a control then

    Code:
            Me.Controls(ODID).BackColor = Color.Red
    Thankyou!!

  23. #23

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    Sorry to be a pain but i need to access it from another form too, i thought it would just be this but alas not!

    Code:
    form1.Controls(item1).BackColor = Color.Green
    Item1 being the labels name on form 1

  24. #24
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    Do you ever use the WATCH or QUICK WATCH windows?

    Set a break point where you want to change the color to green and then put Form1 into the Watch window - click the plus sign to open the object - you can examine what's available.

    Or put Forms1.Controls into the Watch window...

    Or open the IMMEDIATE window and do line-by-line attempts.

    It's a nice way for you to see what "is available" in the object you are attempting to access...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  25. #25

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: How to obtain the name of the dynamically created label your clicking on

    No never so that dosent help me

  26. #26
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to obtain the name of the dynamically created label your clicking on

    Why does that not help you? Have you never set a BREAK POINT and run your code and had it stop at that point to examine what is available in the objects you are working with?

    If not - then you should.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  27. #27
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to obtain the name of the dynamically created label your clicking on

    Quote Originally Posted by szlamany View Post
    ...If not - then you should.
    Agreed. There is a link in my signature that might interest you. Actually there are a couple
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  28. #28
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: How to obtain the name of the dynamically created label your clicking on

    I find tracepoints actually very useful as well. That watch window is a lifesaver though too. I don't think people realize how useful it is until they actually fool around with it enough times.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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