在列表框中更改选定的itms颜色

我想从列表框中更改所选项目的颜色控制如何在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等。