-
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
-
srinivasc_it,
Why not just set the form's AcceptButton property to the btnInstall button?
Kerry Moorman
-
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.
-
 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
-
 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
 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?
 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
-
Yes I made it work that way. Thanks
-
 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?
-
I don't program in C#, but KeyChar expects a Char value. The example in help uses:
Code:
if (e.KeyChar == (char)Keys.Return)
 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
-
By saileela in forum ASP.NET
Replies: 0
Last Post: 01-10-2007, 04:22 AM
-
By INA_ctive in forum .NET
Replies: 2
Last Post: 06-22-2005, 11:38 PM
-
Replies: 1
Last Post: 05-02-2005, 02:21 PM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Nuno Barbeita in forum VB Classic
Replies: 0
Last Post: 08-07-2000, 10: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks