SelectNextControl();

  • Thread starter Thread starter Adrian
  • Start date Start date
A

Adrian

I want to move to the next text box when enter is pressed.
The text boxes are in tab order and start with 0.
Could someone please correct the error line for me?

Many thanks.

private void next(Object o, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
for(int k = 0; k<20;k++)
{
if(Controls[k] is TextBox)
SelectNextControl(); // error
}
}
}
 
Adrian said:
I want to move to the next text box when enter
is pressed.

This works for me, and avoids having to loop over the controls.

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
bool bFirstTime = true;

if (e.KeyChar == (char) 13)
{
while (bFirstTime || !(this.ActiveControl is TextBox))
{
this.ProcessTabKey(true);
bFirstTime = false;
}
}
}

P.
 
Hi Sijin,

Thank you for your response, however:

It doesn't work. The cursor jumps to the same textbox
when enter is pressed in the different text boxes, not to the
one that is one up in the tab order.

*****************************
Sijin Joseph said:
Try

SelectNextControl(Controls[k],true,true,false,true);

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Adrian said:
I want to move to the next text box when enter is pressed.
The text boxes are in tab order and start with 0.
Could someone please correct the error line for me?

Many thanks.

private void next(Object o, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
for(int k = 0; k<20;k++)
{
if(Controls[k] is TextBox)
SelectNextControl(); // error
}
}
}
 
Hi Paul,

I ended up coding like so:

private void returnHandler(Object o, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
valuesToDataBase( );
this.ProcessTabKey(true);
}
}

Because it is always going to be a text box.
OK? Or am I missing something?

Adrian.

*******************************
 
Adrian said:
I ended up coding like so:
[...]
Because it is always going to be a text box.
OK? Or am I missing something?

Your code is equivalent to mine, if the form contains only text boxes.
Otherwise, a control that isn't a text box will eventually get the
focus.

P.
 
Hi Paul,

That risk doesn't arise because of
this.textBox**.KeyPress += etc.(this.returnHandler)
A non text box doesn't need a returnHandler.

Adrian.
**********************************
Paul E Collins said:
Adrian said:
I ended up coding like so:
[...]
Because it is always going to be a text box.
OK? Or am I missing something?

Your code is equivalent to mine, if the form contains only text boxes.
Otherwise, a control that isn't a text box will eventually get the
focus.

P.
 
Back
Top