By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

how to move cursor from one textbox to adjacent textbox using LEFT ARROW key

P: n/a
Hi friends ,
here is the sample code
private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == 13) //enter
{
txtbox2.Focus();

}

if(e.KeyChar == 27) //escape
{

txtbox.Text = "";
}

if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control is not coming inside*/
{
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
}
In the above code when executed the control is not coming inside the if(e.KeyChar == 37) loop when i pressed
left arrow key on keyboard;

how to implement the Arrow Keys in the KeyPress_event or in any other event?

hey guys plz help me out this.
-------------@@@@@@@@@@@@@---------------------------------------------------

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

Try using the KeyDown event instead:

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
textBox2.Focus();
e.Handled = true;
}
}
You will probably want to put in additional checking to make sure that's
what they really want - this will let the user move the cursor in the
textbox with the right arrow, but not the left. This would be a bit
confusing to me!

Hope this helps
- Philip
"Seash" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi friends ,
here is the sample code
private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if(e.KeyChar == 13) //enter
{
txtbox2.Focus();

}

if(e.KeyChar == 27) //escape
{

txtbox.Text = "";
}

if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control is not coming inside*/ {
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
}
In the above code when executed the control is not coming inside the if(e.KeyChar == 37) loop when i pressed left arrow key on keyboard;

how to implement the Arrow Keys in the KeyPress_event or in any other event?
hey guys plz help me out this.
-------------@@@@@@@@@@@@@------------------------------------------------ ---

Nov 15 '05 #2

P: n/a
thxs philip,
But there is a problem, in the middle of text of the textbox if i press left arrow key control straight away goes to the adjacent one, this one bugging me plz help me.

----- Philip Rieck wrote: -----
Try using the KeyDown event instead:

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
textBox2.Focus();
e.Handled = true;
}
}
You will probably want to put in additional checking to make sure that's
what they really want - this will let the user move the cursor in the
textbox with the right arrow, but not the left. This would be a bit
confusing to me!

Hope this helps
- Philip
"Seash" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi friends ,
here is the sample code
private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if(e.KeyChar == 13) //enter
{
txtbox2.Focus();
}
if(e.KeyChar == 27) //escape {
txtbox.Text = "";

}
if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control is not coming inside*/ {
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
} In the above code when executed the control is not coming inside the
if(e.KeyChar == 37) loop when i pressed
left arrow key on keyboard;
how to implement the Arrow Keys in the KeyPress_event or in any other
event? hey guys plz help me out this.

-------------@@@@@@@@@@@@@------------------------------------------------

---

Nov 15 '05 #3

P: n/a
------seash wrote-----------------------
Cursor should not move to the immidiate text box if there is text in the text box , it should pass each and every character before moving to the adgacent texbox

hope someone out there to crack this!
---seash

----- seash wrote: -----

thxs philip,
But there is a problem, in the middle of text of the textbox if i press left arrow key control straight away goes to the adjacent one, this one bugging me plz help me.

----- Philip Rieck wrote: -----
Try using the KeyDown event instead:

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
textBox2.Focus();
e.Handled = true;
}
}
You will probably want to put in additional checking to make sure that's
what they really want - this will let the user move the cursor in the
textbox with the right arrow, but not the left. This would be a bit
confusing to me!

Hope this helps
- Philip
"Seash" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi friends ,
here is the sample code
private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if(e.KeyChar == 13) //enter
{
txtbox2.Focus();
}
if(e.KeyChar == 27) //escape {
txtbox.Text = "";

}
if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control is not coming inside*/ {
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
} In the above code when executed the control is not coming inside the
if(e.KeyChar == 37) loop when i pressed
left arrow key on keyboard;
how to implement the Arrow Keys in the KeyPress_event or in any other
event? hey guys plz help me out this.

-------------@@@@@@@@@@@@@------------------------------------------------

---

Nov 15 '05 #4

P: n/a
Test the selectionstart :

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
if(textBox1.SelectionStart == 0)
{
textBox2.Focus();
e.Handled = true;
}
}
}
If your textbox is right-to-left text instead of left-to-right, then test if
the selectionstart is equal to textbox1.text.length instead of 0.
"seash" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
------seash wrote-----------------------
Cursor should not move to the immidiate text box if there is text in the text box , it should pass each and every character before moving to the
adgacent texbox
hope someone out there to crack this!
---seash

----- seash wrote: -----

thxs philip,
But there is a problem, in the middle of text of the textbox if i press left arrow key control straight away goes to the adjacent one, this
one bugging me plz help me.
----- Philip Rieck wrote: -----
Try using the KeyDown event instead:

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
textBox2.Focus();
e.Handled = true;
}
}
You will probably want to put in additional checking to make sure that's what they really want - this will let the user move the cursor in the textbox with the right arrow, but not the left. This would be a bit confusing to me!

