Results 1 to 11 of 11

Thread: [RESOLVED] Problem with link labels

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [RESOLVED] Problem with link labels

    Hi guys,

    I am creating several link labels at runtime. I am using the following code:
    VB Code:
    1. 'Get all questions with matching qID and create linklabels for each.
    2.         For j As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
    3.             If PhotoPicker.arrQuestion(j).qID = questionnaireID Then
    4.                 lnk_Question = New LinkLabel
    5.                 With lnk_Question
    6.                     .Text = PhotoPicker.arrQuestion(j).question.ToString
    7.                     .Top = PhotoPicker.pnl_Questions.Top + (count * 35)
    8.                     .Width = PhotoPicker.pnl_Questions.Width - 25
    9.                     .Height = 30
    10.                     .Left = PhotoPicker.pnl_Questions.Left + 10
    11.                     .Parent = PhotoPicker.pnl_Questions
    12.                     AddHandler lnk_Question.Click, AddressOf lnk_Question_click
    13.                 End With
    14.                 PhotoPicker.pnl_Questions.Controls.Add(lnk_Question)
    15.                 count += 1
    16.             End If
    17.         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


    (I am using .net compact framework -> but i dont think that will make a difference to how to tackle this problem.)
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    Re: Problem with link labels

    Please show your code in the button click event

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with link labels

    There is no button, its a link label
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with link labels

    Here's my code for the .click event.
    Thanks both for the reply.

    VB Code:
    1. Private Sub lnk_Question_click(ByVal sender As Object, ByVal e As System.EventArgs)
    2.  
    3.         For k As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
    4.             If lnk_Question.Text = PhotoPicker.arrQuestion(k).question Then
    5.                 MsgBox(lnk_Question.Text.ToString)
    6.             End If
    7.         Next
    8.     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.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Private Sub lnk_Question_click(ByVal sender As Object, ByVal e As System.EventArgs)
    2.  
    3.         Dim myString As String = DirectCast(sender, LinkLabel).Text
    4.  
    5.         For k As Integer = 0 To PhotoPicker.arrQuestion.Length - 2
    6.             If myString = PhotoPicker.arrQuestion(k).question Then
    7.                 MsgBox(myString)
    8.             End If
    9.         Next
    10.     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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with link labels

    Thanks i'll try it out and let you know how i get on.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with link labels

    Works perfectly. I didn't assign to a string first. I just put
    VB Code:
    1. If directCast(sender, linklabel).text = PhotoPicker.arrQuestion(k).question Then

    Thanks for the help.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  10. #10
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] Problem with link labels

    That's fine, I was just using the string variable as an example.

    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  11. #11
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Re: [RESOLVED] Problem with link labels

    How can I use PhotoPicker in my website to upload photos, any idea?

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