Custom Textbox AutoTab

G

Guest

I built 3 textbox classes. The auto tab does not tab to the next control in
the tab list. No errors, and the code steps through
"this.SelectNextControl(this, true, true, true, true);".

What is the code missing, so the auto tab will go to the next control in the
tab list when the max length value is reached?

class ClassTextBox : System.Windows.Forms.TextBox
{
protected override void OnEnter(System.EventArgs e)
{
base.OnEnter(e);
SelectAll();
}
}

class ClassTextBoxNum : ClassTextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) &&
!Char.IsControl(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}

}

class ClassTextBoxNumTab : ClassTextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);

if (this.Text.Length == this.MaxLength)
{
this.SelectNextControl(this, true, true, true, true);
}
}

}
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Thanks for your post.

I am not sure I understand your problem context very well. Can you give
some more detailed description on it?

Currently, after reviewing your sample code snippet, I think the problem
may be that the this.SelectNextControl(this, true, true, true, true)
statement does not work in ClassTextBoxNumTab, yes?

To resolve this problem, I can provide 2 solutions:
1. Use ProcessDialogKey(Keys.Tab) method to simulate the tab key function
2. first find the parent form, then inovke Form.SelectNextControl method.

Sample code snippet listed below:
class ClassTextBoxNumTab : ClassTextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);

if (this.Text.Length == 20)
{
//this.ProcessDialogKey(Keys.Tab); // this is the first solution
this.FindForm().SelectNextControl(this, true, true, true, true); //this
is the second solution
}
}
}
Note: I modified this.MaxLength to 20, so that we can achieve the
limitation ASAP.

These 2 solutions works well on my side. Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top