![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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
{
public partial class Form1 : Form
{
string name;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label2.Text = "Hello";
label3.Text = name;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
name = textBox1.Text;
}
}
}
__________________
atariboy.wordpress.com |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
So do you want to put a focus on your button, so when you hit enter it presses the button?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Programmer
|
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
__________________
atariboy.wordpress.com |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Also for Asp.net you can use this JavaScript in your HTML source.
<script language="JavaScript">
function onkey()
{
if (window.event.keyCode==13)
{
Form1.BUTTON_ID_HERE.focus();
}
}
</script>Hope these help ![]() Also do not for get to add the onKey() function to your body onkeydown event. <body onkeydown="onkey();" ... |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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)
{
MessageBox.Show("Hello World!");
}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)
{
if(e.KeyCode == Keys.Enter)
{
this.btnClickMe_Click(new System.Object(), System.EventArgs.Empty);
}
}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. |
|
|
|
|
|
#7 |
|
Programmer
|
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
__________________
atariboy.wordpress.com |
|
|
|
|
|
#8 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#9 |
|
Programmer
|
Give that man a beer ![]() THanks Dameon that has made my day ![]() It works just like i wanted it to now ![]() Cheers nez
__________________
atariboy.wordpress.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|