【发布时间】:2009-08-07 09:39:17
【问题描述】:
我想从列表框控件中更改所选项目的颜色如何做到这一点 在windows(Winforms)中
【问题讨论】:
我想从列表框控件中更改所选项目的颜色如何做到这一点 在windows(Winforms)中
【问题讨论】:
据我所知,如果您想这样做,您需要创建 ListBox.DrawMode OwnerDrawFixed 并向 DrawItem 方法添加一个事件处理程序。
这样的事情可能会做你想做的事:
private void lstDrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = (ListBox)sender;
e.DrawBackground();
e.DrawFocusRectangle();
DrawItemState st = DrawItemState.Selected ^ DrawItemState.Focus;
Color col = ((e.State & st) == st) ? Color.Yellow : lst.BackColor;
e.Graphics.DrawRectangle(new Pen(col), e.Bounds);
e.Graphics.FillRectangle(new SolidBrush(col), e.Bounds);
if (e.Index >= 0)
{
e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(lst.ForeColor), e.Bounds, StringFormat.GenericDefault);
}
}
希望对你有帮助 詹姆斯
【讨论】:
假设您正在使用 WinForms:
大多数控件都有 BackColor 和 BorderColor 属性。您可以将Color 对象添加到列表框(颜色名称应显示为Color.ToString() 返回名称),然后使用listbox.SelectedItems[0] 获取颜色并更新其他控件的BackColor 等。
【讨论】: