RTB - auto indent to match the tab stop of the line above 
Author Message
 RTB - auto indent to match the tab stop of the line above

OK - I'm using a Rich Text Box -
If I use the tab key to indent a line, then I start typing - what I want is,
that, when I hit enter - giving the application a vbcrlf to go to the next
line - I need the starting point to match the tab position of the line I
just left.

Does anyone know how this can be done?

If this isn't clear - here's a more graphical type of

First line
vbtab - rtb.seltext="This is my text" --- here - when I hit the enter key -
I want the tab stop (for the start of the text) of the next line to match
the tab stop on the line above it.....

Any ideas?



Fri, 27 Feb 2004 05:17:05 GMT  
 RTB - auto indent to match the tab stop of the line above
Using the KeyPress event, override the default carriage return handler
and insert your own (or mine as the case may be):

Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
Dim i As Long
  If KeyAscii = 13 Then
    KeyAscii = 0
    RichTextBox1.SelText = vbCrLf
    If RichTextBox1.SelStart - 2 > 0 Then
      i = InStrRev(RichTextBox1.Text, vbCrLf, _
        RichTextBox1.SelStart - 2, vbBinaryCompare)
      Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
        RichTextBox1.SelText = vbTab
        i = i + 1
      Loop
    End If
  End If
End Sub

Howard Henry Schlunder


Quote:
> OK - I'm using a Rich Text Box -
> If I use the tab key to indent a line, then I start typing - what I
want is,
> that, when I hit enter - giving the application a vbcrlf to go to the
next
> line - I need the starting point to match the tab position of the line
I
> just left.

> Does anyone know how this can be done?

> If this isn't clear - here's a more graphical type of

> First line
> vbtab - rtb.seltext="This is my text" --- here - when I hit the enter
key -
> I want the tab stop (for the start of the text) of the next line to
match
> the tab stop on the line above it.....

> Any ideas?



Fri, 27 Feb 2004 06:23:51 GMT  
 RTB - auto indent to match the tab stop of the line above
Oops, found a bug which would occured on the first line.  Use this
instead:

Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
Dim i As Long
  If KeyAscii = 13 Then
    KeyAscii = 0
    RichTextBox1.SelText = vbCrLf
    If RichTextBox1.SelStart - 2 > 0 Then
      i = InStrRev(RichTextBox1.Text, vbCrLf, _
        RichTextBox1.SelStart - 2, vbBinaryCompare)
      If i = 0 Then i = -1
      Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
        RichTextBox1.SelText = vbTab
        i = i + 1
      Loop
    End If
  End If
End Sub

Howard Henry Schlunder



Quote:
> Using the KeyPress event, override the default carriage return handler
> and insert your own (or mine as the case may be):

> Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> Dim i As Long
>   If KeyAscii = 13 Then
>     KeyAscii = 0
>     RichTextBox1.SelText = vbCrLf
>     If RichTextBox1.SelStart - 2 > 0 Then
>       i = InStrRev(RichTextBox1.Text, vbCrLf, _
>         RichTextBox1.SelStart - 2, vbBinaryCompare)
>       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
>         RichTextBox1.SelText = vbTab
>         i = i + 1
>       Loop
>     End If
>   End If
> End Sub

> Howard Henry Schlunder



> > OK - I'm using a Rich Text Box -
> > If I use the tab key to indent a line, then I start typing - what I
> want is,
> > that, when I hit enter - giving the application a vbcrlf to go to
the
> next
> > line - I need the starting point to match the tab position of the
line
> I
> > just left.

> > Does anyone know how this can be done?

> > If this isn't clear - here's a more graphical type of

> > First line
> > vbtab - rtb.seltext="This is my text" --- here - when I hit the
enter
> key -
> > I want the tab stop (for the start of the text) of the next line to
> match
> > the tab stop on the line above it.....

> > Any ideas?



Fri, 27 Feb 2004 06:31:06 GMT  
 RTB - auto indent to match the tab stop of the line above
This NT is great!!
Thanks Howard!

David



Quote:
> Oops, found a bug which would occured on the first line.  Use this
> instead:

> Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> Dim i As Long
>   If KeyAscii = 13 Then
>     KeyAscii = 0
>     RichTextBox1.SelText = vbCrLf
>     If RichTextBox1.SelStart - 2 > 0 Then
>       i = InStrRev(RichTextBox1.Text, vbCrLf, _
>         RichTextBox1.SelStart - 2, vbBinaryCompare)
>       If i = 0 Then i = -1
>       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
>         RichTextBox1.SelText = vbTab
>         i = i + 1
>       Loop
>     End If
>   End If
> End Sub

