通过ms访问表数据到Richtextbox vb.net时格式不正确

问题描述 投票:0回答:1

我有一个用于存储段落位置,段落类型和段落文本的表

enter image description here此信息将发送到Richtextbox。

到目前为止很好

基于存储在表中的段落类型,段落文本被格式化。

这里开始变得令人困惑

enter image description here

如果重复段落文本,我的代码将更改格式。它将对每个段落类型执行此操作。我不明白为什么会这样。

这里有什么优惠!?

谢谢您的时间!

        For i = 1 To ds.Tables("FoundSelection").Rows.Count - 1

        rtbScriptRTF.SelectionStart = rtbScriptRTF.Text.IndexOf(rtbScriptRTF.Lines(i))
        rtbScriptRTF.SelectionLength = rtbScriptRTF.Lines(i).Length
        'MsgBox(rtbScriptRTF.SelectedText.ToString)

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 1 Then 'Action!!
            rtbScriptRTF.SelectionColor = Color.Black

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 2 Then 'Dialogue!!
            rtbScriptRTF.SelectionColor = Color.DarkOliveGreen
            rtbScriptRTF.SelectionIndent = (rtbScriptRTF.Width / 3)
            rtbScriptRTF.SelectionRightIndent = 25

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 3 Then 'Cue!!
            rtbScriptRTF.SelectionColor = Color.Navy
            rtbScriptRTF.SelectionIndent = (rtbScriptRTF.Width / 2)

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 4 Then 'Parenthesis!!
            rtbScriptRTF.SelectionColor = Color.DarkOliveGreen

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 5 Then 'Transition IN!!
            rtbScriptRTF.SelectionColor = Color.Black
        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 6 Then 'Transition OUT!!
            rtbScriptRTF.SelectionColor = Color.Black
            rtbScriptRTF.SelectionAlignment = HorizontalAlignment.Right

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 7 Then 'SLUGLINE!!
            rtbScriptRTF.SelectionColor = Color.DarkRed

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 8 Then 'ACT!!
            rtbScriptRTF.SelectionBackColor = Color.LightGray
            rtbScriptRTF.SelectionColor = Color.Black

        End If
    Next
vb.net loops formatting richtextbox
1个回答
0
投票

这是因为您正在使用IndexOf。那将找到指定文本的第一个实例。尽管您无需查找任何内容的索引。在添加新文本之前和之后,只需获取TextLength,这就是您的起点和终点。

Dim table = ds.Tables("FoundSelection")

For Each row As DataRow In table.Rows
    Dim selectionStart = rtbScriptRTF.TextLength
    Dim selectionColor = rtbScriptRTF.ForeColor
    Dim selectionBackColor = rtbScriptRTF.BackColor
    Dim selectionIndent = 0
    Dim selectionRightIndent = 0
    Dim selectionAlignment = HorizontalAlignment.Left

    Select Case row.Field(Of Integer)("Paragraphtype")
        Case 1, 5
            selectionColor = Color.Black
        Case 2
            selectionColor = Color.DarkOliveGreen
            selectionIndent = rtbScriptRTF.Width \ 3
            selectionRightIndent = 25
        Case 3
            selectionColor = Color.Navy
            selectionIndent = rtbScriptRTF.Width \ 2
        Case 4
            selectionColor = Color.DarkOliveGreen
        Case 6
            selectionColor = Color.Black
            selectionAlignment = HorizontalAlignment.Right
        Case 7
            selectionColor = Color.DarkRed
        Case 8
            selectionColor = Color.Black
            selectionBackColor = Color.LightGray
    End Select

    With rtbScriptRTF
        .AppendText(row.Field(Of String)("Paragraph"))

        Dim selectionLength = .TextLength - selectionStart

        .SelectionStart = selectionStart
        .SelectionLength = selectionLength
        .SelectionColor = selectionColor
        .SelectionBackColor = selectionBackColor
        .SelectionIndent = selectionIndent
        .SelectionRightIndent = selectionRightIndent
        .SelectionAlignment = selectionAlignment

        .AppendText(Environment.NewLine)
    End With
Next

该代码将添加一个段落并对其进行格式设置,添加下一个段落并对其进行格式设置,依此类推,为每个段落执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.