Solved

AutoIndent (Auto Indent) in RichTextBox Control

Posted on 2000-03-25
7
932 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now