简体   繁体   中英

How to update listview after Form.Show

I have a Form with a listview. After calling Form.Show I need to update my listview. However, after Form.Show is called regardless of my listview code, it comes up empty, no columns, no data. If I move the Form.Show till after my listview code, the listview shows correctly.

Here is my listview code :

private void InitializeListView()
{
    _snapshotList.BeginUpdate();
    _snapshotList.Items.Clear();
    foreach (ISnapshot snapshot in _snapshots)
    {
         string comment = InstanceFactory<ProjectRecoveryService>.Instance.RetrieveCommentsforSnapshot(snapshot);

         string[] sub = new string[] { snapshot.Name, snapshot.Version.ToString(), snapshot.CreatedDate.ToString(), comment };
         ListViewItem item = new ListViewItem(sub);
         item.Tag = snapshot;
         this._snapshotList.Items.Add(item);
    }
    _snapshotList.EndUpdate();
    this._snapshotList.Refresh();
}

A side note, I have another Form that is very similar but has a TreeView that someone else has extended which works as desired.

Any thoughts?

EDIT 1 This form needs be a single instance. After reading this post , my Form.Show code is structured like this :

        public static RestoreSnapshotDialog GetInstance()
        {
            if (_dialog == null)
            {
                _dialog = new RestoreSnapshotDialog();
                _dialog.Show(Control.FromHandle(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));
            }
            else
            {
                _dialog.BringToFront();
            }
            return _dialog;
        }

On a FormClosed event I set _dialog = null.

您必须处理Form.Shown事件以更新listview。

The only solution I could find was to call the Form.Show() after my listview was fully populated. So I create my own Form.Show by overriding Form.Show.

public new void Show()
{
    if (_showdialog)
    {
        _dialog.Show(Control.FromHandle(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));
    }
    else
    {
        _dialog.BringToFront();
    }
}

Calling this method after my listview solves my problem. However, all my other dialogs (not using listview) work as expected with the code from the original post. Thanks to Hans Passant for leading me to this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM