• 0

Need basic .NET OnMouseHover help


Question

I have 2 Labels (Label1 and Label2).

 

I have set Label2 "Visible" to False.

 

I want to show Label2 when Label1 is hovered with a mouse.

 

I have double-clicked on Label1 and entered:

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    OnMouseHover(Label2.Visible = True)
End Sub

It doesn't work. What's wrong?

 

I am a beginner - go easy on me!

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I don't have VB.NET installed, so here is some C# code that demonstrates the events you should use:

 

        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Visible = false;
            label1.MouseEnter += Label1_MouseEnter;
            label1.MouseLeave += Label1_MouseLeave;
        }

        private void Label1_MouseLeave(object sender, EventArgs e)
        {
            label2.Visible = false;
        }

        private void Label1_MouseEnter(object sender, EventArgs e)
        {
            label2.Visible = true;
        }

 

Link to comment
Share on other sites

  • 0

This is the top few lines of Form1.vb:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label2.Visible = False;
    End Sub

    Private Sub Label1_MouseEnter(Object sender, EventArgs e)
        Label2.Visible = True;
    End Sub

    Private Sub Label1_MouseLeave(Object sender, EventArgs e)
        Label2.Visible = False;
    End Sub

Lots of errors, so won't compile.

Link to comment
Share on other sites

  • 0

Virtorio has the right idea, not sure what it looks like in VB.

 

Are you trying to use label2 as a tooltip? You could just use built-in tooltips if this is the case.

Link to comment
Share on other sites

  • 0
Just now, Sledge said:

Virtorio has the right idea, not sure what it looks like in VB.

 

Are you trying to use label2 as a tooltip? You could just use built-in tooltips if this is the case.

I am using ToolTips and a custom "Tips" section at the bottom of the form :)

 

So when various parts of the form are hovered, different labels appear next to "Tips:"

Link to comment
Share on other sites

  • 0
5 minutes ago, Elliot B. said:

...

Let me try again when some actual VB.NET code. Use the forms editor to assign the MouseEnter (raised when the cursor is over a control) and MouseLeave (raised when the cursor leaves the control area) for label1, using the following code for the generated event handlers.

Public Class Form1
    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
        Label2.Visible = True
    End Sub

    Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
        Label2.Visible = False
    End Sub
End Class

 

Link to comment
Share on other sites

  • 0
11 minutes ago, Elliot B. said:

This is the top few lines of Form1.vb:


Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label2.Visible = False;
    End Sub

    Private Sub Label1_MouseEnter(Object sender, EventArgs e)
        Label2.Visible = True;
    End Sub

    Private Sub Label1_MouseLeave(Object sender, EventArgs e)
        Label2.Visible = False;
    End Sub

Lots of errors, so won't compile.

First thing is VB.NET lines do not end with ;

 

To create Event handlers fast, you can use VS to add them. Simply select the item in the designer view (Label in your case) and then in the properties form on the left, select event handlers (symbol of a thunderbolt).

You can add Event handling methods there just by double-clicking on them.

 

The code for Label1_MouseEnter would then be changed to:

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

  Label2.Visible = True

End Sub

Link to comment
Share on other sites

  • 0
8 minutes ago, virtorio said:

Let me try again when some actual VB.NET code. Use the forms editor to assign the MouseEnter (raised when the cursor is over a control) and MouseLeave (raised when the cursor leaves the control area) for label1, using the following code for the generated event handlers.


Public Class Form1
    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
        Label2.Visible = True
    End Sub

    Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
        Label2.Visible = False
    End Sub
End Class

 

BOOM!

 

Legend (Y) Thank you!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.