Monday, January 25, 2010

C# TechTip #1 Using MaxLength as AutoTab

So, for all those old VB6 programmers out there: Remember The Visual Basic Programmers Journal TechTip publications.

For me, those pamphlets where the most eagerly waited part of my VBPJ publication. Here are the links to PDF versions.

VBPJ TechTips #1
VBPJ TechTips #2
VBPJ TechTips #3
VBPJ TechTips #4
VBPJ TechTips #5
VBPJ TechTips #6
VBPJ TechTips #7
VBPJ TechTips #8
VBPJ TechTips #9
VBPJ TechTips #10
VBPJ TechTips #11
VBPJ TechTips #12

I learned sooooo much from these. The loss of these publications is just one of the reasons that VSM is just a shadow of VBPJ. Just for a walk through memory lane I read through some of them, seeing if any of them would apply to C# today. Most of the tips were not transferable, but a few could be.

So, here is the first "New" C# TechTip: Originally by Karl E. Peterson

How to add an AutoTab feature to textboxes that have a set MaxLength. Connect the textboxes' TextChanged Event to this function:


Code Snippet



  1. private void textBox_TextChanged(object sender, EventArgs e)

  2. {

  3. if((sender as TextBox).MaxLength == (sender as TextBox).TextLength) SendKeys.Send("{TAB}");

  4. }





Who knew that the Dot Net designers had included the SendKeys function!?!

2 comments:

Anonymous said...

Should be

(sender as TextBox).Text.Length)

I think anyway.

Cheers was a helpful read.

Lee Saunders said...

Actually, both ways work, see:

http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.textlength.aspx