Clickable Label

Last post 05-22-2008 8:14 by Grega. 13 replies.
Page 1 of 1 (14 items)
Sort Posts: Previous Next
  • 06-05-2002 14:01

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Clickable Label

    I don't know who the original author of this Label is. It came from the compactframework Microsoft newsgroup and I got it from a repost.

    Download the code here

  • 07-29-2002 8:58 In reply to

    Re: Clickable Label

    This code is very useful. Unfortunately is only in VB.NET . Can anyone help and contribute changing this code to C#
    Thanks

  • 07-30-2002 12:32 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Re: Clickable Label

    And here's the C# equivalent courtesy of Dean Cleaver


    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace NetTech.Windows.Forms.Controls
    {
    public class Label : Control
    {
    //Implements Label as I expect it to be (more or less...)
    private BorderStyle borderStyle = BorderStyle.None;
    private ContentAlignment textAlignment = ContentAlignment.TopLeft;
    // Accessing the font object of the Base Class throws a nasty exception!
    // So, I am using my own font.
    private string fontName = "Tahoma";
    private int fontSize = 10;
    private FontStyle fontStyle = FontStyle.Regular;

    public event EventHandler labelClick;
    public event PaintEventHandler labelPaint;

    public Label()
    {
    }

    public BorderStyle BorderStyle
    {
    get {return this.borderStyle;}
    set {this.borderStyle = value;}
    }

    public string FontName
    {
    get {return this.fontName;}
    set {this.fontName = value;}
    }

    public int FontSize
    {
    get {return this.fontSize;}
    set {this.fontSize = value;}
    }

    public FontStyle FontStyle
    {
    get {return this.fontStyle;}
    set {this.fontStyle = value;}
    }

    public ContentAlignment TextAlignment
    {
    get {return this.textAlignment;}
    set {this.textAlignment = value;}
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
    Graphics graphics = pe.Graphics;
    Pen pen = new Pen(Color.Black);
    SolidBrush brush = new SolidBrush(ForeColor);
    Font font = new Font(this.fontName, this.fontSize, this.fontStyle);
    SizeF s;

    if (this.BorderStyle == BorderStyle.FixedSingle)
    graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
    else if (this.BorderStyle == BorderStyle.Fixed3D)
    graphics.DrawRectangle(pen, this.ClientRectangle);

    s = graphics.MeasureString(this.Text, font);
    switch (this.textAlignment)
    {
    case ContentAlignment.TopCenter:
    graphics.DrawString(this.Text, font, brush, (this.Width - s.Width) / 2,
    (this.Height - s.Height) / 2);
    break;
    case ContentAlignment.TopLeft:
    graphics.DrawString(this.Text, font, brush, 2, (this.Height - s.Height)
    / 2);
    break;
    case ContentAlignment.TopRight:
    graphics.DrawString(this.Text, font, brush, this.Width - s.Width - 2,
    (this.Height - s.Height) / 2);
    break;
    }

    pen.Dispose();
    brush.Dispose();
    font.Dispose();

    if (labelPaint != null)
    labelPaint(this, pe);

    base.OnPaint(pe);
    }

    protected override void OnClick(System.EventArgs e)
    {
    if (labelClick != null)
    labelClick(this, e);

    base.OnClick(e);
    }
    }
    }


  • 06-29-2003 12:10 In reply to

    Re: Clickable Label

    how to install it in VS.Net toolbox??

    .tolemac.
  • 06-30-2003 6:31 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Re: Clickable Label

    This control has no designer support. I'll see about creating one.
  • 12-04-2003 14:46 In reply to

    Re: Clickable Label

    I'm using the C# version and am seeing a strange problem...

    On my main form, I have a custom TreeView which knows how to handle
    click events. It handles the AfterSelect event and then passes along
    the event to the appropriate node.

    One of my TreeNodes brings up a dialog with a ClickableLabel on it.
    The problem is that, if the location of the click corresponds to the
    position of the ClickableLabel, the label's click handler gets called.
    It's as if a click on the caller form is being handled by an item in
    the dialog box!

    If I replace the ClickableLabel with a Button, everything works fine.

    Anyone else seeing this or have ideas on how I can fix it?
    Thanks,
    Ken
  • 02-15-2004 18:14 In reply to

    Re: Clickable Label

    Hello All,

    after reading this post here I tried to make a ClickLabel control with designer support. If you are interested to try out have a look
    at http://www.mozzak.com.

    Mozzak
  • 08-31-2004 5:54 In reply to

    Re: Clickable Label

    Hi,
    I have tried to use the LinkLabel and ClickLabel (Mozzak's implementation) and have come up with the same problem in each. I have written a programme that reads RFID tags and looks up the data for them in an XML database. Three items are updatable once the data is loaded. I put a LinkLabel / ClickLabel (Colour red, Underlined) and once clicked, it hides and a comboBox is made visible in its place. All this works without a hitch.

    The problem is when updating the .Text property at runtime. This seems to halt the programme and will not react to button presses. This works fine on normal labels, but not these modified implementations. Just thought I'd let you know.

    Other than that, great work! :)

    Gaz
  • 09-01-2004 4:02 In reply to

    Re: Clickable Label

    UPDATE:
    Hi,
    Just and update. When I realised this also didn't work with buttons, I started blaming my software rather than everyone else's great work. I modified the software to invoke the updating routine rather than just call it, and it all works a treat.

    Sorry to implicate anyone else! [:I]

    Gaz
  • 06-30-2005 6:24 In reply to

    Re: Clickable Label

    The click event of Clickable Link is not fired [:(]. At run time I'm creating the x number of Clickable link and at the same time also defining its click event. But it does have focus as well as it does not fire the click event.

    My code is some like this........

    ClickLabel cl1 = new ClickLabel();
    cl1.GotFocus+=new EventHandler(cl1_GotFocus);
    cl1.Click+=new EventHandler(cl1_Click);

    Please help me out to do the above mentioned action. It will be [:D]


    Take Care

    Sudhir Tibrewal
  • 10-25-2005 10:49 In reply to

    Re: Clickable Label

    Does anyone know where I can get a copy of the VB version of the clickable label? I tried the link that ctacke listed in his first post, but it's missing.
  • 02-15-2006 22:05 In reply to

    Re: Clickable Label

    Hey there,
    My first post here - useful bit of code for Label, but I'm just curious as to why I cannot simply derive a custom control like:

    class ClickableLabel : Label
    {
    public event EventHandler clickEvent;
    protected override void OnClick(System.EventArgs e)
    {
    clickEvent(this,e);
    base.OnClick(e);
    }
    }


    This doesn't seem to work for me - could someone explain why?

    Thanks in advance.
  • 02-17-2006 9:44 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Re: Clickable Label

    Becasue the Label doesn't implement and OnClick to override. If it did there would be no point in the ClickableLabel class.
  • 05-22-2008 8:14 In reply to

    • Grega
    • Top 50 Contributor
    • Joined on 09-27-2007
    • Posts 10

    Re: Clickable Label

    Hi guys,

    today i was doing something with label and figured out they don't have click event :o

    so i spent most of the day looking for solutions and found none...

    at start of this thread there is a code chris provided but it doesn't seem to work for me...I converted it back to VB .net and made a class but when I run the whole thing the text in this custom label doesn't work, the click on works fine but no text is shown?!?! is there something I am missing...

    Does anyone have a working implementation of the clickable label that can share? 

     Hope I come up with some solution since this is a mayor thing for my app.


    Thanks  

     Grega
     

Page 1 of 1 (14 items)