Solved

Pegging extended info (e.g a URL) to Label for Click event in Windows Forms app

Posted on 2004-09-05
5
194 Views
Last Modified: 2010-04-15
Hey all -

Here's the problem. I have an app that gets notices from the database, and outputs a Label to the form for each notice (basically just containing a headline of what the notice is about). Each notice (Label) needs to be clickable, and when the user clicks, the form needs to open up a browser window at a URL corresponding to the clicked Label. I have all of it working except for the part of the code that is able to distinguish which Label was actually clicked.

Ideally, I'd like to just set an event argument for each label that contains the URL to visit (but such that all the labels use the same event method). I can't tell a way to do this without creating my own delegate, EventArgs subclass, and Label subclass, though...and if I did it, it would be too specialized to this one case (unless I'm looking at it wrong).

Currently, the hack I have is maintaining a class array with all the URLs, and then setting the length of each label to (500 + i), where i is that label's index within the URL array. But really, I don't want to have to maintain a separate array, and I sure as heck don't want to have to encode indices in the Length field. How else can I peg arbitrary data to these labels? (Also note that I can't use the label's Text field, since some notices will have the same "headline.")

Thanks.
0
Comment
Question by:hfjeremy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 11985365
Are the Label controls being created dynamically on the form, or is there a set of Labels that are used?
0
 
LVL 1

Author Comment

by:hfjeremy
ID: 11985373
Sorry, I neglected to mention that. I'm creating them dynamically in a loop, and then adding them to a panel.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 11985384
Regardless, you could store the index information in the Label.Tag property, or create a table that relates Label objects on your form to a specific index number (i.e.: using a HashTable).

Here is an simple example (you would use this code in your form class):

  Hashtable urlTable = new Hashtable();

  void SetUrl(Label label, string url)
  {
    urlTable[label] = url;
  }

  string GetUrl(Label label)
  {
    return (string)urlTable[label];
  }
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11985394
If you would like to post your code where the Label controls are created and added to the form, and your EventHandler method for them, I can show you how to modify it...
0
 
LVL 1

Author Comment

by:hfjeremy
ID: 11985472
Nah, it's cool -- completely didn't think of a hash table. Thanks for the advice.

I didn't know about the Label's Tag property, and still don't see it -- probably because I'm using the Compact Framework, which does limit some things. I was looking for a general solution, anyway, not CF-specific, and the hash table does that.

Thanks again.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question