VB特定的文本行在多行TextBox中加粗

时间:2014-02-12 00:05:43

标签: vb.net fonts richtextbox

我正在vb.net中编写一个提醒程序,该程序读取文本文件并显示从今天到今天14天的日期条目。我希望今天的日期条目以粗体显示。如果它有任何区别我正在使用TextBox而不是Rich TextBox这就是我尝试过的:

Public Sub getReadFile()

    ' rtfRead is the name of the TextBox
rtfRead.Text = Nothing
' Today, the first read line date
Dim startDate As Date = Date.Now()                                          
    ' First dated line to read
Dim todayDate As String = (GetDateInMyFormat(startDate))                    
    ' The last read line date
Dim endDate As String = (GetDateInMyFormat(DateAdd("d", 14, startDate)))
    ' The first 4 characters of a line. Are the 4 charcters numbers, i.e. yyyy
Dim lineStart As Object
    ' The date at the beginnig of a an entry
Dim lineDate As String = Nothing
    ' Are the first 4 charcters of a line numeric = True
Dim isNum As Boolean = False
    ' TM_Notes.txt
Dim readFile As String = Nothing    
Dim oldFont As Font = rtfRead.Font
Dim boldFont As Font = New Font(rtfRead.Font, FontStyle.Bold)

Try
    ' Create an instance of StreamReader to read from a file.
    ' The using statement also closes the StreamReader.
    Using sr As New StreamReader("TM_Notes.txt")
        Dim lineRead As String

        ' Read and display lines from the file until the end of
        ' the file is reached.
        Do
            lineRead = sr.ReadLine()

            lineStart = Mid(lineRead, 1, 4)
            isNum = IsNumeric(lineStart)

            If isNum = True Then
                lineDate = GetDateInMyFormat(Mid(lineRead, 1, 10))
            End If

            If lineDate = todayDate Then
                rtfRead.Font = boldFont
            Else
                rtfRead.Font = oldFont
            End If

            If Not (lineRead Is Nothing) And isNum = False And lineDate <= endDate Then
                readFile = readFile + lineRead & vbCrLf
            ElseIf lineDate >= todayDate And lineDate <= endDate Then
                readFile = readFile + lineRead & vbCrLf
            End If

        Loop Until lineRead Is Nothing
    End Using

    rtfRead.Text = readFile

Catch ex As Exception
    ' Let the user know what went wrong.
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)
End Try
End Sub

我没有收到任何错误,但今天的日期不是粗体。

今天是02-11,那么测试字符串行应该是粗体。

2014-02-11:测试字符串

2014-02-12:测试字符串2

更新

我已更改为Rich TextBox并更新了我的代码示例以反映我的代码更改。我还没有得到大胆的

1 个答案:

答案 0 :(得分:0)

这是函数...您传递要查找的文本和格式(粗体或斜体 - 1或2)。这非常好用;如果您愿意,也可以将其展开以添加颜色。我希望你能找到这个有用,快乐的编码!

  'Pass in a 1 or 2; one is bold the other is italic'
Private Function FormatText(ByVal TextToFormat As String, ByVal TextFormat As Integer)
    Dim count As New List(Of Integer)()
    For i As Integer = 0 To rText.Text.Length - 1
        If rText.Text.IndexOf(TextToFormat, i) <> -1 Then
            'If the word is found add the index to the list
            count.Add(rText.Text.IndexOf(TextToFormat, i))
        End If
    Next

    Try
        For i As Integer = 0 To count.Count - 1
            rText.[Select](count(i), TextToFormat.Length)
            Select Case TextFormat
                Case 1
                    rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Bold)
                Case 2
                    rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Italic)
            End Select
            count.RemoveAt(i)
        Next
    Catch
    End Try
    Return Nothing
End Function