VB.NET: Dialog exits when enter pressed?
        Posted  
        
            by Camilo Martin
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Camilo Martin
        
        
        
        Published on 2009-08-29T00:44:36Z
        Indexed on 
            2010/05/22
            5:20 UTC
        
        
        Read the original article
        Hit count: 319
        
Hi all;
My problem seems to be quite simple, but it's not working the intuitive way.
I'm designing a Windows Forms Application, and there is a dialog that should NOT exit when the enter key is pressed, instead it has to validate data first, in case enter was pressed after changing the text of a ComboBox.
I've tried by telling it what to do on KeyPress event of the ComboBox if e is the Enter key:
Private Sub ComboBoxSizeChoose_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBoxSizeChoose.KeyPress
    If e.KeyChar = Convert.ToChar(Keys.Enter) Then
        Try
            TamanhoDaNovaFonte = Single.Parse(ComboBoxSizeChoose.Text)
        Catch ex As Exception
            Dim Dialogo2 As New Dialog2
            Dialog2.ShowDialog()
            ComboBoxSizeChoose.Text = TamanhoDaNovaFonte
        End Try
    End If
End Sub
But no success so far. When the Enter key is pressed, even with the ComboBox on focus, the whole dialog is closed, returning to the previous form. The validation is NOT done at all, and it has to be done before exiting. In fact, I don't even want to exit on the form's enter KeyPress, the only purpose of the enter key on the whole dialog is to validate the ComboBox (but only when in focus, for the sake of an intuitive UI).
I've also tried appending the validation to the KeyPress event of the whole dialog's form, if the key is Enter. NO SUCCESS! It's like my code wasn't there at all.
What should I do?
(Visual Studio 2008, VB.NET)
© Stack Overflow or respective owner