Results 1 to 3 of 3

Thread: adding linklabel at runtime withevents

  1. #1
    urvi_bhal is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2005
    Posts
    14
    Reputation
    99

    adding linklabel at runtime withevents

    hi,

    i have created one form in which when user log in that form will show all the accounts related to that user and that will shown in link label like this
    account 1

    account 2

    account 3

    Now my question is when user clicks account 1 it will show detail for account 1 and when user clicks account 2 it will show detail for account 2

    it may be 3 accounts or it may be 5 or any more.

    i have added linklabel like this
    Code:
      
    DimWithEvents l As LinkLabel /* global variable 
     
    form_load event*/
    Do
    l = New LinkLabel()
    Me.Controls.Add(l)
    l.Text = "Account" & " " & dr1.Item("countrow") & " " & "from" & " " & dr1.Item("column1")
    l.Visible = True
    l.Enabled = True
    l.AutoSize = True
    l.Top = t + 40
    t = l.Top
    LoopWhile dr1.Read
     
    PrivateSub l_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles l.Click
    Dim f AsNew form1()
    f.Show()
    Me.Hide()
    EndSub
    problem is when i press account 3 than and than l_click event works otherwise for account 1 and account 2 it is not working... It means for last control it is adding during runtime event is called otherwise not. I want to call event for all the accounts what should i do? I hope answer will come fast....

  2. #2
    TechGnome is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    May 2005
    Posts
    896
    Reputation
    216
    I don't see anywhere you AddHandler to capture the click events.....
    Ahhh.... I see it now....
    You have the click event only linked to l. Each time l=new LinkLabel() fires, it resets the reference... which means as is, the l_Click will only capture the last instance of l that gets created.

    See if this works for you:
    Code:
      
    DimWithEvents l As LinkLabel /* global variable 
     
    form_load event*/
    Me.SuspendLayout 'Prevent time consuming repaints
    Do
      l = New LinkLabel()
      Me.Controls.Add(l)
      l.Text = "Account" & " " & dr1.Item("countrow") & " " & "from" & " " & dr1.Item("column1")
      l.name = "L" & dr1.Item("countrow").ToString
      l.Visible = True
      l.Enabled = True
      l.AutoSize = True
      l.Top = t + 40
      t = l.Top
      AddHandler Me.Controls("L" & dr1.Item("countrow").ToString).Click, AddressOf l_Click
    LoopWhile dr1.Read
    Me.ResumeLayout 'force the repaint to show the updated form
    
    
    
    PrivateSub l_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
    Dim f AsNew form1()
    'In here you can check the name of Sender (besure to cast it first) to see which link label was clicked.
      f.Show()
      Me.Hide()
    EndSub
    I think that's right...
    The only thing I'm not 100% sure of is this line:
    Me.Controls("L" & dr1.Item("countrow").ToString).Click

    I think you may have to cast that as well, to get to the click even member.

    Tg

  3. #3
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    It should read :
    Code:
    AddHandler l.Click, AddressOf l_Click
    — Stephen Paszt

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking