Auto-Indent
This is a very small program that does nothing but show you how to impliment an Auto-Indent feature. A question came up in the API section about this and I decided to put this together so I would have it in the future and I needed something constructive to do anyway :D
Let me know what you think of it.
I won't embarass myself by telling you how long it took me to get it right ;)
Orbity
Get attachment from post below
Auto-Indent #2
There was a bug in the last version where the program would get an error if the .selstart = 0 (the very first position of the text box), so I am uploading the fixed version.
Orbity
Auto-Indent #3
I have come to realize that the version I uploaded before won't work on a RichTextBox so I am posting this version which is for a RichTextBox.
Private Sub rtbMain_KeyDown(KeyCode As Integer, Shift As Integer)
'If the key pressed wasn't the enter key then just let it go
If KeyCode <> vbKeyReturn Then Exit Sub
'If the enter key was pressed at the first spot in the text box
'then just let it go
If rtbMain.SelStart = 0 Then Exit Sub
'now if we made it here we need to auto-indent, so here we go
Dim StartPos As Long, EndPos As Long
Dim PadString As String, HoldString As String
'-----------------------------------------------
'Find where the beginning of the current line is
'-----------------------------------------------
StartPos = InStrRev(vbCrLf & rtbMain.Text, vbCrLf, rtbMain.SelStart + 1)
'---------------------------------------------
'Now find the beginning of the first character
'---------------------------------------------
For EndPos = StartPos To rtbMain.SelStart + 1
'get a temporary string
HoldString = Mid$(rtbMain.Text, EndPos, rtbMain.SelStart + 1)
'see if it begins with white space
If Left$(HoldString, 1) <> " " And Left$(HoldString, 1) <> vbTab Then
'we are out of white space so exit the loop
Exit For
End If
Next
'no sense on moving forward if there isn't any white space to be had
If StartPos = EndPos Then Exit Sub
'here is our padding
PadString = Mid$(rtbMain.Text, StartPos, EndPos - StartPos)
'move to the next line and insert the PadString
rtbMain.SelText = vbNewLine & PadString
KeyCode = 0
End Sub
I don't think there is a need for an example program, since you could change the one I posted above to use a RichTextBox instead of a normal TextBox and then paste this code into the project.
If you find any bugs, or have any suggestions, please let me know by either replying to this thread or via PM.
Orbity
Auto-Indent #4
So, here is the (I hope) final update. I have rearranged the code so it is now in a function which recieves a TextBox's or RichTextBox's Text and it's .SelStart as argumnets and returns the string padding for the new line. This is done to conserve space in the KeyDown event so it will be easier to use for anything else you might want to do with it. I am posting the function here as well as an example of how to use it for both types of TextBoxes.
Here is the AutoIndent function:
'AutoIndent function coded by: Orbity
Private Function AutoIndent(strText As String, SelStart As Integer) As String
Dim StartPos As Long, EndPos As Long
Dim HoldString As String
'Find where the beginning of the current line is
StartPos = InStrRev(vbCrLf & strText, vbCrLf, SelStart + 1)
'Now find the beginning of the first character
For EndPos = StartPos To SelStart + 1
'get a temporary string
HoldString = Mid$(strText, EndPos, SelStart + 1)
'see if it begins with white space
If Left$(HoldString, 1) <> " " And Left$(HoldString, 1) <> vbTab Then
'we are out of white space so exit the loop
Exit For
End If
Next
'no sense on moving forward if there isn't any white space to be had
If StartPos = EndPos Then
AutoIndent = ""
Else
'here is our padding
AutoIndent = Mid$(strText, StartPos, EndPos - StartPos)
End If
End Function
Here is how to use it with a normal TextBox:
'use the AutoIndent function with a normal TextBox
Private Sub txtMain_KeyDown(KeyCode As Integer, Shift As Integer)
Dim strPad As String
'lets be sure things are ok before we proceed
If KeyCode = vbKeyReturn And txtMain.SelStart <> 0 Then
'get the pad srting to insert
strPad = AutoIndent(txtMain.Text, txtMain.SelStart)
'allow the keypress event to fire and insert the new line character
DoEvents
'now we just put in the Padding
txtMain.SelText = strPad
End If
End Sub
And here is how to use it for a RichTextBox:
'use the AutoIndent function with a RichTextBox
Private Sub rtbMain_KeyDown(KeyCode As Integer, Shift As Integer)
Dim strPad As String
'lets be sure things are ok before we proceed
If KeyCode = vbKeyReturn And rtbMain.SelStart <> 0 Then
strPad = AutoIndent(rtbMain.Text, rtbMain.SelStart)
'now we just put in the Padding
rtbMain.SelText = vbNewLine & strPad
KeyCode = 0
End If
End Sub
You can see here that the KeyDown events of the TextBoxes now have plenty of room to do other things, if needed. This should make things easier for anyone who has used one of the previous versions.
As always, if you find any bugs, or have any suggestions, please let me know by either replying to this thread or via PM.
Orbity