making a default button

Hi!!

I’m writing an application that the user write a number in a textbox and then press a button to process a sub. I want the user write in the textbox and when ends press Enter and call the same sub. Is this possible


Thanks for the help.




Answer this question

making a default button

  • Jimski

    And since you are inputing numbers, you can verify that they user is only putting numbers in the textbox by doing this (I didn't include the Enter Keypress checking on this).

    Create a public varable called nonNumberEntered As Boolean = False

    Change the keydown event to:

    ' Initialize the flag to false.
    nonNumberEntered = False
    ' Determine whether the keystroke is a number from the top of the keyboard.
    If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
    ' Determine whether the keystroke is a number from the keypad.
    If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
    ' Determine whether the keystroke is a backspace.
    If e.KeyCode <> Keys.Back Then
    ' A non-numerical keystroke was pressed.
    ' Set the flag to true and evaluate in KeyPress event.
    nonNumberEntered = True
    End If
    End If
    End If
     
    and change the KeyPress event to:
     
    ' Check for the flag being set in the KeyDown event.
    If nonNumberEntered = True Then
    ' Stop the character from being entered into the control since it is non-numerical.
    e.Handled = True
    End If

  • Visual Martin

    I remember in VB 6.0, there was a much easier way doing it. If you had a Button, and the in the text change, you could just add Button1_default or something, like that.

    How is that possible in VB Exp


  • Richard Moorhouse

    man. .  vb sux
    look at how ugly it is!


    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
     If AscW(e.KeyChar) = 13 Then
                MessageBox.Show("Enter Pressed")
            End If
    End Sub

    and in c#:

    private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
     if (e.KeyChar = '\r') MessageBox.Show("Enter Pressed") ;
    }



  • Bojidar Markov

    Well,

    You could do something like this (I'm sure there is probably an easier way):

    Public Class Form1
    'Global Variable for checking for Enter
    Private enterEntered As Boolean = False
     
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
     
    ' Initialize the flag to false.
    enterEntered = False
    ' Determine whether the keystroke is the enter key.
    If e.KeyCode = Keys.Enter Then
    'Set the flag to true and evaluate in KeyPress event.
    enterEntered = True
    End If
    End Sub
     
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
     
    ' Check for the flag being set in the KeyDown event.
    If enterEntered = True Then
    MsgBox("Enter key was pressed")
    End If
    End Sub
     
    End Class

  • buzzbuba

    Thanks Blair!

    That’s is like I’m finding. I remember something like this in VB 6.

    Thanks!!


  • Jens Sauer

    vbsux is even worse!!!

    cheers!



  • Arunachalam

    Public Class Form1

        Protected lb As New Label

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            TextBox1.Multiline = True
            TextBox1.AcceptsReturn = True
            Me.Controls.Add(lb)
            lb.Location = New Point(50, 100)

        End Sub

        Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click

            Dim a As Integer = CInt(TextBox1.Text.Trim)
            lb.Text = CStr(a)
            'Process here

        End Sub

     

        Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

            Static num As String
            Dim er As Boolean

            Select Case e.KeyChar
                Case Chr(Keys.Enter)
                    cbGo_Click(sender, New System.EventArgs)
                    e.Handled = True
                Case Is < "0"
                    er = True
                Case Is > "9"
                    er = True
                Case Else
                    num &= e.KeyChar
            End Select

            If er Then
                Beep()
                TextBox1.Text = num
                e.KeyChar = Nothing
            End If

         
        End Sub

     

    End Class



  • elygp

    hi,

    i guess fox answered this question but i just wanted to add something small

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode = Keys.F1 AndAlso IsNumeric(TextBox1.Text) Then

    Call MySub(TextBox1.Text)

    TextBox1.Text = ""

    Me.TextBox1.Focus()

    ElseIf e.KeyCode = Keys.Enter Then

    Call MySub(TextBox1.Text)

    End If

    End Sub

    hope that was helpfull



  • making a default button