You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整WinForms中ListBox的行间距、缩进及选中项颜色?

解决WinForms ListBox的行间距、文本缩进和选中项红色需求

嘿,我来帮你搞定这三个ListBox的自定义样式需求!标准WinForms ListBox默认不支持这些效果,咱们得通过自定义绘制来实现,下面是具体步骤和整合后的代码:

第一步:开启自定义绘制模式

首先要把ListBox的DrawMode设置为OwnerDrawVariable(支持自定义行高),同时绑定两个核心事件:MeasureItem(控制行高/行间距)和DrawItem(自定义绘制文本与样式)。

第二步:整合后的完整Form1代码

我把你的原有代码和自定义绘制逻辑整合好了,直接替换原Form1代码即可:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // 初始化自定义绘制配置
        listBox1.DrawMode = DrawMode.OwnerDrawVariable;
        listBox1.MeasureItem += listBox1_MeasureItem;
        listBox1.DrawItem += listBox1_DrawItem;

        if (Properties.Settings.Default.Accounts == null)
            Properties.Settings.Default.Accounts = new List<Account>();

        if (Properties.Settings.Default.Accounts != null)
        {
            foreach (var account in Properties.Settings.Default.Accounts)
            {
                listBox1.Items.Add(account);
            }
        }
    }

    // 设置行高,实现行间距
    private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        // 这里设置行高为25像素,数值越大行间距越大,可按需调整
        e.ItemHeight = 25;
    }

    // 自定义绘制项:实现文本缩进+选中项红色
    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        // 先绘制系统默认背景(保留选中/未选中的背景样式)
        e.DrawBackground();

        // 判断当前项是否被选中
        bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;

        // 设置文本颜色:选中时为红色,未选中用ListBox默认颜色
        Color textColor = isSelected ? Color.Red : listBox1.ForeColor;

        // 避免索引越界
        if (e.Index >= 0 && e.Index < listBox1.Items.Count)
        {
            Account currentAccount = listBox1.Items[e.Index] as Account;
            if (currentAccount != null)
            {
                // 绘制文本:X+20实现水平缩进,Y+5让文本垂直居中
                TextRenderer.DrawText(
                    e.Graphics,
                    currentAccount.Name,
                    listBox1.Font,
                    new Point(e.Bounds.X + 20, e.Bounds.Y + 5),
                    textColor,
                    isSelected ? e.BackColor : listBox1.BackColor
                );
            }
        }

        // 绘制焦点框(可选,保留系统默认焦点样式)
        e.DrawFocusRectangle();
    }

    // 原有按钮点击事件(保持不变)
    private void button1_Click(object sender, EventArgs e)
    {
        new Form2(listBox1).Show();
    }

    // 原有选中项变更事件(保持不变)
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem is Account selectedAccount)
        {
            textBox1.Text = selectedAccount.Name;
            textBox2.Text = selectedAccount.Login;
            textBox3.Text = selectedAccount.Password;
        }
    }

    // 原有删除按钮事件(保持不变)
    private void button2_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex != -1 && Properties.Settings.Default.Accounts != null)
        {
            Properties.Settings.Default.Accounts.RemoveAt(listBox1.SelectedIndex);
            listBox1.Items.Remove(listBox1.SelectedItem);
            Properties.Settings.Default.Save();
        }
    }

    // 移除原错误代码:标准ListBox没有ItemFormatStyle属性
    private void Form1_Load(object sender, EventArgs e)
    {
        // 此处无需原有错误代码
    }
}

关键逻辑说明

  1. 行间距控制:通过MeasureItem事件设置e.ItemHeight,数值越大,行与行之间的空隙越宽,可根据视觉需求调整(比如改成30或20)。
  2. 水平缩进实现:绘制文本时将X坐标偏移20像素(e.Bounds.X + 20),修改这个数值可以调整缩进距离。
  3. 选中项红色效果:通过判断DrawItemState.Selected状态,将选中项的文本颜色设为红色,未选中项保持默认颜色。

注意事项

  • 请删除你原Form1_Load中的listBox1.ItemFormatStyle.Margin代码,标准WinForms ListBox没有这个属性,会导致编译错误。
  • 如果需要调整文本垂直居中效果,可以修改e.Bounds.Y + 5中的5,根据行高适配即可。

内容的提问来源于stack exchange,提问作者Klare Fare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:01:34