Example #1
0
        /// <summary>
        /// Updates the file server listbox.
        /// </summary>
        static void UpdateServerListBox(ServerInfoListBox lb, IEnumerable <ServerInfoBase> servers,
                                        ServerInfoEventHandler changedEventHandler)
        {
            try
            {
                // Use Invoke to ensure we are in the correct thread
                lb.Invoke((Action) delegate
                {
                    // Store the selected item so we can restore it when done
                    var selected = lb.SelectedItem;

                    // Remove the update listener from existing items to make sure we don't add it twice (no harm in removing
                    // the event hook if it doesn't exist to begin with)
                    foreach (var s in lb.Items.OfType <ServerInfoBase>())
                    {
                        s.ServerChanged   -= changedEventHandler;
                        s.ProgressChanged -= changedEventHandler;
                    }

                    // Add the update listener to all items (after double-checking that the event listener isn't attached)
                    foreach (var s in servers)
                    {
                        s.ServerChanged   -= changedEventHandler;
                        s.ProgressChanged -= changedEventHandler;

                        s.ServerChanged   += changedEventHandler;
                        s.ProgressChanged += changedEventHandler;
                    }

                    // Re-add all items
                    lb.Items.Clear();
                    lb.Items.AddRange(servers.Cast <object>().ToArray());

                    // Restore the selected item if it is still in the list
                    if (servers.Any(x => x == selected))
                    {
                        lb.SelectedItem = selected;
                    }
                });
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
            }
        }
Example #2
0
        /// <summary>
        /// Updates the file server listbox.
        /// </summary>
        static void UpdateServerListBox(ServerInfoListBox lb, IEnumerable<ServerInfoBase> servers,
                                        ServerInfoEventHandler changedEventHandler)
        {
            try
            {
                // Use Invoke to ensure we are in the correct thread
                lb.Invoke((Action)delegate
                {
                    // Store the selected item so we can restore it when done
                    var selected = lb.SelectedItem;

                    // Remove the update listener from existing items to make sure we don't add it twice (no harm in removing
                    // the event hook if it doesn't exist to begin with)
                    foreach (var s in lb.Items.OfType<ServerInfoBase>())
                    {
                        s.ServerChanged -= changedEventHandler;
                        s.ProgressChanged -= changedEventHandler;
                    }

                    // Add the update listener to all items (after double-checking that the event listener isn't attached)
                    foreach (var s in servers)
                    {
                        s.ServerChanged -= changedEventHandler;
                        s.ProgressChanged -= changedEventHandler;

                        s.ServerChanged += changedEventHandler;
                        s.ProgressChanged += changedEventHandler;
                    }

                    // Re-add all items
                    lb.Items.Clear();
                    lb.Items.AddRange(servers.Cast<object>().ToArray());

                    // Restore the selected item if it is still in the list
                    if (servers.Any(x => x == selected))
                        lb.SelectedItem = selected;
                });
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
            }
        }