Jump to content



Photo

VB.NET - setting label underline during runtime?


  • Please log in to reply
4 replies to this topic

#1 JoDaddy

JoDaddy

    <insert title here>

  • Joined: 09-September 03

Posted 24 January 2004 - 19:08

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?


#2 azcodemonkey

azcodemonkey

    Neowinian Senior

  • Joined: 19-March 02

Posted 24 January 2004 - 22:11

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.

#3 azcodemonkey

azcodemonkey

    Neowinian Senior

  • Joined: 19-March 02

Posted 24 January 2004 - 22:20

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


#4 vetbangbang023

bangbang023

    Silent Veteran

  • Joined: 17-October 01
  • Location: Brooklyn, NY

Posted 24 January 2004 - 23:30

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

#5 OP JoDaddy

JoDaddy

    <insert title here>

  • Joined: 09-September 03

Posted 25 January 2004 - 05:40

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