Link to home
Start Free TrialLog in
Avatar of msali
msali

asked on

AutoIndent (Auto Indent) in RichTextBox Control

Hi Experts,
I am making a small customized Editor in vb6 and am using a RichTextBox.  Every thing is fine so far... but now i am stuck at a point where when the user presses enter key the cursor should move to then next line and automatically indent to the number of tabs pressed in the previous line... Just like VB's own IDE and Word etc.

i have looked at SelTabCount, SelTabs, SelIndent properties but am totally lost and have no iedea where to go from here onwards....

a quick response will be much appriciated.

thanx,
msa.

Avatar of mcrider
mcrider

Here's a quick example you can try... It may not be perfect, but it will give you a general idea...

1) Start a new project.

2) Add a richtextbox control to the form.

3) Add the following code to the declarations section of the form:

'-------------------------------------------------------------------------------
    Dim arrTabStop() As Boolean
    Dim TabCounter As Long
    Dim InhibitTabCount As Boolean
    Dim EnterProcessing As Boolean
    Private Declare Function LockWindowUpdate Lib "user32" _
        (ByVal hwndLock As Long) As Long
    Private Sub Form_Load()
        RichTextBox1.Text = ""
    End Sub
    Private Sub RichTextBox1_GotFocus()
          'Store the TabStop property for each control on the
          'form and then set the TabStop property of each
          'control to False
          ReDim arrTabStop(0 To Controls.Count - 1) As Boolean
          For i = 0 To Controls.Count - 1
             arrTabStop(i) = Controls(i).TabStop
             Controls(i).TabStop = False
          Next
    End Sub
    Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
        Dim iVal As Long
       
            Select Case KeyAscii
                Case 8 'BACKSPACE
                    With RichTextBox1
                        If .GetLineFromChar(.SelStart) = 0 Then
                            If .SelStart <= TabCounter + 1 Then
                                TabCounter = TabCounter - 1
                            End If
                        Else
                            If InhibitTabCount = False Then
                                TabCounter = TabCounter - 1
                            Else
                                If InStr(.SelStart - 2, .Text, vbCrLf) > 0 Then
                                    TabCounter = TabCounter - 1
                                End If
                            End If
                        End If
                        If TabCounter < 0 Then TabCounter = 0
                    End With
                   
                Case 9 'TAB
                    If InhibitTabCount = False Then
                        TabCounter = TabCounter + 1
                    End If
                   
                Case 13 'ENTER
                    If EnterProcessing = False Then
                        EnterProcessing = True
                        InhibitTabCount = True
                        SendKeys "~", True
                        LockWindowUpdate RichTextBox1.hWnd
                        For iVal = 1 To TabCounter
                            SendKeys "{TAB}", True
                        Next iVal
                        LockWindowUpdate 0&
                        InhibitTabCount = False
                        EnterProcessing = False
                        KeyAscii = 0
                    End If
                   
                Case Else 'ALL OTHER KEYS
                    InhibitTabCount = True
            End Select
    End Sub
    Private Sub RichTextBox1_LostFocus()
          'Restore the Tabstop property for each control on the form
          For i = 0 To Controls.Count - 1
             Controls(i).TabStop = arrTabStop(i)
          Next
    End Sub
'-------------------------------------------------------------------------------


4) Run the program and then type:

   {TAB}{TAB}This is a test

When you hit the ENTER key, the next line will start indented 2 tabs...


Play around with it...


Cheers!®©


Avatar of msali

ASKER

Thanx for the code it works for me so far... and will modify it to my custom need....

would like to give u points for it .... so pls lock it so that i can give u points.

later,
msa.
Avatar of msali

ASKER

Thanx for the code it works for me so far... and will modify it to my custom need....

would like to give u points for it .... so pls lock it so that i can give u points.

later,
msa.
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msali

ASKER

just a clearification....what is the purpose of LockWindowUpdate API

thanx
LockWindowUpdate freezes the update of the hWnd passed to it... In this case, it freezes the RTbox so that the tabs sent to the RTbox are not seen "one at a time"... All of the tabs are sent with the control locked and then it's unlocked to refresh the control.


Thanks for the points! Glad I could help!


Cheers!®©
By the way to unlock the last LockWindowUpdate, you do:

   LockWindowUpdate 0&


Cheers!®©