无法使用 TabStops 使 ListBox 中的项目正确对齐

Can#39;t make the items in a ListBox align correctly using TabStops(无法使用 TabStops 使 ListBox 中的项目正确对齐)
本文介绍了无法使用 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" +"			"+ "yyyyyyy");
        listBox1.Items.Add("xxxxxxx" + "			" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxx" + "			" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxxx" + "			" + "yyyyyyy");
        listBox1.Items.Add("xxxxxxxxxx" + "			" + "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?

推荐答案

需要设置属性CustomTabOffsets 和属性 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" + "	" + "yyyyyyy");
lb.Items.Add("xxxxxxx" + "	" + "yyyyyyy");
lb.Items.Add("xxxxxxxx" + "	" + "yyyyyyy");
lb.Items.Add("xxxxxxxxx" + "	" + "yyyyyyy");
lb.Items.Add("xxxxxxxxxx" + "	" + "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 中的项目正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)