> Howard Henry Schlunder



> > Using the KeyPress event, override the default carriage return handler
> > and insert your own (or mine as the case may be):

> > Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> > Dim i As Long
> >   If KeyAscii = 13 Then
> >     KeyAscii = 0
> >     RichTextBox1.SelText = vbCrLf
> >     If RichTextBox1.SelStart - 2 > 0 Then
> >       i = InStrRev(RichTextBox1.Text, vbCrLf, _
> >         RichTextBox1.SelStart - 2, vbBinaryCompare)
> >       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
> >         RichTextBox1.SelText = vbTab
> >         i = i + 1
> >       Loop
> >     End If
> >   End If
> > End Sub

> > Howard Henry Schlunder



> > > OK - I'm using a Rich Text Box -
> > > If I use the tab key to indent a line, then I start typing - what I
> > want is,
> > > that, when I hit enter - giving the application a vbcrlf to go to
> the
> > next
> > > line - I need the starting point to match the tab position of the
> line
> > I
> > > just left.

> > > Does anyone know how this can be done?

> > > If this isn't clear - here's a more graphical type of

> > > First line
> > > vbtab - rtb.seltext="This is my text" --- here - when I hit the
> enter
> > key -
> > > I want the tab stop (for the start of the text) of the next line to
> > match
> > > the tab stop on the line above it.....

> > > Any ideas?



Fri, 27 Feb 2004 13:01:40 GMT  
 RTB - auto indent to match the tab stop of the line above
Another possibility is to use the SelIndent property to control this. This is rough --
VERY ROUGH -- but it should give the concept I'm after

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = vbKeyEscape Then
    With RichTextBox1
      .SelIndent = .SelIndent - 500
    End With
  End If
End Sub

Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
  With RichTextBox1
    If KeyAscii = 13 Then
      KeyAscii = 0
      currentindent = .SelIndent
      .SelText = vbNewLine
    ElseIf KeyAscii = vbKeyTab Then
      KeyAscii = 0
      .SelIndent = .SelIndent + 500
    End If
  End With
End Sub

Here, pressing the Tab key increases the amount of indenting, 500 units (twips I think),
for each press. Pressing the Escape key reduces the indenting by 500 units. Obviously,
this needs work, but it doesn't have the small "flaw(?)" that your code does -- the user
can delete the indent in the middle of a paragraph thus breaking the pattern. An
established indent can't be changed by the user (unless you allow it).

Rick



Quote:
> Oops, found a bug which would occured on the first line.  Use this
> instead:

> Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> Dim i As Long
>   If KeyAscii = 13 Then
>     KeyAscii = 0
>     RichTextBox1.SelText = vbCrLf
>     If RichTextBox1.SelStart - 2 > 0 Then
>       i = InStrRev(RichTextBox1.Text, vbCrLf, _
>         RichTextBox1.SelStart - 2, vbBinaryCompare)
>       If i = 0 Then i = -1
>       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
>         RichTextBox1.SelText = vbTab
>         i = i + 1
>       Loop
>     End If
>   End If
> End Sub

> Howard Henry Schlunder



> > Using the KeyPress event, override the default carriage return handler
> > and insert your own (or mine as the case may be):

> > Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> > Dim i As Long
> >   If KeyAscii = 13 Then
> >     KeyAscii = 0
> >     RichTextBox1.SelText = vbCrLf
> >     If RichTextBox1.SelStart - 2 > 0 Then
> >       i = InStrRev(RichTextBox1.Text, vbCrLf, _
> >         RichTextBox1.SelStart - 2, vbBinaryCompare)
> >       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
> >         RichTextBox1.SelText = vbTab
> >         i = i + 1
> >       Loop
> >     End If
> >   End If
> > End Sub

> > Howard Henry Schlunder



> > > OK - I'm using a Rich Text Box -
> > > If I use the tab key to indent a line, then I start typing - what I
> > want is,
> > > that, when I hit enter - giving the application a vbcrlf to go to
> the
> > next
> > > line - I need the starting point to match the tab position of the
> line
> > I
> > > just left.

> > > Does anyone know how this can be done?

> > > If this isn't clear - here's a more graphical type of

