private void buttonAdd_Click(object sender, System.EventArgs e)
 {
     try
     {
         TreeListViewItem newitem = new TreeListViewItem("treeListView" + _items.Owner.ItemsCount.ToString());
         TreeNode         node    = new TreeNode(newitem.Text);
         node.Tag = newitem;
         if (treeView1.SelectedNode != null)
         {
             TreeListViewItem item = (TreeListViewItem)treeView1.SelectedNode.Tag;
             if (item.Items.Add(newitem) > -1)
             {
                 treeView1.SelectedNode.Nodes.Add(node);
             }
         }
         else
         if (_items.Add(newitem) > -1)
         {
             treeView1.Nodes.Add(node);
         }
         if (node.Index > -1)
         {
             treeView1.SelectedNode = node;
         }
     }
     catch (Exception ex) { MessageBox.Show(ex.Message); }
 }
Exemplo n.º 2
0
        public void PopulateTree(XElement element, TreeListViewItemCollection items)
        {
            foreach (XElement node in element.Nodes())
            {
                TreeListViewItem item  = new TreeListViewItem();
                string           title = node.Name.LocalName.Trim();
                item.Text = title;
                if (title == "Device")
                {
                    var attr = node.Attribute("ItemType")?.Value;
                    switch (attr)
                    {
                    case "Channel": item.ImageIndex = 1; break;

                    case "RStreamer": item.ImageIndex = 3; break;

                    default: break;
                    }
                    item.SubItems.Add(node.Attribute("UniqueID")?.Value);
                    item.SubItems.Add(node.Attribute("ItemType")?.Value);
                }
                else
                {
                    item.ImageIndex = 2;
                    item.SubItems.Add(node.Attribute("Value")?.Value);
                }

                if (node.HasElements)
                {
                    PopulateTree(node, item.Items);
                }
                items.Add(item);
            }
        }
Exemplo n.º 3
0
        public void LoadItems <T>(IEnumerable <T> items, TreeListViewItemCollection Items, Func <T, string> getId, Func <T, string> getParentId, Func <T, string> getDisplayName, Func <T, int> getImageIndex, List <string> lstPropertyName)
        {
            //List<string> lstPropertyName = new List<string>();
            Items.Clear();
            dictItems.Clear();

            foreach (var item in items)
            {
                var id          = getId(item);
                var displayName = getDisplayName(item);
                var imageIndex  = -1;
                if (getImageIndex != null)
                {
                    imageIndex = getImageIndex(item);
                }

                var treeListViewItem = new TreeListViewItem();
                treeListViewItem.Name = id;
                treeListViewItem.Text = displayName;
                treeListViewItem.Tag  = item;
                if (imageIndex >= 0)
                {
                    treeListViewItem.ImageIndex = imageIndex;
                }

                foreach (string propertyName in lstPropertyName)
                {
                    object obj = PublicMethods.GetPropertyValue(item, propertyName);;
                    string str = "";
                    if (obj != null)
                    {
                        str = obj.ToString();
                    }
                    treeListViewItem.SubItems.Add(str);
                }

                dictItems.Add(id, treeListViewItem);
            }

            foreach (var id in dictItems.Keys)
            {
                var item     = GetItem(id);
                var obj      = (T)item.Tag;
                var parentId = getParentId(obj);

                if (parentId != null)
                {
                    var parentNode = GetItem(parentId.ToString());
                    parentNode.Items.Add(item);
                }
                else
                {
                    Items.Add(item);
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Gets the items that are visible in the TreeListView
 /// </summary>
 /// <returns>A collection of items</returns>
 public TreeListViewItemCollection GetVisibleItems()
 {
     TreeListViewItemCollection visibleItems = new TreeListViewItemCollection();
     if(base.Items.Count == 0) return visibleItems;
     if (TopItem == null) return visibleItems;
     int firstItemIndex = TopItem.Index;
     int itemsPerPageCount =
         APIsUser32.SendMessage(new HandleRef(this, Handle), (int) APIsEnums.ListViewMessages.GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();
     int lastVisibleItemIndex = firstItemIndex + itemsPerPageCount > base.Items.Count ?
         base.Items.Count : firstItemIndex + itemsPerPageCount;
     for(int i = firstItemIndex; i < lastVisibleItemIndex; i++)
         visibleItems.Add((TreeListViewItem) base.Items[i]);
     return visibleItems;
 }
Exemplo n.º 5
0
        public void LoadPartItems <T, K>(IEnumerable <T> items, TreeListViewItemCollection Items, Func <T, string> getId, Func <T, string> getParentId, Func <T, string> getDisplayName, Dictionary <string, string> dictPropertyName, Func <T, int> getImageIndex, Func <T, Color> getForeColor) where K : class
        {
            this.BeginUpdate();
            this.SuspendLayout();

            #region 将TreeListViewItem放到字典中

            this.Columns.Clear();
            int columnWidth    = (this.Width - 10) / (dictPropertyName.Count + 1);
            int coulumnCounter = 0;
            //先初始化列
            foreach (KeyValuePair <string, string> dictItem in dictPropertyName)
            {
                ColumnHeader ch = new ColumnHeader();
                ch.Text = dictItem.Value;
                if (coulumnCounter == 0)
                {
                    ch.Width = columnWidth * 2;
                }
                else
                {
                    ch.Width = columnWidth;
                }
                coulumnCounter++;

                this.Columns.Add(ch);
            }

            //再初始化数据行
            foreach (var item in items)
            {
                var id         = getId(item);
                var imageIndex = -1;
                if (getImageIndex != null)
                {
                    imageIndex = getImageIndex(item);
                }

                var treeListViewItem = new TreeListViewItem();
                treeListViewItem.Name = id;
                treeListViewItem.Tag  = item;
                if (imageIndex >= 0)
                {
                    treeListViewItem.ImageIndex = imageIndex;
                }
                treeListViewItem.ForeColor = getForeColor(item);

                K clsK = item as K;
                if (clsK != null)
                {
                    #region TreeListViewItem

                    int columnCounter = 0;
                    foreach (KeyValuePair <string, string> dictItem in dictPropertyName)
                    {
                        object objValue = PublicMethods.GetPropertyValue(item, dictItem.Key);
                        string strValue = objValue == null ? "" : objValue.ToString();
                        if (columnCounter == 0)
                        {
                            treeListViewItem.Text = strValue;
                        }
                        else
                        {
                            treeListViewItem.SubItems.Add(strValue);
                        }
                        columnCounter++;
                    }
                    #endregion
                }
                else
                {
                    treeListViewItem.Text = getDisplayName(item);
                }

                dictItems.Add(id, treeListViewItem);
            }

            #endregion

            #region 将TreeListViewItem进行组装

            foreach (var id in dictItems.Keys)
            {
                var item     = GetItem(id);
                var obj      = (T)item.Tag;
                var parentId = getParentId(obj);

                if (parentId != null)
                {
                    var parentNode = GetItem(parentId.ToString());
                    parentNode.Items.Add(item);
                }
                else
                {
                    Items.Add(item);
                }
            }

            #endregion

            this.ResumeLayout(true);
            this.EndUpdate();
        }
Exemplo n.º 6
0
 internal void GetCheckedItems(ref TreeListViewItemCollection items)
 {
     if(Checked) items.Add(this);
     foreach(TreeListViewItem item in Items)
         item.GetCheckedItems(ref items);
 }