本文介绍了如何更改列表框所选项目的字体颜色C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改列表框所选项目的字体颜色C#按钮点击



我尝试过:



for(int i = 0; i< lbProductsToBuy.Items.Count; i ++)

{

lbProductsToBuy.SetSelected(i ,true);

i.fontcolor = color.Red;

}

但它不起作用

how to change font color of listbox selected items C# on button click

What I have tried:

for (int i = 0; i < lbProductsToBuy.Items.Count; i++)
{
lbProductsToBuy.SetSelected(i, true);
i.fontcolor = color.Red;
}
but it's not working

推荐答案

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)

   MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
    if (item != null)
    {
        e.Graphics.DrawString( // Draw the appropriate text in the ListBox
            item.Message, // The message linked to the item
            listBox1.Font, // Take the font from the listbox
            new SolidBrush(item.ItemColor), // Set the color 
            0, // X pixel coordinate
            e.Index * listBox1.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
        );
    }


这篇关于如何更改列表框所选项目的字体颜色C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:25