Solved

AutoIndent (Auto Indent) in RichTextBox Control

Posted on 2000-03-25
7
965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses
Course of the Month5 days, 11 hours left to enroll

626 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