【问题标题】:ListView reverse vertical scroll directionListView 反向垂直滚动方向
【发布时间】: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:) 如果需要我会发布一个新问题。

【问题讨论】:

    标签: c# winforms listview


    【解决方案1】:

    另一个想法是使用Items.Insert() 在顶部插入每个项目(Index=0)。我最近使用我创建的事件查看器做到了这一点,它似乎运行良好。

    【讨论】:

      【解决方案2】:

      根据我对您问题的理解,我认为这可能是您正在寻找的How to auto scroll down in WinForms ListView control when update new item?

      【讨论】:

        猜你喜欢
        • 2011-01-19
        • 2014-07-12
        • 1970-01-01
        • 2014-01-11
        • 2019-03-22
        • 1970-01-01
        • 2017-04-13
        • 2012-03-06
        • 2017-07-05
        相关资源
        最近更新 更多