Results 1 to 5 of 5

Thread: [RESOLVED] linklabel styling?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    39

    Resolved [RESOLVED] linklabel styling?

    Hi,

    How can I change the font, text size and change the text color of the link when the mouse is hovered over it?

    I have played with:
    Code:
    Linklabel.font = Verdana;
    
    Linklabel.forecolor = red;
    Neither seem to work and the color part isn't what I am looking for.

    Any help would be much appriciated.

    Thanks,

    Al

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: linklabel styling?

    If you notice there is a ForeColor property and a LinkColor Property, so if the entire text in linklabel is acting as a link then use LinkColor.
    VB Code:
    1. this.linkLabel1.LinkColor = Color.Red;
    2. 'Same for Forecolor
    3. this.linkLabel1.ForeColor = Color.Red;

    To change the font:
    VB Code:
    1. this.linkLabel1.Font = new System.Drawing.Font("Verdana",(float)(10.0),FontStyle.Regular);

    EDIT: sorry, thought 'twas a VB question!!
    Last edited by Harsh Gupta; Oct 11th, 2006 at 05:30 PM.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    39

    Re: linklabel styling?

    great thanks harsh gupta!

    That put me in the correct direction but now I have a further issue. My linklabels are created dynamically using this
    Code:
    public void AddNewLinkLabel()
            {
                LinkLabel aLabel = new LinkLabel();
                this.List.Add(aLabel);
                ((Form1)Form1.ActiveForm).panel1.Controls.Add(aLabel);
                //set linklabel properties
                aLabel.AutoSize = true;
                aLabel.LinkVisited = false;
                aLabel.Visible = true;
                aLabel.Top = 5;
                aLabel.Height = 15;
                aLabel.LinkColor = System.Drawing.Color.Blue;
                aLabel.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
                //aLabel.Font.FontFamily.Name.
                aLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // handler for clicked event of news title label
                aLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(ClickHandler);
                aLabel.MouseMove += new MouseEventHandler(aLabel_MouseMove);
                aLabel.MouseLeave += new EventHandler(aLabel_MouseLeave);
                aLabel.MouseHover += new EventHandler(aLabel_MouseHover);
            }
    and I have assigned Event Handlers dynamically which look like this
    Code:
            private void aLabel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                aLabel.LinkColor = System.Drawing.Color.Red;
            }
    
            private void aLabel_MouseLeave(object sender, System.EventArgs e)
            {
                aLabel.LinkColor = System.Drawing.Color.Blue;
            }
    
            private void aLabel_MouseHover(object sender, System.EventArgs e)
            {
                aLabel.LinkColor = System.Drawing.Color.Red;
            }
    The issue is this won't compile as it can't find a linklabel called aLabel as they don't exist yet, until they are created dynamically.

    Is there away to do this to enable it to compile and execute correctly?

    Many thanks,

    Al

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: linklabel styling?

    Whether or not you've created your LinkLabels at run time, the aLabel variable is declared within the AddNewLinkLabel method so it doesn't exist outside of that method. Within an event handler you reference the object that raised the event via the 'sender' argument:
    Code:
            private void aLabel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                ((LinkLabel)sender).LinkColor = System.Drawing.Color.Red;
            }
    Then you can use the same method to handle the event for as many different Linklabel's as you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    39

    Re: linklabel styling?

    Quote Originally Posted by jmcilhinney
    Whether or not you've created your LinkLabels at run time, the aLabel variable is declared within the AddNewLinkLabel method so it doesn't exist outside of that method. Within an event handler you reference the object that raised the event via the 'sender' argument:
    Code:
            private void aLabel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                ((LinkLabel)sender).LinkColor = System.Drawing.Color.Red;
            }
    Then you can use the same method to handle the event for as many different Linklabel's as you like.

    Great thanks jmcilhinney.

    Worked a treat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width