列表框未调整大小以适合最长的项目

时间:2017-02-19 21:19:04

标签: c# .net windows forms

我正在使用一个窗体表单列表框,它放在一个表格布局面板中,列设置为自动调整大小。

当列表框获取具有长名称的项目时,列表框不会重新显示项目的整个内容并将其替换为切割,例如如下图所示:

enter image description here

您是否知道如何让列表框自动调整大小以适应其内容中最长项目的大小?

3 个答案:

答案 0 :(得分:1)

你可以使用:

df['q1'] = df[['1997-01', '1997-02', '1997-03']].sum(axis=1)

df
Out[66]: 
          RegionName     State   1997-01   1997-02   1997-03        q1
0 New           York        NY       NaN       NaN       NaN       0.0
1 Los        Angeles        CA  155900.0  157000.0  157700.0  470600.0
2 Chicago         IL  110800.0  111300.0  111700.0       NaN  223000.0

答案 1 :(得分:0)

PreferredHeight将您的列表框高度设置为合并项目长度。

添加项SizeChanged时会调用,因为您将更改列表框大小。接下来会发生什么很简单: 1)我们使用MeasureString来衡量最后添加的项目listBox宽度,使用Arial字体和12p作为大小。如果结果大于listBox宽度,则宽度将更新。

listBox1.SizeChanged += (_sender, args) =>
{
    listBox1.Height = listBox1.PreferredHeight;
    var CurrentItemWidth = (int)this.CreateGraphics().MeasureString(listBox1.Items[listBox1.Items.Count - 1].ToString(), listBox1.Font, TextRenderer.MeasureText(listBox1.Items[listBox1.Items.Count - 1].ToString(), new Font("Arial", 12.0F))).Width;
    if(CurrentItemWidth > listBox1.Width)
        listBox1.Width = CurrentItemWidth;
};

listBox1.Items.Add("asd");
listBox1.Items.Add("abcdefghijklmnopqrstuvwxyz");

答案 2 :(得分:0)

以下是Zroq评论所需的捕获

enter image description here

enter image description here

enter image description here

相关问题