2012-02-20 66 views
0

我有一個程序,它在安裝軟件時寫入.log文件。 某些行將包含WARNING或FAILED。在VB.net中突出顯示的實時文本閱讀器

我想要的是我的程序中的一個窗口,該窗口將讀取.log文件並將其內容顯示到該窗口中。任何包含WARNING或FAILED的行都會突出顯示爲黃色/紅色。

有誰知道如何做到這一點?

回答

1

創建一個FORM(我使用VB 2010)並添加此代碼。 它將以2種顏色在窗體上書寫3行。 它可能會讓你在路上。詢問MSDN對每個新功能的幫助。

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 
    Dim chRs(0) As CharacterRange 
    Dim sbRs(0) As SolidBrush 
    Dim flRs(0) As SolidBrush 
    Dim blueBrush As New SolidBrush(Color.Blue) 
    Dim whiteBrush As New SolidBrush(Color.White) 
    Dim redBrush As New SolidBrush(Color.Red) 
    Dim EditFont As New Font("Courier New", 9) 
    Dim stringFormat As New StringFormat 
    Dim aRectangle As Rectangle 
    Dim RectHeight As Integer = 20 
    For i = 1 To 3 
     Dim txt As String = "a string " & CStr(i) 
     If i = 2 Then 
      sbRs(0) = blueBrush 
     Else 
      sbRs(0) = redBrush 
     End If 
     flRs(0) = whiteBrush 
     chRs(0) = New CharacterRange(0, txt.Length()) 
     Dim chRsa As Array = Array.CreateInstance(GetType(CharacterRange), 1) 
     Array.Copy(chRs, 0, chRsa, 0, 1) 
     aRectangle = New Rectangle(0, CInt((i - 1) * RectHeight), ClientRectangle.Size.Width, RectHeight) ' x, y, w, h 
     stringFormat.SetMeasurableCharacterRanges(chRsa) 
     Dim stringRegions As Array = Array.CreateInstance(GetType([Region]), 1) 
     stringRegions = e.Graphics.MeasureCharacterRanges(txt, EditFont, aRectangle, stringFormat) 
     Dim measureRect1 As RectangleF = stringRegions(0).GetBounds(e.Graphics) 
     Dim g As Graphics = e.Graphics 
     g.FillRectangle(flRs(0), measureRect1) 
     g.DrawString(txt.Substring(chRs(0).First, chRs(0).Length), EditFont, sbRs(0), CSng(measureRect1.X), CSng(measureRect1.Y)) 
    Next 
End Sub 
+0

你沒有處理你的對象。 – LarsTech 2012-02-21 15:36:06

+0

應拋棄哪個對象?我能想到的唯一一個將是Graphics對象,但我不創建它,只是使用它! – Martin 2012-02-22 19:37:17

+0

基本上,任何實現* IDispose *:刷子和字體,甚至是StringFormat。一個'使用(...){}'塊只能用於一次性物體,所以它會在塊的末尾爲你處理,並且是確保你的物體自己清理完的好方法。在你的例子中,你不會處理Graphic對象,因爲你是對的,你沒有創建它。 – LarsTech 2012-02-22 19:44:35