Archived

This topic is now archived and is closed to further replies.

VB.NET - setting label underline during runtime?

Recommended Posts

JoDaddy    0

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
azcodemonkey    1

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
azcodemonkey    1

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
bangbang023    30

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
JoDaddy    0

:) 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

  • Recently Browsing   0 members

    No registered users viewing this page.