示例#1
0
        /// <summary>
        /// delete group and all child nodes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            try {
                using (var objProvider = new MailProvider()) {
                    //get parent  group node
                    Group objParentGroup = objProvider.GetGroup((int)treeView1.SelectedNode.Tag, treeView1.SelectedNode.Name);
                    //get receiver node
                    Receiver objReceiver = objProvider.GetReceiver((int)treeView1.SelectedNode.Tag, treeView1.SelectedNode.Name);

                    //if selected is receiver node
                    if (objReceiver != null)
                    {
                        objProvider.RemoveReceiver(objReceiver);
                    }

                    //if selected is group parent node
                    if (objParentGroup != null)
                    {
                        //get all childs Recievers
                        List <Group> objChildsToDeleteList = objProvider.Base.Groups.
                                                             Where(gr => gr.LeftKey >= objParentGroup.LeftKey && gr.RightKey <= objParentGroup.RightKey).ToList();
                        //delete all childs
                        foreach (var node in objChildsToDeleteList)
                        {
                            List <Receiver> objReceiversList = objProvider.Base.Receivers.Where(r => r.Group.Id == node.Id).ToList();
                            for (int iJ = 0; iJ < objReceiversList.Count; ++iJ)
                            {
                                objProvider.RemoveReceiver(objReceiversList[iJ]);
                            }
                            objProvider.RemoveGroup(node);
                        }

                        //update parent branch
                        List <Group> objParentsList = objProvider.Base.Groups.Where(gr => gr.RightKey > objParentGroup.RightKey && gr.LeftKey < objParentGroup.LeftKey).ToList();
                        foreach (var node in objParentsList)
                        {
                            node.RightKey = node.RightKey - (objParentGroup.RightKey - objParentGroup.LeftKey + 1);
                        }

                        //update other childs
                        List <Group> objChildList = objProvider.Base.Groups.Where(gr => gr.LeftKey > objParentGroup.RightKey).ToList();
                        foreach (var node in objChildList)
                        {
                            node.LeftKey  = node.LeftKey - (objParentGroup.RightKey - objParentGroup.LeftKey + 1);
                            node.RightKey = node.RightKey - (objParentGroup.RightKey - objParentGroup.LeftKey + 1);
                        }
                    }
                }
                UpdateTree();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }