Label to Act like Button

I have a number of lables on a form.  When I mouse over the lable I need it to rise(popup) just like a button.  When I click on it I need it to stay indented.

I know that I use the Button1_MouseHover but how can I make it so the lable appears raised and then on the MouseClick make it appear indented.

Thank you
running32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

planoczCommented:
FYI.... quick label sample on color....

    Dim x As Color = Color.Red
    Dim x2 As Color = Color.Blue

   Private overLabel As Boolean = False
    Private LabelBorderColor As Color = Color.Red

    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        overLabel = True
        Label1.Tag = Label1.BackColor
        Label1.BackColor = Color.Cyan
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        overLabel = False
        Label1.BackColor = Label1.Tag
    End Sub

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        If overLabel Then
            Dim g As Graphics = e.Graphics
            Dim p As New Pen(LabelBorderColor)
            g.DrawRectangle(p, 0, 0, Label1.Width - 1, Label1.Height - 1)
            p.Dispose()
        End If
    End Sub

   Private Sub Label2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.MouseEnter
        ' Label2.BackColor = MixColors(x, x2, 50) 'middle
        Label2.BackColor = MixColors(x, x2, 20) 'more red
    End Sub
    Private Sub Label2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.MouseLeave
        Label2.BackColor = MixColors(x, x2, 70) 'more blue
    End Sub
    Public Function MixColors(ByVal Color1 As Color, ByVal Color2 As Color, ByVal Percentage As Integer) As Color
        Dim R(1) As Byte, G(1) As Byte, B(1) As Byte
        Dim iR(1) As Integer, iG(1) As Integer, iB(1) As Integer
        Dim fRed As Integer, fGreen As Integer, fBlue As Integer
        Dim fPercentage(2) As Double
        If Percentage < 0 Then Percentage = 0
        If Percentage > 100 Then Percentage = 100

        R(0) = Color1.R
        G(0) = Color1.G
        B(0) = Color1.B
        R(1) = Color2.R
        G(1) = Color2.G
        B(1) = Color2.B
        If R(0) > R(1) Then
            iR(0) = -1
            iR(1) = R(0) - R(1)
        Else
            iR(0) = 1
            iR(1) = R(1) - R(0)
        End If

        fPercentage(0) = (iR(1) / 100) * (Percentage * iR(0))
        If G(0) > G(1) Then
            iG(0) = -1
            iG(1) = G(0) - G(1)
        Else
            iG(0) = 1
            iG(1) = G(1) - G(0)
        End If

        fPercentage(1) = (iG(1) / 100) * (Percentage * iG(0))
        If B(0) > B(1) Then
            iB(0) = -1
            iB(1) = B(0) - B(1)
        Else
            iB(0) = 1
            iB(1) = B(1) - B(0)
        End If

        fPercentage(2) = (iB(1) / 100) * (Percentage * iB(0))
        fRed = R(0) + fPercentage(0)
        fGreen = G(0) + fPercentage(1)
        fBlue = B(0) + fPercentage(2)

        Dim newcol As Color = Color.FromArgb(fRed, fGreen, fBlue)
        Return newcol
    End Function
running32Author Commented:
Thank you.  In your code where do the buttons raise.

Thanks
planoczCommented:
The only thing  I have right now is the colored labels, no raise buttons look alikes.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

The_BiochemistCommented:
This may be a stupid question... but surely if you are tring to emulate the same functionality as a button, why not just use a button?

you can play with their colors, write text on them and they manage mouseovers and onclicks

Just seems like you're trying to create the functionality of an existing component?
Bob LearnedCommented:
Why don't you just use a button as a label instead?

Bob
planoczCommented:
I was going to say the samething.
The_BiochemistCommented:
So it's true... Great minds do think alike!
Erick37Commented:
You could create your own class which inherits the Label.
Then you can control how it behaves when the mouse enters and leaves.
Quick example below, use the LabelEx class in place of your existing Windows.Forms.Label

Public Class LabelEx
    Inherits Windows.Forms.Label

    Private look As Border3DStyle

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(e)
        Dim rc As New Rectangle(0, 0, MyBase.Width, MyBase.Height)
        ControlPaint.DrawBorder3D(e.Graphics, rc, look)
 
    End Sub

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        look = Border3DStyle.Raised
        MyBase.Refresh()
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        look = Border3DStyle.Sunken
        MyBase.Refresh()
    End Sub

    Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        look = Border3DStyle.Raised
        MyBase.Refresh()
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        look = Border3DStyle.Adjust
        MyBase.Refresh()
    End Sub
End Class
planoczCommented:
Good show Erick37,
I am these youngers can write code sometimes. :)
running32Author Commented:
Erick37, thanks I think this is a bit above my skill level at this time.  Can you explain to me how I call this and how it works.

Thanks
planoczCommented:
Just change this in your form code and add a new class with Erick37's code

  Friend WithEvents Label3 As Buttons.LabelEx              'System.Windows.Forms.Label

  Me.Label3 = New Buttons.LabelEx                 'System.Windows.Forms.Label
planoczCommented:
Sorry Buttons is the name of YOUR app not my app.

Buttons.LabelEx              
Erick37Commented:
The easiest way to use an inherited control:

Add a new Class to your project and name it labelex.vb.
Paste above code into the editor window for the new class you just created.

On the main form where you want to use the label, open the code window.
Expand the section marked "Windows Form Designer generated code"
You will be replacing code in 2 places (assuming you already placed a standard Label1 on the form):

Change the old declaration:
Friend WithEvents Label1 As System.Windows.Forms.Label
to
Friend WithEvents Label1 As LabelEx

Also change
Me.Label1 = New System.Windows.Forms.Label
to
Me.Label1 = New LabelEx

That should do it.



running32Author Commented:
My code view does not show the Windows form designer generated code. What option should I set to view it.

Thanks
planoczCommented:
do a search on (New System.Windows.Forms.Label) to find it.
running32Author Commented:
That is nearly what I need.  Thank you so much.  How can it be made so that onclick it stays sunken until the next lable is clicked?  Thank you again
Erick37Commented:
Rewrite:

The new LabelEx class:

Public Class LabelEx
    Inherits Windows.Forms.Label

    Private look As Border3DStyle

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(e)
        Dim rc As New Rectangle(0, 0, MyBase.Width, MyBase.Height)
        ControlPaint.DrawBorder3D(e.Graphics, rc, look)
 
    End Sub

    Public Sub setlook(ByVal newlook As Border3DStyle)
        look = newlook
        MyBase.Refresh()
    End Sub

    Public Sub New()
        MyBase.new()
        look = Border3DStyle.Raised
    End Sub

End Class

'=============================================

Form code:

    'this is the pointer to the currently pressed label
    Private lastPressedLabel As LabelEx

    'This Sub handles the MouseDown events for all the labels
    Private Sub LabelEx_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown
        Dim lbl As LabelEx = DirectCast(sender, LabelEx)

        If Not IsNothing(lastPressedLabel) Then
            lastPressedLabel.setlook(Border3DStyle.Raised)
        End If

        lbl.setlook(Border3DStyle.Sunken)
        lastPressedLabel = lbl

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
running32Author Commented:
Sorry I'm just learning.  I don't think I explained myself very clearly but I really appreciate you helping me.

I need it to be flat at start, raise when mouse over and flat when mouse leave and sunken when clicked.

Thank you
Erick37Commented:
Another attempt:

LabelEx class code:

Public Class LabelEx
    Inherits Windows.Forms.Label

    Private look As Border3DStyle

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(e)
        Dim rc As New Rectangle(0, 0, MyBase.Width, MyBase.Height)
        ControlPaint.DrawBorder3D(e.Graphics, rc, look)
 
    End Sub

    Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        MyBase.OnMouseEnter(e)
        If look <> Border3DStyle.Sunken Then
            look = Border3DStyle.Raised
            MyBase.Refresh()
        End If
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        If look <> Border3DStyle.Sunken Then
            look = Border3DStyle.Adjust
            MyBase.Refresh()
        End If
    End Sub

    Public Sub setlook(ByVal newlook As Border3DStyle)
        look = newlook
        MyBase.Refresh()
    End Sub

End Class


'=======================================
Form Code:

    'this is the pointer to the currently pressed label
    Private lastPressedLabel As LabelEx

    'This Sub handles the MouseDown events for all the labels
    Private Sub LabelEx_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown
        Dim lbl As LabelEx = DirectCast(sender, LabelEx)

        If Not IsNothing(lastPressedLabel) Then
            lastPressedLabel.setlook(Border3DStyle.Adjust)
        End If

        lbl.setlook(Border3DStyle.Sunken)
        lastPressedLabel = lbl

    End Sub
running32Author Commented:
Can you tell me why only two of the buttons will work?
running32Author Commented:
Sorry I see why.

Thank you so much for all your hard work.  I really appreicate it.
Erick37Commented:
Make sure you add the button to the handler...

 Private Sub LabelEx_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown '<< all your labels must be listed for this eventy to handle them

Also make sure you have done the replacements to LabelEx in the "Windows Form Designer generated code"

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.