【发布时间】: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