列表框中的格式问题
Format issue in Listbox
我想在我的 listbox
上显示这种格式:
"File name Size";
foreach (string filePath in filePaths)
{
BarraDeProgresso.Visible = true;
PegarMeuFicheiro = filePath;
// here is where im tryng to do it. but isn´tworking as i expected.
lbMostrarItems.Items.Add(String.Format("{0, 5} {1, 30}",PegarMeuFicheiro.Substring(9 + nome.Length + 20, PegarMeuFicheiro.Length - (9 + nome.Length + 20)),"Size"));
//lbMostrarItems.SelectedIndex = lbMostrarItems.Items.Count - 1;
}
我该怎么做才能使它全部靠右对齐?
改用 ListView
控件,它允许列和许多其他功能。
首先添加控件,然后 select 它并转到控件的 属性 并将 View 更改为 Details。这将允许您看到一个包含可调整大小的列名称的列表。
接下来,创建两列(一列用于文件名,另一列用于文件大小)或根据您的情况创建任何列。为此,在属性 window 上转到“列”并单击它以获取允许您添加列的对话框 window。
最后,这里有一些关于如何使用 ListView
的示例代码。
private void Form1_Load(object sender, EventArgs e)
{
var fileListForExample = Directory.GetFiles(@"C:\");
foreach (var item in fileListForExample)
{
FileInfo fileInfo = new FileInfo(item);
var lstItem = new ListViewItem(Path.GetFileName(item));
lstItem.SubItems.Add(fileInfo.Length.ToString());
var itemAdded = listView1.Items.Add(lstItem);
}
}
您可以在列表框中手动绘制项目。
示例:
public Form1()
{
//InitializeComponent();
this.Width = 500;
var listBox = new ListBox { Parent = this, Width = 400, Height = 250 };
listBox.DrawMode = DrawMode.OwnerDrawFixed;
var files = new DirectoryInfo(".").GetFiles();
listBox.DataSource = files;
listBox.DrawItem += (o, e) =>
{
e.Graphics.DrawString(files[e.Index].Name, listBox.Font, Brushes.Black, e.Bounds);
var length = files[e.Index].Length.ToString();
var size = e.Graphics.MeasureString(length, listBox.Font);
e.Graphics.DrawString(length, listBox.Font,
Brushes.Black, e.Bounds.Width - size.Width, e.Bounds.Y);
};
}
我想在我的 listbox
上显示这种格式:
"File name Size";
foreach (string filePath in filePaths)
{
BarraDeProgresso.Visible = true;
PegarMeuFicheiro = filePath;
// here is where im tryng to do it. but isn´tworking as i expected.
lbMostrarItems.Items.Add(String.Format("{0, 5} {1, 30}",PegarMeuFicheiro.Substring(9 + nome.Length + 20, PegarMeuFicheiro.Length - (9 + nome.Length + 20)),"Size"));
//lbMostrarItems.SelectedIndex = lbMostrarItems.Items.Count - 1;
}
我该怎么做才能使它全部靠右对齐?
改用 ListView
控件,它允许列和许多其他功能。
首先添加控件,然后 select 它并转到控件的 属性 并将 View 更改为 Details。这将允许您看到一个包含可调整大小的列名称的列表。
接下来,创建两列(一列用于文件名,另一列用于文件大小)或根据您的情况创建任何列。为此,在属性 window 上转到“列”并单击它以获取允许您添加列的对话框 window。
最后,这里有一些关于如何使用 ListView
的示例代码。
private void Form1_Load(object sender, EventArgs e)
{
var fileListForExample = Directory.GetFiles(@"C:\");
foreach (var item in fileListForExample)
{
FileInfo fileInfo = new FileInfo(item);
var lstItem = new ListViewItem(Path.GetFileName(item));
lstItem.SubItems.Add(fileInfo.Length.ToString());
var itemAdded = listView1.Items.Add(lstItem);
}
}
您可以在列表框中手动绘制项目。
示例:
public Form1()
{
//InitializeComponent();
this.Width = 500;
var listBox = new ListBox { Parent = this, Width = 400, Height = 250 };
listBox.DrawMode = DrawMode.OwnerDrawFixed;
var files = new DirectoryInfo(".").GetFiles();
listBox.DataSource = files;
listBox.DrawItem += (o, e) =>
{
e.Graphics.DrawString(files[e.Index].Name, listBox.Font, Brushes.Black, e.Bounds);
var length = files[e.Index].Length.ToString();
var size = e.Graphics.MeasureString(length, listBox.Font);
e.Graphics.DrawString(length, listBox.Font,
Brushes.Black, e.Bounds.Width - size.Width, e.Bounds.Y);
};
}