Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Question about Visual Basic 2010 Express coding?

I am trying to make a simple sort of hello world program but with a twist of when your mouse goes out the box area it says "why you leave" and stuff. I just can't figure out what is wrong, because every time I click the button to make the Message pop up, it goes onto the "why you leave one first. Here's the code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox("Hello World!", MsgBoxStyle.Information, "Hello")

End Sub

End Class

Public Class Form1

Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave

MsgBox("Why you a' leave? You make a' me so a' sad!", MsgBoxStyle.Question, "D:")

End Sub

End Class

Anyone know?????

Update:

@flyingtiggeruk

that has just made it not say the "why you leave" one at all :/

2 Answers

Relevance
  • 10 years ago
    Favorite Answer

    When you display a message box, the mouse focus gets transferred away from your main form to the message box itself (So that you can click on the buttons in the message box.)

    So, by creating a message box, you are generating a mouse leave event, which is why you see both messages.

    You need to set a flag before you display the Hello World message, then clear it after user closes it.

    In your mouse leave handler, check that flag. If set, don't display the so sad message.

    ' Class level flag variable:

    Private bInMessageBox As Boolean = False

    ' In the click handler:

    bInMessageBox = True

    MsgBox("Hello World!", MsgBoxStyle.Information, "Hello")

    bInMessageBox = False

    ' In the leave handler:

    If Not bInMessageBox Then

        MsgBox("Why you a' leave? You make a' me so a' sad!", MsgBoxStyle.Question, "D:")

    End If

  • 10 years ago

    Don't you want it to be a different event from Button1_MouseLeave?

    edit

    like

    Private Sub Form1_MouseLeave(ByVal sender As System.Object,

    ByVal e As System.EventArgs) Handles MyBase.MouseLeave

    MsgBox("Why you a' leave? You make a' me so a' sad!",

    MsgBoxStyle.Question, "D:")

    End Sub

    edit 2

    It works fine for me. Once the mouse leaves the form the messagebox pops up..

Still have questions? Get your answers by asking now.