[RESOLVED] Problem with link labels
Hi guys,
I am creating several link labels at runtime. I am using the following code:
VB Code:
'Get all questions with matching qID and create linklabels for each.
For j As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
If PhotoPicker.arrQuestion(j).qID = questionnaireID Then
lnk_Question = New LinkLabel
With lnk_Question
.Text = PhotoPicker.arrQuestion(j).question.ToString
.Top = PhotoPicker.pnl_Questions.Top + (count * 35)
.Width = PhotoPicker.pnl_Questions.Width - 25
.Height = 30
.Left = PhotoPicker.pnl_Questions.Left + 10
.Parent = PhotoPicker.pnl_Questions
AddHandler lnk_Question.Click, AddressOf lnk_Question_click
End With
PhotoPicker.pnl_Questions.Controls.Add(lnk_Question)
count += 1
End If
Next
Now the link labels display fine but when i click on a link label i need to compare the text of the link label to an array and return the element number. Now when i actually click on any link label it returns the text of the last link label created.
Can anybody tell me why it is doing this and how i can get around it?
Thanks in advance for any help
:afrog:
(I am using .net compact framework -> but i dont think that will make a difference to how to tackle this problem.)
Re: Problem with link labels
Please show your code in the button click event
Re: Problem with link labels
There is no button, its a link label
Re: Problem with link labels
I'll wager that in your Click event handler you're referring to the 'lnk_Question' variable, which of course is going to refer to the last control created. As for every event handler, the 'sender' argument is a reference to the object that raised the event. As always, it will be type Object, so you need to cast it as type LinkLabel.
Also, the Click event of the LinkLabel class should rarely be used. That event is raised when the user clicks anywhere within the region of the control, which is rarely what you want from a LinkLabel. You should be handling the LinkClicked event.
Re: Problem with link labels
Quote:
Originally Posted by Kimmy4
There is no button, its a link label
Opss... Sorry. I meant the LinkLabel click event
Re: Problem with link labels
Here's my code for the .click event.
Thanks both for the reply.
VB Code:
Private Sub lnk_Question_click(ByVal sender As Object, ByVal e As System.EventArgs)
For k As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
If lnk_Question.Text = PhotoPicker.arrQuestion(k).question Then
MsgBox(lnk_Question.Text.ToString)
End If
Next
End Sub
I understand what you're saying jmcilhinney. I just dont know how to change the code i already have accordingly. Could either of you provide some examples for me.
Thanks again.
Re: Problem with link labels
There's no need to add a string variable like I've done but it would be something like this I think in order to cast which link label it refers to:
VB Code:
Private Sub lnk_Question_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myString As String = DirectCast(sender, LinkLabel).Text
For k As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
If myString = PhotoPicker.arrQuestion(k).question Then
MsgBox(myString)
End If
Next
End Sub
I haven't tried this but you should get the idea of how the cast works and get it to hopefully work from this.
Re: Problem with link labels
Thanks i'll try it out and let you know how i get on.
:wave:
Re: Problem with link labels
Works perfectly. I didn't assign to a string first. I just put
VB Code:
If directCast(sender, linklabel).text = PhotoPicker.arrQuestion(k).question Then
Thanks for the help. :thumb:
Re: [RESOLVED] Problem with link labels
That's fine, I was just using the string variable as an example.
:thumb:
Re: [RESOLVED] Problem with link labels
How can I use PhotoPicker in my website to upload photos, any idea?