Example #1
0
        void UpdateGrid(bool updateDevices)
        {
            lock (updateGridLock)
            {
                if (updateDevices)
                {
                    devices    = DeviceDetector.GetDevices().ToList();
                    interfaces = DeviceDetector.GetInterfaces().ToList();
                    devices.AddRange(interfaces);
                }
                var filter = FilterTextBox.Text.Trim();
                var view   = devices;
                if (EnableFilterCheckBox.Checked && !string.IsNullOrEmpty(filter))
                {
                    view = devices.Where(x =>
                                         comp(x.ClassDescription, filter) ||
                                         comp(x.Description, filter) ||
                                         comp(x.Manufacturer, filter) ||
                                         comp(x.DeviceId, filter))
                           .ToList();
                }
                // WORKAROUND: Remove SelectionChanged event.
                DeviceDataGridView.SelectionChanged -= DeviceDataGridView_SelectionChanged;
                DeviceDataGridView.DataSource        = view;
                // WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
                ControlsHelper.BeginInvoke(() =>
                {
                    DeviceDataGridView.SelectionChanged += DeviceDataGridView_SelectionChanged;
                    DeviceDataGridView_SelectionChanged(DeviceDataGridView, new EventArgs());
                });
                DeviceTabPage.Text = string.Format("{0} Devices on {1:yyyy-MM-dd HH:mm:ss}", view.Count, DateTime.Now);
                var dis     = devices.Where(x => string.IsNullOrEmpty(x.ParentDeviceId)).ToArray();
                var classes = devices.Select(x => x.ClassGuid).Distinct();

                // Suppress repainting the TreeView until all the objects have been created.
                DevicesTreeView.Nodes.Clear();
                TreeImageList.Images.Clear();
                foreach (var cl in classes)
                {
                    var icon = DeviceDetector.GetClassIcon(cl);
                    if (icon != null)
                    {
                        Image img = new Icon(icon, 16, 16).ToBitmap();
                        TreeImageList.Images.Add(cl.ToString(), img);
                    }
                }
                DevicesTreeView.BeginUpdate();
                foreach (DeviceInfo di in dis)
                {
                    var tn = new TreeNode(System.Environment.MachineName);
                    tn.Tag              = di;
                    tn.ImageKey         = di.ClassGuid.ToString();
                    tn.SelectedImageKey = di.ClassGuid.ToString();
                    DevicesTreeView.Nodes.Add(tn);
                    AddChildren(tn);
                }
                DevicesTreeView.EndUpdate();
                DevicesTreeView.ExpandAll();
            }
        }
Example #2
0
 private void UpdateListAndTree(bool updateDevices)
 {
     lock (updateGridLock)
     {
         if (updateDevices)
         {
             var newDevices    = DeviceDetector.GetDevices().ToList();
             var newInterfaces = DeviceDetector.GetInterfaces().ToList();
             if (devices.Count > 0)
             {
                 var addedDevices   = newDevices.Where(n => !devices.Any(o => o.DeviceId == n.DeviceId)).ToList();
                 var removedDevices = devices.Where(n => !newDevices.Any(o => o.DeviceId == n.DeviceId)).ToList();
                 AddLog("Added", addedDevices);
                 AddLog("Removed", removedDevices);
             }
             if (interfaces.Count > 0)
             {
                 var addedInterfaces   = newInterfaces.Where(n => !interfaces.Any(o => o.DeviceId == n.DeviceId)).ToList();
                 var removedInterfaces = interfaces.Where(n => !newInterfaces.Any(o => o.DeviceId == n.DeviceId)).ToList();
                 AddLog("Added", addedInterfaces);
                 AddLog("Removed", removedInterfaces);
             }
             // Store new list.
             devices    = newDevices;
             interfaces = newInterfaces;
             // Note: 'devices' and 'interfaces' share same DeviceId.
             // Don't just select by DeviceID from 'devices'.
             allDevices = newDevices;
             allDevices.AddRange(newInterfaces);
         }
         var filter   = FilterStripTextBox.Text.Trim();
         var filtered = JocysCom.ClassLibrary.Data.Linq.ApplySearch(allDevices, filter, (x) =>
         {
             return(string.Join(" ",
                                x.ClassDescription,
                                x.Description,
                                x.Manufacturer,
                                x.DeviceId));
         }).ToList();
         BindDeviceList(filtered);
         BindDeviceTree(filtered);
     }
 }