0

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.

Community
  • 1
  • 1
Web
  • 1,685
  • 2
  • 22
  • 36
  • When does _snapshots get populated? – Tony Hopkinson Dec 30 '11 at 13:14
  • It gets populated after Form.Show. Populating _snapshots triggers the above method. – Web Dec 30 '11 at 13:18
  • So Form.Show has nothing to do with the issue then? When does the populate get called. Put some debugs on the relevant routines, make sure what you think is happening is happening. Going to be a "doh" moment this can't see anything wrong with the code you posted. – Tony Hopkinson Dec 30 '11 at 13:52
  • 1
    Sounds like there's something wrong with the way you call Show() or whatever triggers the listview code. Stuff you didn't post. Use the debugger, put a breakpoint on this method. And ensure you're actually updating the form object that the user is looking at instead of *another* one whose Show() method was never called. – Hans Passant Dec 30 '11 at 13:55

2 Answers2

1

You have to handle Form.Shown event to update the listview.

KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
  • Sorry...not following. What do I do with this event? Run my InitializeListView method when this event is triggered? – Web Dec 30 '11 at 13:25
  • I modified my code run the above InitializeListView method in the Form.Shown event. Still the same result as in the original post. – Web Dec 30 '11 at 13:34
0

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.

Web
  • 1,685
  • 2
  • 22
  • 36