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

如何增大WinForm中ListBox1的行间距并实现水平缩进?

解决WinForm ListBox行间距增大与文本水平缩进的问题

要实现ListBox的行间距增大和文本水平缩进,咱们得用**自定义绘制(Owner-Draw)**的方式——因为WinForm默认的ListBox并没有直接提供这两个配置项。下面结合你现有的代码,一步步来实现:

步骤1:开启ListBox的自定义绘制模式

首先,我们需要把listBox1的OwnerDrawMode属性设置为OwnerDrawFixed(固定行高,适合咱们的需求)。你可以两种方式设置:

  • 设计器方式:选中ListBox,在属性面板里找到OwnerDrawMode,选择OwnerDrawFixed;
  • 代码方式:在Form1的构造函数里,InitializeComponent()之后添加:
    listBox1.OwnerDrawMode = OwnerDrawMode.OwnerDrawFixed;
    

步骤2:处理MeasureItem事件,设置行间距

这个事件用来定义每一行的高度,通过增大行高来实现行间距的效果。在Form1里添加以下方法:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    // 默认行高大概是13px,这里设置为25px,你可以根据需求调整数值
    e.ItemHeight = 25;
}

步骤3:处理DrawItem事件,实现文本缩进与自定义绘制

这个事件负责绘制每一项的内容,我们在这里添加文本缩进,同时保持选中项的系统默认样式:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    // 处理索引无效的情况
    if (e.Index < 0) return;

    // 获取当前项的Account对象
    var account = listBox1.Items[e.Index] as Account;
    if (account == null) return;

    // 绘制项的背景:选中项用系统高亮背景,否则用ListBox默认背景
    e.DrawBackground();

    // 设置文本颜色:选中项用高亮文本色,否则用ListBox默认前景色
    Color textColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected 
        ? SystemColors.HighlightText 
        : listBox1.ForeColor;

    // 实现文本水平缩进:这里用StringFormat设置15px的左缩进,也可以用"    "这种空格替代
    StringFormat textFormat = new StringFormat();
    textFormat.LeftIndent = 15; // 缩进像素值,可按需调整

    // 绘制文本
    e.Graphics.DrawString(account.Name, e.Font, new SolidBrush(textColor), e.Bounds, textFormat);

    // 绘制焦点框(保持系统默认的焦点样式)
    e.DrawFocusRectangle();
}

步骤4:绑定事件到ListBox

最后,我们需要把上面的两个事件绑定到listBox1上。在Form1的构造函数里,InitializeComponent()之后添加:

listBox1.MeasureItem += listBox1_MeasureItem;
listBox1.DrawItem += listBox1_DrawItem;

修改后的完整Form1代码示例

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 
        // 开启自定义绘制模式
        listBox1.OwnerDrawMode = OwnerDrawMode.OwnerDrawFixed;
        // 绑定事件
        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)
    {
        e.ItemHeight = 25; // 设置行高,增大间距
    }

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index < 0) return;

        var account = listBox1.Items[e.Index] as Account;
        if (account == null) return;

        e.DrawBackground();

        Color textColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected 
            ? SystemColors.HighlightText 
            : listBox1.ForeColor;

        StringFormat textFormat = new StringFormat();
        textFormat.LeftIndent = 15; // 文本左缩进

        e.Graphics.DrawString(account.Name, e.Font, new SolidBrush(textColor), e.Bounds, textFormat);
        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 (Properties.Settings.Default.Accounts != null) 
            Properties.Settings.Default.Accounts.RemoveAt(listBox1.SelectedIndex); 
        listBox1.Items.Remove(listBox1.SelectedItem); 
        Properties.Settings.Default.Save(); 
    } 
}

补充说明

  • 行高和缩进的数值都可以根据你的UI需求自行调整;
  • 如果需要更灵活的文本样式(比如不同字体、颜色),都可以在DrawItem事件里修改绘制逻辑;
  • 这种自定义绘制方式不会影响你原有ListBox的交互逻辑(比如选中、添加删除项的功能)。

内容的提问来源于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.29 07:04:46