Solved

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

Posted on 2004-09-05
5
190 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now