使用TabStops无法使ListBox中的项目正确对齐 [英] Can't make the items in a ListBox align correctly using TabStops

查看:40
本文介绍了使用TabStops无法使ListBox中的项目正确对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListbBox出现以下问题.

I have the following problem with ListbBox.

为了易于理解,我对其进行了简化.

To make it easy to understand I have simplified it a bit.

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("xxxxxx" +"\t\t\t"+ "yyyyyyy");
        listBox1.Items.Add("xxxxxxx" + "\t\t\t" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxx" + "\t\t\t" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxxx" + "\t\t\t" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxxxx" + "\t\t\t" + "yyyyyyy");
    }
}

从第1行到第4行向下完美地连续打印.但是,尽管有足够的空间,但是当我运行该程序时,第5行已完全关闭.有人可以帮助我使所有物品向下成一直排吗?

From row 1 to 4 it prints out perfectly in straight rows downwards. But the 5th row is completly off when I run the program, although there is plenty of space. Can any body help me to get all items to be in straight rows downwards?

推荐答案

您需要设置属性 UseCustomTabOffsets ,然后可以将字符串中的选项卡数量减少到一个.

You need to set the property CustomTabOffsets and the property UseCustomTabOffsets and then you can reduce the number of tabs in your strings to only one.

例如

ListBox lb = new ListBox();
lb.Size = new Size(500, 200);
lb.CustomTabOffsets.Add(100);
lb.UseCustomTabOffsets = true;

lb.Items.Add("xxxxxx" + "\t" + "yyyyyyy");
lb.Items.Add("xxxxxxx" + "\t" + "yyyyyyy");
lb.Items.Add("xxxxxxxx" + "\t" + "yyyyyyy");
lb.Items.Add("xxxxxxxxx" + "\t" + "yyyyyyy");
lb.Items.Add("xxxxxxxxxx" + "\t" + "yyyyyyy");

Form f = new Form();
f.Controls.Add(lb);
f.Show();

当然,您应该将100更改为与字符串第一部分的实际最大长度和ListBox的宽度更一致的值

Of course you should change that 100 to something more consistent with the actual maximum length of the first part of your strings and the width of your ListBox

这篇关于使用TabStops无法使ListBox中的项目正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