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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
-------------@@@@@@@@@@@@@---------------------------------------------------
 
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 said:
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
 
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 said:
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(); inside*/
{
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
}
if(e.KeyChar == 37) loop when i pressed
 
------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 said:
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(); inside*/
{
txtboxAdjacent.Enabled = true;
txtboxAdjacent.Focus();
}
}
if(e.KeyChar == 37) loop when i pressed
 
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 said:
------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.
 
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 said:
------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
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
 
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 said:
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?
 
Back
Top