![]() |
Simple c# question
Hi all,
Im building my first small program in Visual C# 2005 express, you enter your name in a text box and press the ok button and it changes the value of a label so it shows "hello <name entered>", its all working fine, but i would like it to change the name of the labels when you press return in the text box but im not sure how to do it, any advise would be apreciated, thanks Nez :
namespace WindowsApplication1 |
So do you want to put a focus on your button, so when you hit enter it presses the button?
|
i want it so that after typing in your name, if you press enter it preforms the same function as pressing the ok button.. example windows calculator, type '4','+' and '5' then press the enter key and it shows 6, rather than having to press the '=' button.
nez |
If you view the properties of your form, there is a property that is called "AcceptButton." Here you can select the button that you would like to be your accept button. This automatically ties the selected button event to the Enter key.
|
Also for Asp.net you can use this JavaScript in your HTML source.
:
<script language="JavaScript">Hope these help :) Also do not for get to add the onKey() function to your body onkeydown event. <body onkeydown="onkey();" ... |
One more way.
Here is one more way to handle the enter key and any other key. :)
For this example I have one button on a form. The onclick event of the button is as follows: :
private void btnClickMe_Click(object sender, System.EventArgs e)This just displays a message box when the user clicks the button. Now select the form and add this to the KeyUp event: :
private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)Simple right? You can call any key that you would like here. Just replace Keys.Enter with whatever key you would like. Basically what happens is when the user presses the enter key, it calls btnClickMe's click event and passes a blank object and empty args. You can use this to fire an event from anywhere. |
Thanks for your help Hoffmandirt, your first and third posts work perfectly for the form, but i want to have the return work in the text box, in the properties section the acceptbutton and kepup options are not there, i cannot find anything under the behaviour properties section that seems to give me that option.
nez |
If you are using the form's events to capture keys there is a property that must be set to True so that the form catches keypresses even if a child control has focus. This property is KeyPreview.
http://msdn.microsoft.com/library/de...rtiesTopic.asp |
:D :D :D
Give that man a beer :D THanks Dameon that has made my day :D It works just like i wanted it to now :D Cheers nez |
| All times are GMT -5. The time now is 5:30 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC