Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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