Capturing Text box key press event in VS.net 2005 windows application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Capturing Text box key press event in VS.net 2005 windows application

  1. #1
    Join Date
    Jul 2006
    Posts
    91

    Capturing Text box key press event in VS.net 2005 windows application

    Hi

    I have a windows form application on which I placed a textbox (txtLocation) and a button (btnInstall) from the toolbox. I am trying to give it the following functionality:-

    Once user types in some text in the “txtLocation TextBox” and presses an “Enter” key on the Keyboard. It should Invoke/fire “btnInsatll_click()” event of the button. See the code below:-

    private void txtLocation_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar != (char)13)
    e.Handled = true //filtering out all other keys except “Enter” key

    else //if “enter” key is pressed
    btnInstall_Click( ); //what arguments must be passed here?
    }

    private void btnInstall_click(object sender, EventArgs e)
    {
    }

    I have few Questions on this:-

    1.What is the ASCII value of “Enter” key for vista operating system is it 13?

    2.The “btnInsatll_click()” event must be fired only when “Enter” key on the keyboard is pressed, if any other key is pressed it should not fire. Is that the correct way of filtering all other keys in the above code?

    3.What arguments must be passed when “btnInsatll_click()” is called within “keyPress” event of the textbox.

    Please correct me or refer me to a good resource on the web

    Thanks

  2. #2
    Join Date
    Dec 2004
    Posts
    163
    srinivasc_it,

    Why not just set the form's AcceptButton property to the btnInstall button?

    Kerry Moorman

  3. #3
    Join Date
    Jul 2006
    Posts
    91
    Thanks. I din't know that its that easy and straight forward . But, I will also try to make it work in the way I was trying just for my knowledge.

  4. #4
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Quote Originally Posted by srinivasc_it
    Thanks. I din't know that its that easy and straight forward . But, I will also try to make it work in the way I was trying just for my knowledge.
    The best way is to wrap the code in the click event into a sub and call that sub from the click event. Then your KeyPress code can just call the sub rather than trying to pass the correct parameters to the click event.

    ...joe

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by srinivasc_it
    1.What is the ASCII value of “Enter” key for vista operating system is it 13?
    Yes, but rather than use that you would use Keys.Enter. Example
    Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Asc(e.KeyChar) = Keys.Enter Then
         btnInstall.PerformClick()
    End If
    End Sub
    Quote Originally Posted by srinivasc_it
    2.The “btnInsatll_click()” event must be fired only when “Enter” key on the keyboard is pressed, if any other key is pressed it should not fire. Is that the correct way of filtering all other keys in the above code?
    On the surface it looks OK....are you have a problem with this?
    Quote Originally Posted by srinivasc_it
    3.What arguments must be passed when “btnInsatll_click()” is called within “keyPress” event of the textbox.
    A. Don't use button_click - use my example. B: You don't have to pass it anything.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Jul 2006
    Posts
    91
    Yes I made it work that way. Thanks

  7. #7
    Join Date
    May 2008
    Posts
    1

    Question

    Quote Originally Posted by srinivasc_it
    Hi

    I have a windows form application on which I placed a textbox (txtLocation) and a button (btnInstall) from the toolbox. I am trying to give it the following functionality:-

    Once user types in some text in the “txtLocation TextBox” and presses an “Enter” key on the Keyboard. It should Invoke/fire “btnInsatll_click()” event of the button. See the code below:-

    private void txtLocation_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar != (char)13)
    e.Handled = true //filtering out all other keys except “Enter” key

    else //if “enter” key is pressed
    btnInstall_Click( ); //what arguments must be passed here?
    }

    private void btnInstall_click(object sender, EventArgs e)
    {
    }

    I have few Questions on this:-

    1.What is the ASCII value of “Enter” key for vista operating system is it 13?

    2.The “btnInsatll_click()” event must be fired only when “Enter” key on the keyboard is pressed, if any other key is pressed it should not fire. Is that the correct way of filtering all other keys in the above code?

    3.What arguments must be passed when “btnInsatll_click()” is called within “keyPress” event of the textbox.

    Please correct me or refer me to a good resource on the web

    Thanks










    Hi,
    I have the same problem, good to know smone below solved it, hope you can help me out, i am calling the button click event from Keypress event, below is my code, just need to know my mistake, I am getting no error, but nothin is happening!


    public void keypress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == 13)
    {
    button1.PerformClick();

    }
    }

    Do i need to call this keypress event anywhere else in the app?

  8. #8
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    I don't program in C#, but KeyChar expects a Char value. The example in help uses:

    Code:
     if (e.KeyChar == (char)Keys.Return)
    Quote Originally Posted by Chetansehgal
    Hi,
    I have the same problem, good to know smone below solved it, hope you can help me out, i am calling the button click event from Keypress event, below is my code, just need to know my mistake, I am getting no error, but nothin is happening!


    public void keypress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == 13)
    {
    button1.PerformClick();

    }
    }

    Do i need to call this keypress event anywhere else in the app?

    ...joe

Similar Threads

  1. calling windows application in windows service
    By saileela in forum ASP.NET
    Replies: 0
    Last Post: 01-10-2007, 05:22 AM
  2. Replies: 2
    Last Post: 06-23-2005, 12:38 AM
  3. Replies: 1
    Last Post: 05-02-2005, 03:21 PM
  4. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  5. Replies: 0
    Last Post: 08-07-2000, 11:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center