C# 单击项目时,如何删除listBox1中所选项目的蓝色包装?
当我单击listBox1选择一个项目时,该项目将保持蓝色。我怎样才能去掉这种颜色C# 单击项目时,如何删除listBox1中所选项目的蓝色包装?,c#,winforms,C#,Winforms,当我单击listBox1选择一个项目时,该项目将保持蓝色。我怎样才能去掉这种颜色 对于ListBox,您可以编写如下代码: private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { string s = string.Empty; if (listBox1.SelectedIndex != -1) s = listBox1.SelectedItem.ToString();
对于ListBox,您可以编写如下代码:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = string.Empty;
if (listBox1.SelectedIndex != -1)
s = listBox1.SelectedItem.ToString();
/// continue you code here ....
///
/// after that remove the hilight
listBox1.SelectedIndex = -1;
}
使用此代码将选择颜色更改为所需的任何颜色:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Add this to your form initialization
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
}
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.Transparent);//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();
}
}
或
干杯 是否要更改所选项目的颜色?或者你想取消选择它?
listBox1.ClearSelected();
listBox1.SelectedIndex = -1;