Can#39;t make the items in a ListBox align correctly using TabStops(无法使用 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 中的项目正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 TabStops 使 ListBox 中的项目正确对齐
基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
