?
Solved

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

Posted on 2004-09-05
5
Medium Priority
?
196 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 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