c#combobox设置无边框
时间: 2023-09-28 11:06:47 浏览: 56
您可以使用以下代码将 ComboBox 设置为无边框:
```csharp
comboBox1.FlatStyle = FlatStyle.Flat;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.BackColor = SystemColors.Control;
comboBox1.ForeColor = SystemColors.WindowText;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool isItemSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
if (e.Index >= 0)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds;
// 绘制背景色
g.FillRectangle((isItemSelected) ? Brushes.LightGray : Brushes.White, rect);
// 绘制文本
string itemText = comboBox1.Items[e.Index].ToString();
g.DrawString(itemText, e.Font, (isItemSelected) ? Brushes.White : Brushes.Black, rect.X, rect.Y + 2);
// 绘制焦点矩形
if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
ControlPaint.DrawFocusRectangle(g, rect);
}
}
```
这段代码会将 ComboBox 的外观设置为无边框,并且只能通过下拉列表选择项来选择。另外,还需要在窗体的 `Load` 事件中添加以下代码: