• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
The Heart In Binding
博客园    首页    新随笔    联系   管理     

在RichTextBox中设置首行缩进

 

   Private position(1024) As Integer '用来记录每段首行位置的数组

   Private currentposition As Integer = 0

Private rtb_Selectionlen As Integer = 0

Private rtb As RichTextBox

    '首行缩进

    Public Sub SetFirstLineIndent()

 

        '获取当前插入点的位置

        currentposition = rtb.GetFirstCharIndexOfCurrentLine

        rtb_Selectionlen = rtb.SelectionLength

        '找出全部换行符的位置并存入数组中

        Call FindFirstCharOfParagraph()

        '找到插入点所在段落的首字符位置

        FindPositonofFirstChar()

        '进行首行缩进

        InsertIndent(firstlineindentvalue)

    End Sub

 

    '找出全部段落首行首字符的位置并存入数组中

    Public Sub FindFirstCharOfParagraph()

        Dim indexofposition As Integer = 0

        Dim startposition As Integer = 0

        Dim indexofarray As Integer = 1 '数组的索引从1开始,因为0已经被缺省

        position(0) = 0 '将第一段定为0

        Do

            indexofposition = rtb.Find(Chr(13), startposition, RichTextBoxFinds.None)

            If indexofposition >= 0 Then

                position(indexofarray) = indexofposition + 1 '当前换行符的位置加1就等于下一段的首字符位置

                startposition = indexofposition + 1

                indexofarray += 1

            Else

                'MsgBox("未找到!")

            End If

        Loop While indexofposition >= 0

    End Sub

 

    '获取当前字符所在行的首字符位置

    Public Function FindPositionOfCurrentLine() As Integer

 

        currentposition = rtb.GetFirstCharIndexOfCurrentLine

        rtb_Selectionlen = rtb.SelectionLength

        Return currentposition

    End Function

 

    '找到插入点所在段落的首字符位置

    Public Function FindPositonofFirstChar() As Integer

        '方法是:首先找到当前插入点所在段落的换行符的位置,然后加1与数组position进行匹配

        '如果匹配则返回当前数组元素的前一个元素的值

        Try

            Dim indexofposition As Integer = 0

            Dim i_index As Integer = 0

            For Each i As Integer In position

                If currentposition >= position(i_index) And currentposition < position(i_index + 1) Then

                    Return position(i_index)

                ElseIf currentposition = position(i_index + 1) Then

                    Return position(i_index + 1)

                Else

                    i_index += 1

                End If

            Next

        Catch ex As Exception

            MsgBox(ex.Message)

            Exit Function

        End Try

    End Function

 

    '向剪贴板中写入缩进字符并粘贴到插入点

    Public Function InsertIndent(ByVal indentvalue As Integer) As Integer

        Try

            Dim indentstr As String = ""

            For i As Integer = 1 To indentvalue * 8

                indentstr &= Chr(32)

            Next

            rtb.SelectionStart = FindPositonofFirstChar()

            rtb.SelectionLength = 0

            Clipboard.Clear()

            Clipboard.SetText(indentstr, TextDataFormat.Text)

            rtb.Paste(DataFormats.GetFormat(DataFormats.Text))

        Catch ex As Exception

            MsgBox(ex.Message)

            Exit Function

        End Try

    End Function

 

posted @ 2009-03-29 08:58  Satyr  阅读(2510)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3