我想更改列表框的选定项目forecolor [英] I want to change selected item forecolor of listbox
问题描述
Hello Guys! i am facing a little issue in my project how can i change selected listbox items forecolor i can select all items of listbox but i dont know how to change forecolor of selected items
This coding am usinh in my project for select listbox items
for (int i = 0; i < lbProductsToBuy.Items.Count; i++)
{
lbProductsToBuy.SetSelected(i,true);
}
printreceiptToken1();
dataGridView67.Rows.Clear();
}
我尝试了什么:
i不知道怎么做所以我不尝试任何事情请帮助我
What I have tried:
i dont know how to do it so i dont try any thing please help me
推荐答案
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index<0) return;
//if the item state is selected them change the back color
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor,
Color.Yellow);//Choose the color
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Draw the current item text
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
使用逻辑以上。
use above logic.
如果没有,你就不能这样做将 DrawMode
属性更改为
You can't do that without changing the DrawMode
Property to
DrawMode.OwnerDrawFixed<br />
但这将删除一些功能,然后您必须添加所有项目都由您自己编程。
这里我用Google搜索了一个C#样本;
在C#中创建一个自己绘制的ListBox - C#HelperC#Helper
我有在VB.NET中制作了这样一个ComboBox控件,具有许多扩展功能,如: BorderColor,DisplayText,ItemsForeColor,ItemsBackColor,ItemsFont等..
如果你想要它我可以发送到你的电子邮件,它是在VB.NET。
如果有帮助,请接受这个答案。
but that will remove some functionality, you then have to add all items programatically by your own.
Here i googled a C# sample;
Make an owner-drawn ListBox in C# - C# HelperC# Helper
I have made such a ComboBox control in VB.NET with many extended features like; BorderColor, DisplayText, ItemsForeColor, ItemsBackColor, ItemsFont etc..
If you want it i can send it to your Email, it's in VB.NET.
Please accept this answer if it helped you.
参考这个
如何更改列表框所选项目forecolor / backcolor C# [ ^ ]
这篇关于我想更改列表框的选定项目forecolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!