示例#1
0
    private void OnKeyUp(object sender, KeyEventArgs e)
    {
        //if (e.Modifiers != Keys.Control || e.KeyCode != Keys.Left || e.KeyCode != Keys.Right)
        //{
        //   return;
        //}
        if (e.Modifiers == Keys.Control && (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
        {
            try
            {
                ListViewItem2 lvi = (SelectedItems.Count == 1 ? SelectedItems[0] : null) as ListViewItem2;
                if (lvi == null)
                {
                    return;
                }

                switch (e.KeyCode)
                {
                case Keys.Left:
                    Collapse(lvi);
                    break;

                case Keys.Right:
                    Expand(lvi);
                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, TreeListView.MsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
示例#2
0
        private static ListViewItem2[] GenerateFileSystemItems(string dirPath, ListViewItem2 lviParent)
        {
            var fseItems = new List <ListViewItem2>();

            var subDirs = Directory.GetDirectories(dirPath);

            foreach (var d in subDirs)
            {
                var di       = new DirectoryInfo(d);
                var subItems = new[] { di.Name, di.Extension, di.CreationTime.ToLongDateString(), di.LastAccessTime.ToLongDateString() };

                var lvi = new ListViewItem2(subItems, lviParent)
                {
                    ForeColor = RandomColor.Next(), ImageIndex = 0
                };

                var lviFSItems = GenerateFileSystemItems(d, lvi);
                if (lviFSItems != null && lviFSItems.Length > 0)
                {
                    lvi.AddChildren(lviFSItems);
                    // TreeListView.AssignParent(lvi, lviParent);
                }

                fseItems.Add(lvi);
            }

            var lviFiles = GetFileItems(dirPath, lviParent);

            fseItems.AddRange(lviFiles);
            return(fseItems.ToArray());
        }
示例#3
0
        public static ListViewItem2[] CreateTree(IEnumerable <XElement> xElements, ListViewItem2 lviParent, bool skipEmpty)
        {
            var lvItems = new List <ListViewItem2>();

            foreach (XElement xe in xElements)
            {
                if (skipEmpty && xe.Attributes().All(xa => string.IsNullOrEmpty(xa.Value)))
                {
                    continue;
                }

                var subItems = xe.Attributes().Select(xa => xa.Value).ToArray();
                var lvi      = new ListViewItem2(subItems, lviParent)
                {
                    ForeColor = RandomColor.Next(), ImageIndex = 0
                };
                lvItems.Add(lvi);

                if (xe.Elements().Count() > 0)
                {
                    var lvis = CreateTree(xe.Elements(), lvi, true);
                    if (lvis.Length > 0)
                    {
                        lvi.AddChildren(lvis);
                    }
                }
            }

            return(lvItems.ToArray());
        }
示例#4
0
        private void InitializeListView(string dirPath)
        {
            listView.Clear();
            listView.Columns.AddRange(new[] {
                new ColumnHeader {
                    Text = "Name", Width = 250, TextAlign = HorizontalAlignment.Left
                },
                new ColumnHeader {
                    Text = "Type", Width = 100, TextAlign = HorizontalAlignment.Left
                },
                new ColumnHeader {
                    Text = "Length", Width = 200, TextAlign = HorizontalAlignment.Left
                },
                new ColumnHeader {
                    Text = "Date Created", Width = 300, TextAlign = HorizontalAlignment.Left
                }
            });

            if (false)
            {
                ListViewItem2 rootItem = new ListViewItem2(new string[] { Path.GetFileName(dirPath) }, null);
                this.listView.Items.Add(rootItem);

                ListViewItem2[] fseItems = GenerateFileSystemItems(dirPath, rootItem);
                rootItem.AddChildren(fseItems);
            }
            else
            {
                var fseItems = GenerateFileSystemItems(dirPath, null);
                foreach (var fseLvi in fseItems)
                {
                    listView.Items.Add(fseLvi);
                }
            }
        }
示例#5
0
        private static ListViewItem2[] PrepareTree(IList <XElement> xList, ListViewItem2 lviParent)
        {
            var lItems = new List <ListViewItem2>();

            for (int i = 0; i < xList.Count; ++i)
            {
                if (string.Compare(Names.ExpenseItem, xList[i].Name.LocalName) == 0)
                {
                    var li = GetListViewItem2(xList[i], lviParent);
                    li.ImageIndex = 0;
                    li.ForeColor  = RandomColor.Next();

                    lItems.Add(li);
                }
                else if (string.Compare(Names.ExpenseGroup, xList[i].Name.LocalName) == 0)
                {
                    var xaName = xList[i].Attribute(Names.Name);
                    var xaDate = xList[i].Attribute(Names.Date);

                    var subItems = new[] {
                        xaDate != null ? xaDate.Value : "?",
                        xaName != null ? xaName.Value : "Expense Group"
                    };

                    var xGroup = new ListViewItem2(subItems, null)
                    {
                        ImageIndex = 1,
                        ForeColor  = RandomColor.Next()
                    };

                    var xItems   = new List <XElement>(xList[i].Elements(XName.Get(Names.ExpenseItem)));
                    var lviItems = new ListViewItem2[xItems.Count];

                    for (int k = 0; k < xItems.Count; ++k)
                    {
                        var li = GetListViewItem2(xItems[k], xGroup);
                        li.ImageIndex = 0;
                        li.ForeColor  = RandomColor.Next();
                        lviItems[k]   = li;
                    }

                    xGroup.AddChildren(lviItems);

                    lItems.Add(xGroup);
                }
                else if (string.Compare(Names.Seperator, xList[i].Name.LocalName) == 0)
                {
                    lItems.Add(new ListViewItem2(null, null));
                }
                else
                {
                    continue;
                }
            }

            return(lItems.ToArray());
        }
示例#6
0
    public void ToggleExpandState(ListViewItem2 lvi)
    {
        if (lvi.Expanded)
        {
            Collapse(lvi);
            return;
        }

        Expand(lvi);
    }
示例#7
0
    public ListViewItem2(string[] subItems, ListViewItem2 lviParent, ListViewItem2[] lviChildren)
        : base(subItems)
    {
        // Debug.Assert(lviParent != null);
        Parent = lviParent;

        if (lviChildren != null)
        {
            Throw.IfTrue(Array.Exists(lviChildren, delegate(ListViewItem2 lvi2) { return(lvi2 == null); }), "One or more items in the ListViewItem2 array (children) is null.");
            _lviChildren = new List <ListViewItem2>(lviChildren);
        }
    }
示例#8
0
        private static ListViewItem2 GetListViewItem2(XElement xe, ListViewItem2 lviParent)
        {
            var subItems = new string[Names.ExpenseItemAttributes.Length];

            for (int i = 0; i < Names.ExpenseItemAttributes.Length; ++i)
            {
                var xa = xe.Attribute(XName.Get(Names.ExpenseItemAttributes[i]));
                subItems[i] = xa != null ? xa.Value : string.Empty;
            }

            return(new ListViewItem2(subItems, lviParent));
        }
示例#9
0
    public ListViewItem2 AddItem(ListViewItem2 lviItem)
    {
        Throw.IfNull(lviItem, "Specified item is null.");
        Throw.IfNotNull(lviItem.Parent, "Parent has to be a root level item.");
        Throw.IfTrue(lviItem.Parent == lviItem, "Self reference. Parent is same as self.");

        if (lviItem.Parent == null)
        {
            Items.Add(lviItem);
        }

        return(lviItem);
    }
示例#10
0
 private void OnMouseDoubleClick(object sender, MouseEventArgs e)
 {
     try
     {
         ListViewItem2 lvi = GetItemAt(e.X, e.Y) as ListViewItem2;
         if (lvi != null && lvi.HasChildren)
         {
             ToggleExpandState(lvi);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, TreeListView.MsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
示例#11
0
    public void Collapse(ListViewItem2 lvItem)
    {
        Throw.IfNull(lvItem, "Specified item is null.");
        if (!lvItem.HasChildren || !lvItem.Expanded)
        {
            return;
        }

        foreach (ListViewItem2 lv in lvItem.Children)
        {
            Collapse(lv);
            Items.Remove(lv);
        }

        lvItem.Expanded = false;
    }
示例#12
0
    private bool KeyDownProc(ref Message msg)
    {
        Debug.Assert(msg.Msg == Messages.WM_KEYDOWN);

        int wParam = msg.WParam.ToInt32();

        //int lParam = msg.LParam.ToInt32();
        //uint scanCode = (uint)((lParam >> 15) & 0x0000007f);
        //uint scanCode = (uint)((lParam >> 16) & 255);
        //uint vkCode = PInvoke.MapVirtualKey(scanCode, (uint)VirtualKeyMappingOption.MAPVK_VSC_TO_VK);
        //bool extendedKey = (lParam & 0x00800000) == 0x00800000;

        Keys  keyCode      = (Keys)Enum.ToObject(typeof(Keys), wParam);
        short ctrlPressed  = PInvoke.GetKeyState((int)VirtualKeyCodes.VK_CONTROL);
        short shiftPressed = PInvoke.GetKeyState((int)VirtualKeyCodes.VK_SHIFT);

        bool expandCmd   = keyCode == Keys.Add || (Control.ModifierKeys == Keys.Control && keyCode == Keys.Right);
        bool collapseCmd = keyCode == Keys.Subtract || (Control.ModifierKeys == Keys.Control && keyCode == Keys.Left);

        // CTRL-RightArrow or +: Expand the list view item (if it has children and is collapsed)
        if (expandCmd)
        {
            ListViewItem2 lvi2 = SelectedItems.Count == 1 ? SelectedItems[0] as ListViewItem2 : null;
            if (lvi2 != null && lvi2.Collapsed)
            {
                Expand(lvi2);
                return(true);
            }
        }

        // CTRL-LeftArrow or -: Collapse the list view item (if it has children and is expanded)
        if (collapseCmd)
        {
            ListViewItem2 lvi2 = SelectedItems.Count == 1 ? SelectedItems[0] as ListViewItem2 : null;
            if (lvi2 != null && lvi2.Expanded)
            {
                Collapse(lvi2);
                return(true);
            }
        }

        return(false);
    }
示例#13
0
    //public bool HasChildNodes(ListViewItem lvi)
    //{
    //   if (lvi.Tag == null || !(lvi.Tag is LVLink))
    //   {
    //      return false;
    //   }

    //   LVLink lvl = (LVLink)lvi.Tag;
    //   return lvl.HasChildren();
    //}

    //public ListViewItem GetParent(ListViewItem lvi)
    //{
    //   if (lvi.Tag == null || !(lvi.Tag is LVLink))
    //   {
    //      return null;
    //   }

    //   LVLink lvl = (LVLink)lvi.Tag;
    //   return lvl.Parent;
    //}

    //public int GetIndentLevel(ListViewItem lvi)
    //{
    //   ListViewItem lviParent = GetParent(lvi);

    //   int index = 0;
    //   while (lviParent != null)
    //   {
    //      ++index;
    //      lviParent = GetParent(lviParent);
    //   }

    //   return index;
    //}

    //public int GetRowIndex(ListViewItem lvi)
    //{
    //   return lvi.Index;
    //}

    #endregion

    #region Private Helper Methods

    private bool LButtonDblClickProc(Point clickLoc)
    {
        if (!OwnerDraw)
        {
            return(false);
        }

        ListViewItem2 lvi2 = GetItemAt(clickLoc.X, clickLoc.Y) as ListViewItem2;

        if (lvi2 == null)
        {
            return(false);
        }

        if (lvi2.HasChildren)
        {
            ToggleExpandState(lvi2);
        }

        return(true);
    }
示例#14
0
        private static ListViewItem2[] GetFileItems(string dirPath, ListViewItem2 lviParent)
        {
            var fseItems = new List <ListViewItem2>();

            string[] files = Directory.GetFiles(dirPath);
            foreach (string f in files)
            {
                var fi       = new FileInfo(f);
                var subItems = new[] { fi.Name, fi.Extension, fi.Length.ToString(), fi.CreationTime.ToLongDateString() };

                var lvi = new ListViewItem2(subItems, lviParent)
                {
                    ForeColor = RandomColor.Next(), ImageIndex = 1
                };
                // TreeListView.AssignParent(lvi, lviParent);

                fseItems.Add(lvi);
            }

            return(fseItems.ToArray());
        }
示例#15
0
    private void OnMouseClick(object sender, MouseEventArgs e)
    {
        if (!CheckBoxes)
        {
            return;
        }

        ListViewItem2 lvi = GetItemAt(e.X, e.Y) as ListViewItem2;

        if (lvi == null)
        {
            return;
        }

        Point     cbLoc  = this.PointToScreen(lvi.CheckBoxBounds.Location).GetShift(1, 1);
        Rectangle cbRect = new Rectangle(cbLoc, new Size(10, 10));

        if (e.Location.IsInRect(cbRect))
        {
            lvi.Checked = !lvi.Checked;
        }
    }
示例#16
0
    private bool LButtonUpProc(Point clickLoc)
    {
        if (!OwnerDraw || !clickLoc.Equals(this._clickLocation))
        {
            return(false);
        }

        // A mouse (left button) click has happened
        ListViewItem2 clickedItem = GetItemAt(clickLoc.X, clickLoc.Y) as ListViewItem2;

        if (clickedItem == null)
        {
            return(false);
        }

        clickedItem.Selected = clickedItem.Focused = true;
        if (clickedItem.HasChildren)
        {
            Rectangle pmImageRect = new Rectangle(clickedItem.PlusMinusLocation.GetShift(1, 1),
                                                  new Size(TreeListView.PlusImage.Width - 1, TreeListView.PlusImage.Height - 1));

            if (clickLoc.IsInRect(pmImageRect))
            {
                ToggleExpandState(clickedItem);
                return(true);
            }
        }

        Rectangle cbRect = new Rectangle(clickedItem.CheckBoxBounds.Location.GetShift(1, 1),
                                         new Size(clickedItem.CheckBoxBounds.Width - 1, clickedItem.CheckBoxBounds.Height - 1));

        if (clickLoc.IsInRect(cbRect))
        {
            clickedItem.Checked = !clickedItem.Checked;
            return(true);
        }

        return(false);
    }
示例#17
0
        public static ListViewItem2[] CreateTreeFromXML(XElement[] xeArray, ListViewItem2 lviParent)
        {
            var lvItems = new List <ListViewItem2>();

            foreach (XElement xe in xeArray)
            {
                var subItems = new string[] { xe.Name.LocalName, xe.HasElements ? "[...]" : xe.Value };
                var lvi      = new ListViewItem2(subItems, lviParent);
                lvItems.Add(lvi);

                XAttribute[] xas = ToArray <XAttribute>(xe.Attributes());
                if (xas.Length > 0)
                {
                    var xaLvis = new ListViewItem2[xas.Length];
                    for (int i = 0; i < xas.Length; ++i)
                    {
                        var sis = new string[] { xas[i].Name.LocalName, xas[i].Value };
                        xaLvis[i] = new ListViewItem2(sis, lvi);
                    }

                    lvi.AddChildren(xaLvis);
                }

                lvi.ImageIndex = xas.Length > 0 ? 1 : 0;

                XElement[] xes = ToArray(xe.Elements());
                if (xes.Length > 0)
                {
                    var lvis = CreateTreeFromXML(xes, lvi);
                    if (lvis.Length > 0)
                    {
                        lvi.AddChildren(lvis);
                    }
                }
            }

            return(lvItems.ToArray());
        }
示例#18
0
    //public static void AssignParent(ListViewItem lvi, ListViewItem lviParent)
    //{
    //   if (lvi.Tag == null)
    //   {
    //      lvi.Tag = new LVLink { Parent = lviParent };
    //   }
    //   else
    //   {
    //      Debug.Assert(lvi.Tag is LVLink);
    //      ((LVLink)lvi.Tag).Parent = lviParent;
    //   }
    //}

    //public static void AddChildren(ListViewItem lvi, ListViewItem[] lvChildren)
    //{
    //   if (lvi.Tag == null)
    //   {
    //      lvi.Tag = new LVLink { Children = new List<ListViewItem>(lvChildren) };
    //   }
    //   else
    //   {
    //      Debug.Assert(lvi.Tag is LVLink);
    //      List<ListViewItem> lvis = ((LVLink)lvi.Tag).Children;
    //      if (lvis == null)
    //      {
    //         lvis = new List<ListViewItem>(lvChildren);
    //      }
    //      else
    //      {
    //         lvis.AddRange(lvChildren);
    //      }
    //   }
    //}

    //public bool IsParentOf(ListViewItem lvi, ListViewItem lviParent)
    //{
    //   if (lvi.Tag == null || !(lvi.Tag is LVLink))
    //   {
    //      return false;
    //   }

    //   return lviParent == ((LVLink)lvi.Tag).Parent;
    //}

    public void Expand(ListViewItem2 lvi)
    {
        Throw.IfNull(lvi, "Specified item is null.");
        if (!lvi.HasChildren || lvi.Expanded)
        {
            return;
        }

        foreach (ListViewItem2 lv in lvi.Children)
        {
            Items.Insert(lvi.Index + 1, lv);
        }

        lvi.Expanded = true;

        //Debug.Assert(this.expandedLVList.Contains(lvi) == false);
        //this.expandedLVList.Add(lvi);

        //LVLink lvs = (LVLink)lvi.Tag;
        //foreach (ListViewItem lv in lvs.Children)
        //{
        //   Items.Insert(lvi.Index + 1, lv);
        //}
    }
示例#19
0
 /// <summary>
 /// If the parent is null, then it is a root level item
 /// </summary>
 public ListViewItem2(string[] subItems, ListViewItem2 lviParent)
     : this(subItems, lviParent, null)
 {
 }