BUG: Focus for a MessageBox

I'm pretty sure this is a bug of some sort, but if this is by design, then my suggestion would be to change it!  :p 

If I have one TextBox on a form and have a method that handles the KeyUp Event.  In the event, I have code to check for if the user hit enter.  When they hit enter, it goes out to a table and looks up to see if what's in the .Text property exists in a particular field in the table and return back true or false.  If there is no record, I display a MessageBox telling them there was no record found, then when they hit OK, I set the focus back to the TextBox.

Now, if the user hits the Enter key, because the MessageBox has the focus currently and the OK button is the default button, it closes the MessageBox, however, it immediately opens the same MessageBox back up.  You can hit enter as many times as you want, but it keeps come back.  If you click the OK button with your mouse, however, the MessageBox goes away and focus is returned to the TextBox

My assumption is that even though you hit the enter key on the MessageBox it was actually registered as if you hit it while in the TextBox.

That's not by design, right   Seems like a bug to me.  So yah, that's my suggestion.  Make it so that doesn't happen!

I even tried just getting rid of the TextBox.Focus() after the MessageBox.Show, but that didn't fix it either!


Answer this question

BUG: Focus for a MessageBox

  • WylieECoyote

    I just tried it in RC3 and still get it, so I'm really guessing in this a "by design" case unfortunately, but i'll hit up BetaPlace tomorrow...sorry for the late post on this guys, I'm on vacation.
  • Developr

    Works fine in Everett...
  • DinoBob

    Shouldnt you set "e.Handled = true" to tell the system that you did handle the Up key 
  • Kevin M. Owen

    yup, let me see what i can do...
  • Alains

    Well, you know, I thought so too. Tried it, in several places in the code. Simply didn't help. I think perhaps that because the code was trapping the KeyUp event, it was too late to indicate that the key had been handled. 
  • Axerion

    Well, there you go. That's not good. I betcha this comes back as "as designed", and works as advertised in Everett, as well. Cool
  • Mohit Goel

    Can you cruft up a simple demo that doesn't involve the database lookup  I'd like to give this a try, and see what's up! It should be possible to simply remove the database stuff, and pretend the lookup failed, right
  • lifeforms

    Yeah I got the endless loop ... misunderstood the issue. Has someone bugged it on betaplace yet
  • vishu_gupta

    Pretty sure I tried the posted code in Everett, and got the described behavior (that is, the endless loop). Are you saying that using the posted code, you didn't have that behavior
  • MartinFr-

    Ok, this was a lot easier to reproduce than I thought...

    Create a new form.  Put a TextBox on it and nothing else.

    Put this code in your form.


    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
    MessageBox.Show("Test")
    End If
    End Sub


    Hit Enter in the TextBox, now hit Enter to get hid of the MessageBox.  You'll hit it forever and ever until you actually <b>Click</b> the OK Button on the MessageBox.  :~ 

  • Aaron Sulwer

    Well, there's certainly one easy fix, if you can live with it: trap the KeyDown event instead of the KeyUp. Works fine, in that case. 
  • BUG: Focus for a MessageBox