Solved

AutoIndent (Auto Indent) in RichTextBox Control

Posted on 2000-03-25
7
946 Views
Last Modified: 2010-05-18
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.

0
Comment
Question by:msali
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2656459
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!®©


0
 

Author Comment

by:msali
ID: 2660245
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.
0
 

Author Comment

by:msali
ID: 2660250
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.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
ID: 2661229
Posting for points... ;)


Cheers!®©
0
 

Author Comment

by:msali
ID: 2662771
just a clearification....what is the purpose of LockWindowUpdate API

thanx
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2664828
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!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2664833
By the way to unlock the last LockWindowUpdate, you do:

   LockWindowUpdate 0&


Cheers!®©
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question