-1

追加しようとしているアイテムのテキストまたはそれに含まれるテキストに基づいて、リストボックスに各アイテムを描画する必要があります。次に、リストボックスの先頭にアイコンを配置する必要があります。指定した単語に応じて、他の 2 つの色とアイコンを配置します。

  • 項目にエラー テキストが含まれている場合は、エラー (16x16 ピクセル) アイコンを先頭に配置し、背景を薄い赤で、テキストを太い濃い赤で描画します。

  • 準備完了または開始テキストが含まれている場合は、明るいオレンジ色の背景と濃い太字の青色のテキストを使用します。

  • OK または成功のテキストが含まれている場合は、明るい緑色の背景と濃い太字の緑色のフォント テキストを使用します。

これどうやってするの?

編集

これは私がすでに持っているものですが、このコードは無限に更新されるようです。色を選択する必要があるのは、e.index の値です。e.index を e.stringvalue のような somthinf に変更できますか?

Private Sub lsbLog_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lsbLog.DrawItem
        '//Draw the background of the ListBox control for each item.
        '// Create a new Brush and initialize to a Black colored brush
        '// by default.
        e.DrawBackground()
        Dim mybrush = Brushes.Black

        '// Determine the color of the brush to draw each item based on 
        '//the index of the item to draw.
        Select Case e.Index
            Case 0
                mybrush = Brushes.Red
            Case 1
                mybrush = Brushes.Blue
            Case 2
                mybrush = Brushes.Green
        End Select

        '//
        '// Draw the current item text based on the current 
        '// Font and the custom brush settings.
        '//
        e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                              e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)
        '//
        '// If the ListBox has focus, draw a focus rectangle 
        '// around the selected item.
        '//
        e.DrawFocusRectangle()
        lsbLog.Refresh()
    End Sub
4

1 に答える 1

1

あなたの2つの特定の質問に答えて:

  1. Refresh最後に呼び出しを追加したため、表示したコードは際限なく更新されます。

    lsbLog.Refresh()
    

    それを取り除くと、無限のリフレッシュの問題が解決されます.

  2. はい、確かにアイテムのインデックスの代わりにキャプションをテストできますが、 のようなプロパティはありませんe.stringvalue。への呼び出しで既に発見して使用した別の方法で取得する必要がありますDrawString

    lsbLog.Items(e.Index).ToString()
    

    アイテムに一般的に含まれるものに応じて、私が持っているよりも少し複雑なことをしたいかもしれません. たとえば、等しいかどうかをテストするのではなく、文字列にキーワードが含まれているかどうかを確認したい場合があります。柔軟性を高めるために、ステートメントSelect CaseIf-に置き換える必要がある場合があります。Else


したがって、後でいくつかの小さな変更を加えて、次のコードになります。

Private Sub lsbLog_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles lsbLog.DrawItem
    '//Draw the background of the ListBox control for each item.
    '// Create a new Brush and initialize to a Black colored brush
    '// by default.
    e.DrawBackground()
    Dim mybrush As Brush = Brushes.Black

    '// Determine the color of the brush to draw each item based on 
    '//the index of the item to draw.
    Select Case lsbLog.Items(e.Index).ToString
        Case "Error"
            mybrush = Brushes.Red
        Case "Ready"
            mybrush = Brushes.Blue
        Case "Success"
            mybrush = Brushes.Green
    End Select

    '//
    '// Draw the current item text based on the current 
    '// Font and the custom brush settings.
    '//
    e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                          e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)
    '//
    '// If the ListBox has focus, draw a focus rectangle 
    '// around the selected item.
    '//
    e.DrawFocusRectangle()
End Sub

結果は次のようになります。

   所有者が描画した項目を含む ListBox


もちろん、要件を完全に満たすには、各アイテムの背景も入力する必要があります。これを行う最善の方法は、次のようなものを使用することですが、それに応じてブラシの色を変更します。

e.Graphics.FillRectangle(Brushes.Coral, e.Bounds)
于 2011-03-15T09:32:20.427 に答える