Enter Key instead of Button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adarshyam
    New Member
    • Oct 2008
    • 31

    Enter Key instead of Button

    Hi .. Is there any ways where we can press enter key to calculate numbers in two textboxes and display in another textbox instead of using a separate Calculate button(for the user to press it to calculate) .
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Yes, if I understand what you're asking, then yes. You need to set the form's KeyPreview property to True in the form_load event handler. And use the Form.KeyPress event or Control.KeyPres s Event.

    joedeene

    Comment

    • adarshyam
      New Member
      • Oct 2008
      • 31

      #3
      thank u for the reply.. and can you please temme whr should these be placed and how to edit it so that the dynamic textboxes gets created while enter button is presses (right now its in button1 event.. i want it to happen for enter button press) pls guide me

      Code:
      Imports System.Drawing
      
      Partial Public Class WebForm1
          Inherits System.Web.UI.Page
      
          Dim textdynamic1 As New ArrayList()
          Dim textdynamic2 As New ArrayList()
          Dim textdynamic3 As New ArrayList()
      
          Dim TxtDyn1 As TextBox()
          Dim TxtDyn2 As TextBox()
          Dim TxtDyn3 As TextBox()
          Public Event KeyPress As WebPartEventHandler
      
          Dim numofper As Integer
      
      
          Dim instance As Control
          Dim handler As WebPartEventHandler
      
          AddHandler instance.KeyPress, handler
      
          Private nonNumberEntered As Boolean = False
      
      
          ' Handle the KeyDown event to determine the type of character entered into the control.
          Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles TextBox1.KeyDown
              ' 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
              'If shift key was pressed, it's not a number.
              If Control.ModifierKeys = Keys.Shift Then
                  nonNumberEntered = True
              End If
          End Sub 'textBox1_KeyDown
      
      
          ' This event occurs after the KeyDown event and can be used 
          ' to prevent characters from entering the control.
          Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles TextBox1.KeyPress
              ' 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
          End Sub 'textBox1_KeyPress
      
      
          Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
      
         
          End Sub
      
      
          Public Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
              SessionHandler.keynumper = numofper
          
              Response.Redirect("thrpage.aspx")
      
          End Sub
      
          Public Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
              Response.Redirect("secpage.aspx")
          End Sub
      
       
       
        
          Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
              If IsPostBack Then
                  Try
      
                      If Not TextBox1.Text Is String.Empty Then
                          numofper = TextBox1.Text
      
                          ReDim TxtDyn1(numofper)
                          ReDim TxtDyn2(numofper)
                          ReDim TxtDyn3(numofper)
      
                      End If
      
                      'To create the dynamic text box and add the controls to panel, save them in session state
      
                      For i As Integer = 0 To numofper - 1
                          TxtDyn1(i) = New TextBox
                          TxtDyn1(i).EnableViewState = True
                          Panel1.Controls.Add(TxtDyn1(i))
                          textdynamic1.Add(textdynamic1)
                          TxtDyn1(i).Width = 100
                      Next
      
                      For j As Integer = 0 To numofper - 1
                          TxtDyn2(j) = New TextBox
                          TxtDyn2(j).EnableViewState = True
                          TxtDyn2(j).Width = 60
                          TxtDyn2(j).Text = j + 1
                          TxtDyn2(j).ID = "TxtDyn2_" & j
                          Panel2.Controls.Add(TxtDyn2(j))
                          textdynamic2.Add(textdynamic2)
      
                      Next
      
                      For k As Integer = 0 To numofper - 1
                          TxtDyn3(k) = New TextBox
                          TxtDyn3(k).EnableViewState = True
                          Panel3.Controls.Add(TxtDyn3(k))
                          textdynamic3.Add(textdynamic3)
                          TxtDyn3(k).Width = 60
      
                      Next
      
                  Catch ex As Exception
                      MsgBox("Enter the Number of Periods")
                  End Try
      
              End If
      
          End Sub
      End Class
      Last edited by Curtis Rutland; Oct 30 '08, 02:30 PM. Reason: ADDING [CODE] TAGS

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by adarshyam
        thank u for the reply.. and can you please temme whr should these be placed and how to edit it so that the dynamic textboxes gets created while enter button is presses (right now its in button1 event.. i want it to happen for enter button press) pls guide me

        [CODE=vB]
        Imports System.Drawing

        Partial Public Class WebForm1
        Inherits System.Web.UI.P age

        Dim textdynamic1 As New ArrayList()
        Dim textdynamic2 As New ArrayList()
        Dim textdynamic3 As New ArrayList()

        Dim TxtDyn1 As TextBox()
        Dim TxtDyn2 As TextBox()
        Dim TxtDyn3 As TextBox()
        Public Event KeyPress As WebPartEventHan dler

        Dim numofper As Integer


        Dim instance As Control
        Dim handler As WebPartEventHan dler

        AddHandler instance.KeyPre ss, handler

        Private nonNumberEntere d As Boolean = False


        ' Handle the KeyDown event to determine the type of character entered into the control.
        Private Sub textBox1_KeyDow n(ByVal sender As Object, ByVal e As System.IO.FileS ystemEventArgs) Handles TextBox1.KeyDow n
        ' Initialize the flag to false.
        nonNumberEntere d = 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.
        nonNumberEntere d = True
        End If
        End If
        End If
        'If shift key was pressed, it's not a number.
        If Control.Modifie rKeys = Keys.Shift Then
        nonNumberEntere d = True
        End If
        End Sub 'textBox1_KeyDo wn


        ' This event occurs after the KeyDown event and can be used
        ' to prevent characters from entering the control.
        Private Sub textBox1_KeyPre ss(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Form ViewPageEventAr gs) Handles TextBox1.KeyPre ss
        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntere d = True Then
        ' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True
        End If
        End Sub 'textBox1_KeyPr ess


        Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


        End Sub


        Public Sub Button3_Click(B yVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        SessionHandler. keynumper = numofper

        Response.Redire ct("thrpage.asp x")

        End Sub

        Public Sub Button2_Click(B yVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Response.Redire ct("secpage.asp x")
        End Sub




        Protected Sub Button1_Click(B yVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If IsPostBack Then
        Try

        If Not TextBox1.Text Is String.Empty Then
        numofper = TextBox1.Text

        ReDim TxtDyn1(numofpe r)
        ReDim TxtDyn2(numofpe r)
        ReDim TxtDyn3(numofpe r)

        End If

        'To create the dynamic text box and add the controls to panel, save them in session state

        For i As Integer = 0 To numofper - 1
        TxtDyn1(i) = New TextBox
        TxtDyn1(i).Enab leViewState = True
        Panel1.Controls .Add(TxtDyn1(i) )
        textdynamic1.Ad d(textdynamic1)
        TxtDyn1(i).Widt h = 100
        Next

        For j As Integer = 0 To numofper - 1
        TxtDyn2(j) = New TextBox
        TxtDyn2(j).Enab leViewState = True
        TxtDyn2(j).Widt h = 60
        TxtDyn2(j).Text = j + 1
        TxtDyn2(j).ID = "TxtDyn2_" & j
        Panel2.Controls .Add(TxtDyn2(j) )
        textdynamic2.Ad d(textdynamic2)

        Next

        For k As Integer = 0 To numofper - 1
        TxtDyn3(k) = New TextBox
        TxtDyn3(k).Enab leViewState = True
        Panel3.Controls .Add(TxtDyn3(k) )
        textdynamic3.Ad d(textdynamic3)
        TxtDyn3(k).Widt h = 60

        Next

        Catch ex As Exception
        MsgBox("Enter the Number of Periods")
        End Try

        End If

        End Sub
        End Class [/CODE]
        Its plastered all over bytes, to please use [code] tags to make everything more readable to those trying to help you. See how much nicer the above is and how it actually looks like the code we are all used to writting/debugging?
        More on tags. They're cool. Check'em out.

        Plz, U, whr, temme need to stay on your cell phone.If you can't be bothered with using real words, or even to capital a sentence then why should someone else be bothered trying to translate your SMS text abreviations? When English is not someone's native language we all make allowances. When someone is too lazy to write a full sentence it looks as if their question has no value to the writer, so it would have even less value to the reader.
        please visit the Posting Guidelines

        Your original post was asking if it is possible to use the enter key instead of a button click, to perform a calculation of numbers in two text boxes and place the result in the third. Now that has become dynamically creating the boxes and doing all the work.

        Joedeene pointed you right to the article on handling keypress. May I suggest you make some efforts at incorporating that into your code before asking someone else to do your work for you? You are not going to find a code snippet already created that exactly matches your need. You need to think through the problem... think through the goal... apply the answer already given you... and work through some trial and error. The code you have provided doesn't have any errors, it just isn't what you wanted. You need to make an effort to turn it into what you wanted. Come back with something that shows your trial and errors, shows where you were close but not quite there and ask for some help understanding that last portion resulting in error messages.

        Comment

        Working...