Home » Microsoft TechnologiesRSS

How to set the Tab size in a TextBox Control

Hi All,
I have a multiline text box in which I'd like the Tab char to be 4
chars long rather than the standard 8.
I've seen a few postings suggesting the use of the EM_SETTABSTOPS
message but that's not exactly what I want to do (unless I don't quite
understand and I'm happy for someone to explain).
I don't know in advance how many tabs there will be on a line, I just
want that each time the user hits the tab key, it moves the caret 4
spaces right rather than 8.
Thanks for your help.
JB

 

8 Answers Found

 

Answer 1

The only reliable way to implement this is to intercept keystrokes and substitute 4 space characters for one tab  character.  Use the KeyPress or KeyDown event of the text  box. If the key character is not tab, pass it on.  If it is tab, cancel the keystroke and modify the text at the current insertion point to include the 4 spaces.
 

Answer 2

Hi JB Brossard,

I tried this with 3 wide TextBoxes on a FORM but the TAB key moves to the next TextBox. :-(

Using the F1 key instead I tried this.>>

 

Public Class Form1

 Private Sub TextBoxes_KeyDown _
 (ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyEventArgs) _
  Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown

  If e.KeyCode = Keys.F1 Then
   TextBox1.AppendText(Microsoft.VisualBasic.ControlChars.Tab)
  End If

 End Sub
End Class

 

 

Regards,

John

 

 

Answer 3

Pretty sure you want EM_SETTABSTOPS.  You don't need to set  an array of stops.

Public Class Form1
  
Const EM_SETTABSTOPS As Integer = &HCB
  
Declare Function SendMessageA Lib "user32" (ByVal TBHandle As IntPtr, _
                                             
ByVal EM_SETTABSTOPS As Integer, _
                                             
ByVal wParam As Integer, _
                                             
ByRef lParam As IntegerAs Boolean
  Private Sub Form1_Load(ByVal sender As ObjectByVal e As EventArgs) Handles MyBase.Load
    
Dim TabStop As Integer = 16 'Tab times 4
    SendMessageA(TextBox1.Handle, EM_SETTABSTOPS, 1, TabStop)
  
End Sub
End
 Class

 

Answer 4

Hi JB Brossard,

I tried this with 3 wide TextBoxes on a FORM but the tab  key moves to the next TextBox. :-(


 

Proposed the reply from John as answer because of his sentence above, using the tab key in two different ways on a windows forms would lead to unpredictable behaviour, which is called by a user a bug.

 


Success
Cor
 

Answer 5

@John Anthony Oliver and Cor Lighthert: You can prevent the "Tab key focuses next control" behavior by setting TextBox.AcceptsTab = True.  This is not unpredictable behaviour.  The tab  key was originally intended to indent text  and works this way in all text processing software.

@JB Brossard: I just tested JohnWein's code.  Works like a charm.

@JohnWein: Thanks for the tip!

 

-Hazelrah

 

Answer 6

@John Anthony Oliver and Cor Lighthert: You can prevent the "Tab key focuses next control" behavior by setting TextBox.AcceptsTab = True.  This is not unpredictable behaviour.  The tab  key was originally intended to indent text  and works this way in all text processing software.

@JB Brossard: I just tested JohnWein's code.  Works like a charm.

@JohnWein: Thanks for the tip!

 

-Hazelrah

Hi Hazelrah,

Would you believe it, all this time  I've been using Vb.Net ( since around 2003 - 2004 )

and not even looked at AcceptsTab for a TextBox.

Thanks for your input to this thread.  :-)

 

I should have used Ctrl + Tab in my code instead like this as of course Shift + Tab

goes to the previous control  and Alt + Tab switches between active applications.>>

 

Public Class Form1

 Private Sub TextBoxes_KeyDown _
 (ByVal sender As Object, _
 ByVal e As System.Windows.Forms.KeyEventArgs) _
 Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown

 If e.Modifiers = Keys.Control And e.KeyCode = Keys.Tab Then
 TextBox1.AppendText(Microsoft.VisualBasic.ControlChars.Tab)
 End If

 End Sub
End Class

 

 

Regards,

John

 

 

Answer 7

Thanks JohnWein, you were right, that's exactly what I needed. It works perfectly.
 

Answer 8

Hi ALL,

I suggested in connect.microsoft.com here a TabSize property.>>

https://connect.microsoft.com/VisualStudio/feedback/details/564973/how-about-adding-tabsize-as-a-property-for-a-textbox-or-and-a-richtextbox-please

This is just an update, Microsoft will not be doing so as it is "resolved" as "Won't Fix".    :-(


Do you want Netherlands ( Holland ) or Spain to win the 2010 World Cup? Give a reason if you want to. :-)
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter