2014-02-12 64 views
0

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

我已經改變爲格式文本框,並更新了我的代碼示例以反映我的代碼更改。我還沒有變得粗體

+0

您可以發佈代碼所在的完整方法嗎?很難弄清楚代碼片段出了什麼問題。 – wdosanjos

+0

@wdosanjos Here you go – redwdc

+0

Me.Font是表單的字體,不是TextBox控件的字體。此外,使用TextBox控件,沒有選項可粗體顯示單行(全是文字或不顯示)。你需要爲此使用RichTextBox。 – wdosanjos

回答

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 
+0

它缺少有關如何使用此方法從用戶的代碼中的信息。看起來代碼需要保存所有需要突出顯示的行,然後遍歷並調用這個方法,對吧? – wdosanjos

+0

如何使用這種方法?這是一個功能,他需要做的就是傳遞他想要的功能將爲他做的事情。這個函數通過richtextbox進行引用並查找出現的地方,如果找到,可以將出現地址添加到列表中。然後,我們將瀏覽該列表並選擇我們需要的內容。我昨晚測試了5000行,速度很快,加上它發現了你傳入的任何內容。 – Codexer

+0

哦,差點忘了,他想要的字符串格式化爲:readFile,是的就是這樣。這裏如何使用函數如果你不記得如何:FormatText(readFile,1) - 對於BOLD或FormatText(readFile,2) - 對於斜體。 – Codexer

相關問題