【问题标题】:VB specific line of text to bold in a multi-line TextBoxVB特定行文本在多行TextBox中加粗
【发布时间】:2014-02-16 07:47:44
【问题描述】:

我正在 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

更新

我已更改为富文本框并更新了我的代码示例以反映我的代码更改。我还没有变得大胆

【问题讨论】:

  • 你能把你的代码所在的完整方法贴出来吗?很难从代码 sn-p 中找出问题所在。
  • @wdosanjos 给你
  • Me.Font 是表单的字体,而不是 TextBox 控件的字体。此外,对于 TextBox 控件,没有加粗单行的选项(它是所有文本或什么都没有)。您需要为此使用 RichTextBox。
  • @wdosanjos 如果我不打扰您,请您看看我的更改。
  • 我有一个函数,只需传入字符串和您想要格式化的字符串即可。 . .

标签: vb.net fonts richtextbox


【解决方案1】:

这是函数...您传递要查找的文本和格式(粗体或斜体 - 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

【讨论】:

  • 它缺少有关如何从用户代码中使用此方法的信息。看来代码需要保存所有需要突出显示的行,然后遍历然后调用这个方法,对吧?
  • 这个方法怎么用?这是一个功能,他需要做的就是传递他想要的功能,该功能将为他完成。此函数遍历富文本框并查找出现,如果找到,则将出现添加到列表中。然后我们将浏览该列表并选择我们需要的内容。昨晚我用 5,000 行测试了这个,速度很快,而且它发现了你传入的任何内容。
  • 哦差点忘了,他想要格式化的字符串:readFile,是的,就是这样。如果您不记得如何使用函数,这里是如何使用函数的:FormatText(readFile,1) - 用于 BOLD 或 FormatText(readFile,2) - 用于斜体。
  • @MrCoDeXeR 抱歉耽搁了这么久,Life... wdosanjos 确实正确地假设我是初学者。但是我更喜欢自己尝试,所以我从 12 岁开始就断断续续地玩这个。今天一整天。我无法弄清楚我的代码中的 rText 等于什么。另外,感谢您提供的 FormatText 函数,我已经知道它是如何工作的了。
  • @MrCoDeXeR 你说得对,我确实知道如何使用函数,但是拥有 FormatText(readFile, 1) 让我省去了记忆的麻烦。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多