-
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!
-
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
-
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>
-
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!
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
dunfiddlin
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!
-
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
-
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?
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
szlamany
@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.
-
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...
-
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 ;)
-
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 ;)
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
stanav
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.
-
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. :)
-
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.
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
dday9
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?
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
Stuw
Thankyou. What do i define sender as?
Ignore me, too early!
-
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.
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
szlamany
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
-
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
-
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?
-
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
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
dbasnett
If ODID has the name of a control then
Code:
Me.Controls(ODID).BackColor = Color.Red
Thankyou!!
-
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
-
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...
-
Re: How to obtain the name of the dynamically created label your clicking on
No never so that dosent help me
-
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.
-
Re: How to obtain the name of the dynamically created label your clicking on
Quote:
Originally Posted by
szlamany
...If not - then you should.
Agreed. There is a link in my signature that might interest you. Actually there are a couple ;)
-
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.