我在vb.net中編寫了一個提醒程序,該程序讀取一個文本文件並顯示日期條目從今天到今天的14天。我希望今天的日期以粗體顯示。如果讓我用一個文本框,而不是RTF文本框有什麼區別這是我已經試過:在多行文本框中以粗體顯示VB的特定行文本
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年2月11日:測試字符串
2014年2月12日:測試字符串2
UPDATE
我已經改變爲格式文本框,並更新了我的代碼示例以反映我的代碼更改。我還沒有變得粗體
您可以發佈代碼所在的完整方法嗎?很難弄清楚代碼片段出了什麼問題。 – wdosanjos
@wdosanjos Here you go – redwdc
Me.Font是表單的字體,不是TextBox控件的字體。此外,使用TextBox控件,沒有選項可粗體顯示單行(全是文字或不顯示)。你需要爲此使用RichTextBox。 – wdosanjos