473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Enabling event handler

I am trying to create an event handler for my link labels. What I want to do is when someone clicks the link then it should open the link in a new browser. But the problem I am having is that I dont know how to add event handler for link labels.
This is what I am trying to do, but I dont know what I am missing. Also I am not sure if "LinkClickedEventArgs" is right or am I suppose to add something else.

Expand|Select|Wrap|Line Numbers
  1.    Private Sub addCommandHandler()
  2.         Dim ctrl As Control
  3.         For Each ctrl In Me.Controls
  4.             If TypeOf (ctrl) Is Label Then
  5.                 AddHandler ctrl.Click, AddressOf link_Clicked
  6.             End If
  7.         Next
  8.     End Sub
  9.  
  10.     Public Sub link_Clicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs)
  11.  
  12.     End Sub
May 22 '08 #1
6 1160
Sick0Fant
121 100+
I am trying to create an event handler for my link labels. What I want to do is when someone clicks the link then it should open the link in a new browser. But the problem I am having is that I dont know how to add event handler for link labels.
This is what I am trying to do, but I dont know what I am missing. Also I am not sure if "LinkClickedEventArgs" is right or am I suppose to add something else.

Expand|Select|Wrap|Line Numbers
  1.    Private Sub addCommandHandler()
  2.         Dim ctrl As Control
  3.         For Each ctrl In Me.Controls
  4.             If TypeOf (ctrl) Is Label Then
  5.                 AddHandler ctrl.Click, AddressOf link_Clicked
  6.             End If
  7.         Next
  8.     End Sub
  9.  
  10.     Public Sub link_Clicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs)
  11.  
  12.     End Sub
Edit:
I see what you're trying to do. Let me think about it.
May 22 '08 #2
???

Click is a standard event. You don't have to add one yourself, you just have to implement it.

Can you tell me why you're adding event handlers like this?
Well,
I have 12 linklabels and I want to do smart programming. So instead of doing something

Expand|Select|Wrap|Line Numbers
  1. Private Sub LinkLabel12_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel12.LinkClicked
  2.           System.Diagnostics.Process.Start("http://" + LinkLabel12.Text)
  3.     End Sub
for 12 labels, I want to add an event handler which will automatically figure out which link is clicked and display it in the browser. I was able to add right event handlers, but now the problem is that I dont get the data of the link, for example in code below I cannot get the link value which can be www.yahoo.ca or something else

Expand|Select|Wrap|Line Numbers
  1. Private Sub addCommandHandler()
  2.         Dim ctrl As Control
  3.         For Each ctrl In Me.Controls
  4.             If TypeOf (ctrl) Is LinkLabel Then
  5.                 AddHandler CType(ctrl, LinkLabel).LinkClicked, AddressOf EventHandler
  6.             End If
  7.         Next
  8.     End Sub
  9.  
  10.     Private Sub EventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
  11.         Dim target As String = CType(e.Link.LinkData, String)
  12.         MsgBox(target)
  13.     End Sub
May 22 '08 #3
Plater
7,872 Expert 4TB
Well if you want to have them open links in new windows, you're going to need to use javascript, not server-side code.
So you would go do something like:
Expand|Select|Wrap|Line Numbers
  1. Dim ctrl As Control
  2.         For Each ctrl In Me.Controls
  3.             If TypeOf (ctrl) Is LinkLabel Then
  4.                 ctrl.Attributes.Add("onclick","SomeJavaScriptFunctionToOpenNewWindow("&"The url you want maybe"&");")
  5.             End If
  6.         Next
  7.  
Then add a javascript function to your page that takes in a url and opens another window with the link.
May 22 '08 #4
Sick0Fant
121 100+
Well if you want to have them open links in new windows, you're going to need to use javascript, not server-side code.
So you would go do something like:
Expand|Select|Wrap|Line Numbers
  1. Dim ctrl As Control
  2.         For Each ctrl In Me.Controls
  3.             If TypeOf (ctrl) Is LinkLabel Then
  4.                 ctrl.Attributes.Add("onclick","SomeJavaScriptFunctionToOpenNewWindow("&"The url you want maybe"&");")
  5.             End If
  6.         Next
  7.  
Then add a javascript function to your page that takes in a url and opens another window with the link.
I don't think he's doing asp.
May 22 '08 #5
Plater
7,872 Expert 4TB
I don't think he's doing asp.
Good point, well then the post prior to mine appeared to have the correct functionality, but I hate sifting through vb "code" so I can't be sure why it didn't work.

Although I would probably have gone with using the sender object o for finding out what was clicked.
May 22 '08 #6
Good point, well then the post prior to mine appeared to have the correct functionality, but I hate sifting through vb "code" so I can't be sure why it didn't work.

Although I would probably have gone with using the sender object o for finding out what was clicked.
Guys, I have found out myself. As I mentioned in my previous posts that I am kinda out from development since a while, thats why I am finding hard to pick it up again. Anyways, here is the working code.

Expand|Select|Wrap|Line Numbers
  1.  Private Sub addCommandHandler()
  2.         Dim ctrl As Control
  3.         For Each ctrl In Me.Controls
  4.             If TypeOf (ctrl) Is LinkLabel Then
  5.                 AddHandler CType(ctrl, LinkLabel).LinkClicked, AddressOf EventHandler
  6.             End If
  7.         Next
  8.     End Sub
  9.  
  10.     Private Sub EventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
  11.         System.Diagnostics.Process.Start("http://" & CType(sender, LinkLabel).Text)
  12.     End Sub
May 22 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
1
by: Waqas Pitafi | last post by:
Hi, 3rd day is already gone without any solution. My problem is, I have a Windows Server 2003 sp1 machine as my development platform having NTFS filesystem. Other notable components installed...
5
by: Ivan | last post by:
I am used to VB6 and am not sure how to do this in Vstudio .NET. I have a main form which calls other forms. I want to disable that main form while other ones are called. I tried hiding it and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.