Sign in to follow this  
Followers 0
JoDaddy

VB.NET - setting label underline during runtime?

5 posts in this topic

I have VB.NET 2002, and I have a label that I'm using as a button (more customization), I want to change the underline property (to true) when the mouse is over it (either mouse hover. enter, or move - i don't know which is best to use), then cchange it back using the form's mouse over/hover/enter property. The problem is, when i try it, i get a read only error, seems the property is readonly. So how would I change it?

Share this post


Link to post
Share on other sites

You should specify that you're dealing with the Font of the Label, and not the Label itself... I was thinking, "There's no underline property for a Label." :o

The underline property of a Font is readonly. You could mimic it by drawing your own underline on the label itself, or use a LinkLabel as a button and have its behavior set to HoverUnderline.

You could also instantiate a new font of the same family and whatnot with the FontStyle set to Underline.

Share this post


Link to post
Share on other sites

To be more specific on the last suggestion:

   Private mThisFont As Font


    Public Sub New()

        MyBase.New()


        'This call is required by the Windows Form Designer.

        InitializeComponent()


        'Add any initialization after the InitializeComponent() call

        mThisFont = New Font("Sans Serif", 12, FontStyle.Regular)

        Label1.Font = mThisFont

    End Sub


    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter

        mThisFont = New Font("Sans Serif", 12, FontStyle.Underline)

        Label1.Font.Dispose()

        Label1.Font = mThisFont

    End Sub


    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave

        mThisFont = New Font("Sans Serif", 12, FontStyle.Regular)

        Label1.Font.Dispose()

        Label1.Font = mThisFont

    End Sub

Share this post


Link to post
Share on other sites

You basically, as weenur shows, create a new font and then apply it to the label.

Share this post


Link to post
Share on other sites
:) OK. Thanks guys. Sorry I wasn't too clear about the object in question. I guess this is another one of those things that .NET makes more complicated. Appreciate it though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.