Hope this helps
- Philip
"Seash" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
> Hi friends ,
> here is the sample code
> private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
> {
> if(e.KeyChar == 13) //enter
> {
> txtbox2.Focus();
>> }
>> if(e.KeyChar == 27) //escape

> {
>> txtbox.Text = "";

> }
>> if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control
is not coming inside*/
> {
> txtboxAdjacent.Enabled = true;
> txtboxAdjacent.Focus();
> }
> }
>>> In the above code when executed the control is not
coming inside the if(e.KeyChar == 37) loop when i pressed
> left arrow key on keyboard;
>> how to implement the Arrow Keys in the KeyPress_event or
in any other event? >> hey guys plz help me out this.




-------------@@@@@@@@@@@@@------------------------------------------------
---
>

Nov 15 '05 #5

P: n/a
Thxs Philip
this statement "e.Handled = true;" is necessary to include?, is there any cases of events not executing after events invocation? will the above statement gives any exception if events is not handled

-- seash
----- Philip Rieck wrote: ----

Test the selectionstart

private void textBox1_KeyDown(object sender
System.Windows.Forms.KeyEventArgs e

if( e.KeyCode == Keys.Left

if(textBox1.SelectionStart == 0

textBox2.Focus()
e.Handled = true

If your textbox is right-to-left text instead of left-to-right, then test i
the selectionstart is equal to textbox1.text.length instead of 0
"seash" <an*******@discussions.microsoft.com> wrote in messag
news:CE**********************************@microsof t.com..
------seash wrote----------------------
Cursor should not move to the immidiate text box if there is text in th text box , it should pass each and every character before moving to th
adgacent texbo
hope someone out there to crack this

---seas
----- seash wrote: ----
thxs philip

But there is a problem, in the middle of text of the textbox if

press left arrow key control straight away goes to the adjacent one, thi
one bugging me plz help me
----- Philip Rieck wrote: ----
Try using the KeyDown event instead

private void textBox1_KeyDown(object sender

System.Windows.Forms.KeyEventArgs e

if( e.KeyCode == Keys.Left

textBox2.Focus()
e.Handled = true

You will probably want to put in additional checking to mak
sure that' what they really want - this will let the user move the curso in th textbox with the right arrow, but not the left. This would be bi confusing to me
Hope this help - Phili "Seash" <an*******@discussions.microsoft.com> wrote in messag news:F3**********************************@microsof t.com..
Hi friends
here is the sample cod
private void txtbox_KeyPress(object sender

System.Windows.Forms.KeyPressEventArgs e

if(e.KeyChar == 13) //ente

txtbox2.Focus()

if(e.KeyChar == 27) //escap

txtbox.Text = ""

if(e.KeyChar == 37)// LEFT ARROW KEY /*here the contro

is not comin inside*

txtboxAdjacent.Enabled = true
txtboxAdjacent.Focus()

In the above code when executed the control is no
coming inside th if(e.KeyChar == 37) loop when i presse
left arrow key on keyboard
how to implement the Arrow Keys in the KeyPress_event o
in any othe event hey guys plz help me out this

-------------@@@@@@@@@@@@@-----------------------------------------------

--

Nov 15 '05 #6

P: n/a
The statement "e.Handled = true" will simply cause the base class to stop
processing the KeyDown event. This will keep it from performing the normal
action of moving the cursor. Since in this case you don't care, it's not
entirely needed.
The event will still be handled, but the textbox assumes that you are going
to do the right thing with the message, it will simply ignore the keypress.
You don't need to worry about exceptions due to unhandled events.

"seash" <an*******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
Thxs Philip,
this statement "e.Handled = true;" is necessary to include?, is there any cases of events not executing after events invocation? will the above
statement gives any exception if events is not handled?
-- seash
----- Philip Rieck wrote: -----

Test the selectionstart :

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
if(textBox1.SelectionStart == 0)
{
textBox2.Focus();
e.Handled = true;
}
}
}
If your textbox is right-to-left text instead of left-to-right, then test if the selectionstart is equal to textbox1.text.length instead of 0.
"seash" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
> ------seash wrote-----------------------
> Cursor should not move to the immidiate text box if there is text in the
text box , it should pass each and every character before moving to the adgacent texbox >> hope someone out there to crack this! > ---seash
>> ----- seash wrote: -----
>> thxs philip,

> But there is a problem, in the middle of text of the textbox if i press left arrow key control straight away goes to the adjacent one, this one bugging me plz help me. >> ----- Philip Rieck wrote: -----
>>> Try using the KeyDown event instead:
>> private void textBox1_KeyDown(object sender, > System.Windows.Forms.KeyEventArgs e)
> {
> if( e.KeyCode == Keys.Left)
> {
> textBox2.Focus();
> e.Handled = true;
> }
> }
>>> You will probably want to put in additional checking to make sure that's
> what they really want - this will let the user move the cursor
in the
> textbox with the right arrow, but not the left. This
would be a bit
> confusing to me!
>> Hope this helps

> - Philip
>>> "Seash" <an*******@discussions.microsoft.com> wrote in

message > news:F3**********************************@microsof t.com... >> Hi friends ,
>> here is the sample code
>> private void txtbox_KeyPress(object sender,

> System.Windows.Forms.KeyPressEventArgs e)
>> {
>> if(e.KeyChar == 13) //enter
>> {
>> txtbox2.Focus();
>>> }
>>> if(e.KeyChar == 27) //escape
>> {
>>> txtbox.Text = "";
>> }
>>> if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control

is not coming
> inside*/
>> {
>> txtboxAdjacent.Enabled = true;
>> txtboxAdjacent.Focus();
>> }
>> }
>>>> In the above code when executed the control is not

coming inside the
> if(e.KeyChar == 37) loop when i pressed
>> left arrow key on keyboard;
>>> how to implement the Arrow Keys in the KeyPress_event or

in any other
> event?
>>> hey guys plz help me out this.



-------------@@@@@@@@@@@@@----------------------------------------------- - > ---
>>

Nov 15 '05 #7

P: n/a
The statement "e.Handled = true" will simply cause the base class to stop
processing the KeyDown event. This will keep it from performing the normal
action of moving the cursor. Since in this case you don't care, it's not
entirely needed.
The event will still be handled, but the textbox assumes that you are going
to do the right thing with the message, it will simply ignore the keypress.
You don't need to worry about exceptions due to unhandled events.

"seash" <an*******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
Thxs Philip,
this statement "e.Handled = true;" is necessary to include?, is there any cases of events not executing after events invocation? will the above
statement gives any exception if events is not handled?
-- seash
----- Philip Rieck wrote: -----

Test the selectionstart :

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Left)
{
if(textBox1.SelectionStart == 0)
{
textBox2.Focus();
e.Handled = true;
}
}
}
If your textbox is right-to-left text instead of left-to-right, then test if the selectionstart is equal to textbox1.text.length instead of 0.
"seash" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
> ------seash wrote-----------------------
> Cursor should not move to the immidiate text box if there is text in the
text box , it should pass each and every character before moving to the adgacent texbox >> hope someone out there to crack this! > ---seash
>> ----- seash wrote: -----
>> thxs philip,

> But there is a problem, in the middle of text of the textbox if i press left arrow key control straight away goes to the adjacent one, this one bugging me plz help me. >> ----- Philip Rieck wrote: -----
>>> Try using the KeyDown event instead:
>> private void textBox1_KeyDown(object sender, > System.Windows.Forms.KeyEventArgs e)
> {
> if( e.KeyCode == Keys.Left)
> {
> textBox2.Focus();
> e.Handled = true;
> }
> }
>>> You will probably want to put in additional checking to make sure that's
> what they really want - this will let the user move the cursor
in the
> textbox with the right arrow, but not the left. This
would be a bit
> confusing to me!
>> Hope this helps

> - Philip
>>> "Seash" <an*******@discussions.microsoft.com> wrote in

message > news:F3**********************************@microsof t.com... >> Hi friends ,
>> here is the sample code
>> private void txtbox_KeyPress(object sender,

> System.Windows.Forms.KeyPressEventArgs e)
>> {
>> if(e.KeyChar == 13) //enter
>> {
>> txtbox2.Focus();
>>> }
>>> if(e.KeyChar == 27) //escape
>> {
>>> txtbox.Text = "";
>> }
>>> if(e.KeyChar == 37)// LEFT ARROW KEY /*here the control

is not coming
> inside*/
>> {
>> txtboxAdjacent.Enabled = true;
>> txtboxAdjacent.Focus();
>> }
>> }
>>>> In the above code when executed the control is not

coming inside the
> if(e.KeyChar == 37) loop when i pressed
>> left arrow key on keyboard;
>>> how to implement the Arrow Keys in the KeyPress_event or

in any other
> event?
>>> hey guys plz help me out this.



-------------@@@@@@@@@@@@@----------------------------------------------- - > ---
>>

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.