实时文本阅读器,在VB.net中突出显示

时间:2012-02-20 17:07:08

标签: vb.net

我有一个程序在安装软件时写入.log文件。 有些行包含WARNING或FAILED。

我想要的是,我的程序中的一个窗口将读取.log文件,并在写入窗口时显示内容。任何包含WARNING或FAILED的行都会突出显示为黄色/红色。

有谁知道怎么做?

1 个答案:

答案 0 :(得分: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