Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Error 2 47
Not showing JavaScript in the list 5 40
Finally almost got it 5 23
C# Comparing String Arrays. 5 28
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

18 Experts available now in Live!

Get 1:1 Help Now