【发布时间】:2016-02-18 18:23:14
【问题描述】:
我正在动态填充 ListView 以搜索长目录路径。
ListView 更新非常不稳定,垂直滚动条向上,我希望滚动条向下滚动,因为数据被添加到 ListView 中,我希望这会停止结果的闪烁。
您可以使用以下代码使 ListBox 做到这一点,但我无法使用 ListView 找到类似的东西。
lstBoxResults2.Items.Add(value);
lstBoxResults2.TopIndex = lstBoxResults2.Items.Count - 1;
lstBoxResults2.Update();
我尝试将排序属性设置为升序或降序,但这也不起作用,我得到一个奇怪的结果,即找到的路径未按行进顺序显示,即
Folder 1...
Folder 2...
Folder 2...
Folder 1...
etc.
代码如下。
listView1.View = View.Details;
listView1.GridLines = true;
listView1.Columns.Add("Length", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Path", 1800);
//Test length 150
//static int MAX_DIR_PATH = 150;
static int MAX_DIR_PATH = 260;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
btnStop.Enabled = true;
if (rBtnFolders.Checked == true)
{
try
{
this.Invoke(new Action(() => lblStatus.Text = "Scanning..."));
foreach (string dir in Directory.EnumerateDirectories(txtPath.Text, "*.*", SearchOption.AllDirectories))
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
//backgroundWorker1.ReportProgress(0);
return;
//break;
}
this.Invoke(new Action(() => listUpdate1(dir + Environment.NewLine)));
try
{
if (dir.Length >= MAX_DIR_PATH)
{
this.Invoke(new Action(() => listView1.Items.Add(dir.Length.ToString()).SubItems.Add(dir)));
this.Invoke(new Action(() => lblCount.Text = listView1.Items.Count.ToString())); }
}
catch (Exception err)
{
// This code just logs the message and continues to recurse.
log.Add(err.Message);
}
}
}
catch (Exception err)
{
// This code just logs the message and continues to recurse.
log.Add(err.Message);
}
}
顺便说一句,请随意批评代码,由于某种原因,上述代码不会递归搜索连接到我的 PC(驱动器 M:) 的 raid 框,但它会在连接的 USB 记忆棒上 (J:) 如果需要我会发布一个新问题。
【问题讨论】: