1

有谁知道为什么 Listbox1.Refresh() 命令可能不会每次都触发 ListBox1_DrawItem 子?

在 Microsoft Visual Basic 2010 中,列表框具有 forcolor 和 backcolor 属性。这些属性更改列表框中所有项目的颜色和背景颜色。默认情况下,列表框上单个项目的前景色和背景色没有属性,我知道在列表视图中有,但我仍然希望使用列表框。我正在尝试能够更改列表框中各个项目的前景色和背景色属性。要做到这一点,列表框的绘制项 sub 必须与列表框的 drawmode 属性设置为 OwnerDrawFixed 一起使用。然后使用画笔颜色和电子图形,可以更改前景色或背景色。我已经看到并遵循了如何为当前选定的项目执行此操作的示例。比如ehow网站上的那个. 然而,我厌倦的是改变 litsbox 项目的颜色,因为它是根据变量添加的。

这是我的代码:

    Private Sub listbox_add()

        Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
        add_item_colour = True
        ListBox1.Refresh()

End Sub



    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    Dim myBrush As Brush = Brushes.Black

    e.DrawBackground()
    If add_item_colour = True Then
        If blue_message = True Then
            myBrush = Brushes.Blue
        Else
            myBrush = Brushes.Black
        End If
        e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
        New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        add_item_colour = False
    End If
    e.DrawFocusRectangle()
End Sub

listbox_text 是一个字符串数组,用于存储要添加的字符串,list_num 是一个整数,当将新项目添加到列表框时递增,而 blue_message 是一个布尔值,当我想要蓝色消息时它为 true,当我不想要蓝色消息时为 false .

我似乎遇到的问题是 Listbox1.Refresh() 命令似乎没有在每次调用时触发 ListBox1_DrawItem 子。我通过使用刹车点发现了这一点。有谁知道为什么会出现这种情况以及我该如何解决?

谢谢,对此的任何帮助将不胜感激。

4

1 回答 1

-1

首先我建议你使用后台工作者而不是直接在 UI 线程上写下你的代码。

请参考以下代码:

public partial class Form1 : Form
{
    Brush myBrush = Brushes.Blue;
    public Form1()
    {
        InitializeComponent();
        this.backgroundWorker1.DoWork += backgroundWorker1_DoWork;
        this.backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
        this.backgroundWorker1.RunWorkerAsync(this);
    }

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, myBrush, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
        e.DrawFocusRectangle();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync(button1);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync(button2);
    }

    void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.listBox1.Refresh();
    }

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        if (e.Argument == this)
        {
            listBox1.Items.Add("p1");
            listBox1.Items.Add("p2");
        }
        else if (e.Argument == this.button1)
        {
            myBrush = Brushes.Red;
            listBox1.Refresh();
        }
        else if (e.Argument == this.button2)
        {
            myBrush = Brushes.Green;

            if (listBox1.SelectedItem == null)
                return;

            var test = listBox1.Items[listBox1.SelectedIndex];
            listBox1.SelectedItem = test;
            var g = listBox1.CreateGraphics();
            var rect = listBox1.GetItemRectangle(listBox1.SelectedIndex);
            listBox1_DrawItem(listBox1, new DrawItemEventArgs(g, this.Font, rect, listBox1.SelectedIndex, DrawItemState.Default));
            listBox1.Refresh();
        }
    }
}
于 2013-07-29T12:01:20.877 回答