Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have two text boxs.
When i am write something in first text box and press enter key then text is send to second text box Means enter key of first text box is work like a button control.
i am using this code but it gives error-

C#
private void txtwriteinenglish_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode = Keys.Enter}
    {
    }
}


Thanks in Advance.
Posted
Updated 7-Feb-12 2:13am
v3
Comments
Uday P.Singh 7-Feb-12 8:00am    
not clear! what are you trying to implement & what is the error message?
Uday P.Singh 7-Feb-12 8:12am    
why you tag Asp.net for this question? you must be asking this for Winform?

Hi Srivatsava

The keydown event is generally good to handle the Enter key.
Can you please tell what error you are getting when keydown event is used.

"text box is clear but cursor is start from second line"

When a key is pressed it is passed on to the text box.
To suppress this behavior in KeyDown event set

C#
e.SuppressKeyPress = True

and in KeyPress event set
C#
e.Handled = true


so that the key is not passed on to the text box
 
Share this answer
 
v2
Comments
mayankshrivastava 7-Feb-12 10:12am    
Thanks ...it works...
C#
private void txtwriteinenglish_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == (char)13}
        {
            txtSecondTextBox.Text = txtwriteinenglish.Text.ToString();
            txtwriteinenglish.Text = string.Empty;
            txtwriteinenglish.Focus();
            return;
        }
    }


This Piece Of Code Will Work As A Button For Your First TextBox
 
Share this answer
 
Comments
mayankshrivastava 7-Feb-12 8:10am    
Thanks ..
Aniket Yadav 7-Feb-12 8:13am    
It's My Pleasure That I am Helpful To You
mayankshrivastava 7-Feb-12 8:37am    
Hi, when i press enter in a txtwriteenglish.Text then text is goes on second text it works fine but then i want to clear txtwriteenglish.Text.The text box is clear but cursor is start from second line of txtwriteenglish.Text.
NeptuneHACK! 8-Feb-12 1:29am    
my 5, excellent Answer.
Hi, when i press enter in a txtwriteenglish.Text then text is goes on second text it works fine but then i want to clear txtwriteenglish.Text.The text box is clear but cursor is start from second line of txtwriteenglish.Text.


The Solution For This Question Is:
C#
private void txtwriteinenglish_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == (char)13}
        {
            txtSecondTextBox.Text = txtwriteinenglish.Text.ToString();
            txtwriteinenglish.Text = string.Empty;
            txtwriteinenglish.Select(0, 0);//To Set The Cursor To Default Position
            txtwriteinenglish.Focus();
            return;
        }
    }
 
Share this answer
 
textBox1.Attributes.add("onkeydown","keyDownHandle(event)");


java script code....

JavaScript
function keyDownHandle(ev)
{
if(ev.keyCode==13)
{
alert('enter key pressed');
}
}
 
Share this answer
 
Comments
Aniket Yadav 7-Feb-12 8:11am    
Hello Sanjay,

I Think You Have Not Read The Question Properly Or Not Have Seen That This Question Was About The WinForm. And In Winform I Dont Think That Javascript Works.
Sanjay K. Gupta 8-Feb-12 6:55am    
See the Tag!!!!!!!!!!!
Accept The Answer If It Has Helped You.
 
Share this answer
 
Comments
Sanjay K. Gupta 8-Feb-12 6:56am    
My 1!
If you are giving any suggestion, put it in comment not in "Solution"
Aniket Yadav 8-Feb-12 7:00am    
Yes I will keep it in mind... thanks for notifying

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900