Auto tab

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

Guest

This is for a Win form.

When the field reaches the max limit I want the cursor to go to the next
field.

Saving the user from pressing the tab key.
 
Mike L,

What do you mean when the field reaches the max limit? Do you mean a
text field? If that is the case, then you have to check when the text
changes (through the TextChanged event) and when you reach the max limit for
the textbox, you can call the GetNextControl method on the textbox, and then
set the focus on that (through the Focus method).

Hope this helps.
 
Hi,

I don;t remember seeing this feature in the framework, so you most probably
will have to implement it. It's easy you would have to use a KeyPress
handler (or another event that fires when a key is pressed) and check if
the length of the text vs the size, if needed move to the next.
What I would do is I would create a handler that will dynamically be
assigned to the "next" control:

This is a very basic struct, just to give you an idea:


EventHandler moveNext = new EventHandler ( TheMethod );

form_load( ... )
firstControl.KeyPress += moveNext;

void TheMethod( object sender , ... )
{
TextBox cur = (TextBox) sender;
if ( cur.Text.Lenght == cur.MaxLength )
{
cur.KeyPress -=moveNext ;
// get the next one, maybe this can be improved
foreach( Control c in Controls )
if ( c.TabIndex == cur.TabIndex+1 ) //gotcha
c.KeyPress += moveNext ;
}
}



cheers,
 
Ignacio,

You should replace that loop with a call to the GetNextControl method on
the Control that sends the event. It's much easier.
 
Hi,

You are right , I missed it :)

Also to notice is that he should be looking only for TextBox instances , if
a combobox appear it should be ignored altogether.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Nicholas Paldino said:
Ignacio,

You should replace that loop with a call to the GetNextControl method
on the Control that sends the event. It's much easier.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I don;t remember seeing this feature in the framework, so you most
probably will have to implement it. It's easy you would have to use a
KeyPress handler (or another event that fires when a key is pressed) and
check if the length of the text vs the size, if needed move to the next.
What I would do is I would create a handler that will dynamically be
assigned to the "next" control:

This is a very basic struct, just to give you an idea:


EventHandler moveNext = new EventHandler ( TheMethod );

form_load( ... )
firstControl.KeyPress += moveNext;

void TheMethod( object sender , ... )
{
TextBox cur = (TextBox) sender;
if ( cur.Text.Lenght == cur.MaxLength )
{
cur.KeyPress -=moveNext ;
// get the next one, maybe this can be improved
foreach( Control c in Controls )
if ( c.TabIndex == cur.TabIndex+1 ) //gotcha
c.KeyPress += moveNext ;
}
}



cheers,
 
Your answer gave me the direction I needed.

With my own modification here is the best solution for my problem.

On TextChanged on the textbox, check the text length to the maxlenght, if
equal then perform code.

private void txtDealerNum_TextChanged(object sender, System.EventArgs e)
{
if (txtDealerNum.Text.Length == txtDealerNum.MaxLength)
{
frmDealerSearch f = new frmDealerSearch(this);
if( f.ShowDialog() == DialogResult.Cancel )
{
txtDealerNum.Focus();
}
else
{
txtLicNum.Focus();
}
}
}




Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You are right , I missed it :)

Also to notice is that he should be looking only for TextBox instances , if
a combobox appear it should be ignored altogether.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Nicholas Paldino said:
Ignacio,

You should replace that loop with a call to the GetNextControl method
on the Control that sends the event. It's much easier.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I don;t remember seeing this feature in the framework, so you most
probably will have to implement it. It's easy you would have to use a
KeyPress handler (or another event that fires when a key is pressed) and
check if the length of the text vs the size, if needed move to the next.
What I would do is I would create a handler that will dynamically be
assigned to the "next" control:

This is a very basic struct, just to give you an idea:


EventHandler moveNext = new EventHandler ( TheMethod );

form_load( ... )
firstControl.KeyPress += moveNext;

void TheMethod( object sender , ... )
{
TextBox cur = (TextBox) sender;
if ( cur.Text.Lenght == cur.MaxLength )
{
cur.KeyPress -=moveNext ;
// get the next one, maybe this can be improved
foreach( Control c in Controls )
if ( c.TabIndex == cur.TabIndex+1 ) //gotcha
c.KeyPress += moveNext ;
}
}



cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


This is for a Win form.

When the field reaches the max limit I want the cursor to go to the next
field.

Saving the user from pressing the tab key.
 
Back
Top