adding linklabel at runtime withevents

urvi_bhal

Member
Joined
Jun 29, 2005
Messages
14
Programming Experience
Beginner
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
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] l [/size][size=2][color=#0000ff]As[/color][/size][size=2] LinkLabel /* global variable [/size]
 
form_load event*/
[size=2][size=2][color=#0000ff]Do[/color][/size][/size]
[size=2][size=2]l = [/size][size=2][color=#0000ff]New[/color][/size][size=2] LinkLabel()[/size][/size]
[size=2][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add(l)[/size][/size]
[size=2][size=2]l.Text = "Account" & " " & dr1.Item("countrow") & " " & "from" & " " & dr1.Item("column1")[/size][/size]
[size=2][size=2]l.Visible = [/size][size=2][color=#0000ff]True[/color][/size][/size]
[size=2][size=2]l.Enabled = [/size][size=2][color=#0000ff]True[/color][/size][/size]
[size=2][size=2]l.AutoSize = [/size][size=2][color=#0000ff]True[/color][/size][/size]
[size=2][size=2]l.Top = t + 40[/size][/size]
[size=2][size=2]t = l.Top[/size][/size]
[size=2][size=2][color=#0000ff]Loop[/color][/size][size=2] [/size][size=2][color=#0000ff]While[/color][/size][size=2] dr1.Read[/size][/size]
[size=2][/size] 
[size=2][size=2][color=#0000ff][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] l_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] l.Click[/size][/color][/size][/size]
[size=2][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][size=2] f [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] form1()[/size][/color][/size][/size]
[size=2][size=2][color=#0000ff][size=2]f.Show()[/size][/color][/size][/size]
[size=2][size=2][color=#0000ff][size=2][color=#0000ff]Me[/color][/size][size=2].Hide()[/size][/color][/size][/size]
[size=2][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][/color][/size][/size]

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....
 
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:
VB.NET:
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
 
Back
Top