> > > First line
> > > vbtab - rtb.seltext="This is my text" --- here - when I hit the
> enter
> > key -
> > > I want the tab stop (for the start of the text) of the next line to
> > match
> > > the tab stop on the line above it.....

> > > Any ideas?



Fri, 27 Feb 2004 14:34:48 GMT  
 RTB - auto indent to match the tab stop of the line above
The units are relative.  Relative to the Scale mode of the Container that
contains the RTB control, ie - form, picture box, frame, etc.

You can have some fun with this by playing with the different types of scale
modes -
Twip
Point
Pixel
Character

to name a few.  The result is that sometimes you may not want to add 500
units depending on what scale mode you are using as some units are larger
than others.


Quote:
> Another possibility is to use the SelIndent property to control this. This
is rough --
> VERY ROUGH -- but it should give the concept I'm after

> Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
>   If KeyCode = vbKeyEscape Then
>     With RichTextBox1
>       .SelIndent = .SelIndent - 500
>     End With
>   End If
> End Sub

> Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
>   With RichTextBox1
>     If KeyAscii = 13 Then
>       KeyAscii = 0
>       currentindent = .SelIndent
>       .SelText = vbNewLine
>     ElseIf KeyAscii = vbKeyTab Then
>       KeyAscii = 0
>       .SelIndent = .SelIndent + 500
>     End If
>   End With
> End Sub

> Here, pressing the Tab key increases the amount of indenting, 500 units
(twips I think),
> for each press. Pressing the Escape key reduces the indenting by 500
units. Obviously,
> this needs work, but it doesn't have the small "flaw(?)" that your code
does -- the user
> can delete the indent in the middle of a paragraph thus breaking the
pattern. An
> established indent can't be changed by the user (unless you allow it).

> Rick



> > Oops, found a bug which would occured on the first line.  Use this
> > instead:

> > Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> > Dim i As Long
> >   If KeyAscii = 13 Then
> >     KeyAscii = 0
> >     RichTextBox1.SelText = vbCrLf
> >     If RichTextBox1.SelStart - 2 > 0 Then
> >       i = InStrRev(RichTextBox1.Text, vbCrLf, _
> >         RichTextBox1.SelStart - 2, vbBinaryCompare)
> >       If i = 0 Then i = -1
> >       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
> >         RichTextBox1.SelText = vbTab
> >         i = i + 1
> >       Loop
> >     End If
> >   End If
> > End Sub

> > Howard Henry Schlunder



> > > Using the KeyPress event, override the default carriage return handler
> > > and insert your own (or mine as the case may be):

> > > Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
> > > Dim i As Long
> > >   If KeyAscii = 13 Then
> > >     KeyAscii = 0
> > >     RichTextBox1.SelText = vbCrLf
> > >     If RichTextBox1.SelStart - 2 > 0 Then
> > >       i = InStrRev(RichTextBox1.Text, vbCrLf, _
> > >         RichTextBox1.SelStart - 2, vbBinaryCompare)
> > >       Do Until Mid$(RichTextBox1.Text, i + 2, 1) <> vbTab
> > >         RichTextBox1.SelText = vbTab
> > >         i = i + 1
> > >       Loop
> > >     End If
> > >   End If
> > > End Sub

> > > Howard Henry Schlunder



> > > > OK - I'm using a Rich Text Box -
> > > > If I use the tab key to indent a line, then I start typing - what I
> > > want is,
> > > > that, when I hit enter - giving the application a vbcrlf to go to
> > the
> > > next
> > > > line - I need the starting point to match the tab position of the
> > line
> > > I
> > > > just left.

> > > > Does anyone know how this can be done?

> > > > If this isn't clear - here's a more graphical type of

> > > > First line
> > > > vbtab - rtb.seltext="This is my text" --- here - when I hit the
> > enter
> > > key -
> > > > I want the tab stop (for the start of the text) of the next line to
> > > match
> > > > the tab stop on the line above it.....

> > > > Any ideas?



Fri, 05 Mar 2004 14:40:35 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Tip - Using TAB to indent several lines

2. resetting default first line indent, hanging indent

3. How do you Indent using RTB Control?

4. How do you Indent using the RTB Control?

5. tab bars do not match tab page

6. Disabling Auto indenting.

7. Auto Indent don't work, VB5 prof

8. Auto Indenting

9. Auto Indent - what I am missing?

10. Tab stop ranges not unique in tab panels!

11. indents and tabs?

12. Make Rich Text Box indent when tab is hit, not change control

 

 
Powered by phpBB® Forum Software