Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 1st, 2005, 9:50 AM   #1
nez
Programmer
 
nez's Avatar
 
Join Date: May 2005
Location: Plymouth UK
Posts: 34
Rep Power: 0 nez is on a distinguished road
Send a message via MSN to nez
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;
        }
    }
}
n.b This is what i have so far, i tried using an if statement, but i couldnt quite get it right, im sure there must be some simple line or two i can put in the textbox1 section.
__________________
atariboy.wordpress.com
nez is offline   Reply With Quote
Old Jul 1st, 2005, 10:53 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Jul 1st, 2005, 11:34 AM   #3
nez
Programmer
 
nez's Avatar
 
Join Date: May 2005
Location: Plymouth UK
Posts: 34
Rep Power: 0 nez is on a distinguished road
Send a message via MSN to nez
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
nez is offline   Reply With Quote
Old Jul 1st, 2005, 2:58 PM   #4
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
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.
hoffmandirt is offline   Reply With Quote
Old Jul 1st, 2005, 3:02 PM   #5
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
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();" ...
hoffmandirt is offline   Reply With Quote
Old Jul 1st, 2005, 4:46 PM   #6
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
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.
hoffmandirt is offline   Reply With Quote
Old Jul 1st, 2005, 6:06 PM   #7
nez
Programmer
 
nez's Avatar
 
Join Date: May 2005
Location: Plymouth UK
Posts: 34
Rep Power: 0 nez is on a distinguished road
Send a message via MSN to nez
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
nez is offline   Reply With Quote
Old Jul 1st, 2005, 6:21 PM   #8
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Jul 1st, 2005, 6:29 PM   #9
nez
Programmer
 
nez's Avatar
 
Join Date: May 2005
Location: Plymouth UK
Posts: 34
Rep Power: 0 nez is on a distinguished road
Send a message via MSN to nez


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
nez is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC