C# 在Windows窗体应用程序中,在不更改背景颜色的情况下创建下拉列表
如图所示,要从设计器中创建下拉列表,我选择DropDownStyle作为下拉列表。这样做,它将成为一个列表,但其背景颜色已更改。我还将BackColor属性更改为window,但颜色仍与列表相同。现在,我要更改下拉列表的背景颜色,就像创建列表之前一样C# 在Windows窗体应用程序中,在不更改背景颜色的情况下创建下拉列表,c#,winforms,C#,Winforms,如图所示,要从设计器中创建下拉列表,我选择DropDownStyle作为下拉列表。这样做,它将成为一个列表,但其背景颜色已更改。我还将BackColor属性更改为window,但颜色仍与列表相同。现在,我要更改下拉列表的背景颜色,就像创建列表之前一样 将FlatStyle属性更改为“展开”或“弹出”。在这里,您可以更改组合框的背景色。但是,组合框必须失去焦点,您才能看到颜色,因为当选择它时,它是蓝色的,在聚焦时会随Windows当前主题的变化而变化,这表明您已选择所选的项目 您可以通过更改Dra
将FlatStyle属性更改为“展开”或“弹出”。在这里,您可以更改组合框的背景色。但是,组合框必须失去焦点,您才能看到颜色,因为当选择它时,它是蓝色的,在聚焦时会随Windows当前主题的变化而变化,这表明您已选择所选的项目
您可以通过更改DrawMode属性来完成此操作。将DrawMode属性更改为DrawMode.OwnerDrawFixed后,添加DrawItem事件并在其中更改背景色 代码: 输出:
有关详细信息,请参阅。运行它,您将能够看到所需的结果可能重复的
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}
private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
try
{
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
catch (Exception ex)
{
}
}