private void ButtonGroupSaveClick(object sender, EventArgs e)
        {
            var g = (ComputerPartGroup) treeViewGroupsAndParts.SelectedNode.Tag;
            var parentId = (int) comboBoxGroupParents.SelectedValue;

            using (var dbContext = new ComputerSetDatabaseContext())
            {
                dbContext.ComputerPartGroups.Attach(g);

                g.Name = textBoxName.Text;
                g.Parent = parentId == -1 ? null : dbContext.ComputerPartGroups.Single(x => x.Id == parentId);
                g.Show = checkBoxGroupShow.Checked;

                //TODO: g.Sequence

                dbContext.SaveChanges();

                DiagMsg(string.Format("zapisano nowe wartości - {0}", g.Name));
            }
        }
        private void ButtonAddNewGroupClick(object sender, EventArgs e)
        {
            var g = new ComputerPartGroup {Name = "_Nowa grupa", Show = false};
            using (var dbContext = new ComputerSetDatabaseContext())
            {
                var lastSequence = dbContext.ComputerPartGroups.Max(x => x.Sequence);
                g.Sequence = ++lastSequence;

                dbContext.ComputerPartGroups.Add(g);
                dbContext.SaveChanges();
            }

            buttonAddNewGroup.Enabled = false;
            buttonAddNewPart.Enabled = false;
            buttonDeleteSelected.Enabled = false;
            treeViewGroupsAndParts.Nodes.Clear();
            InitializeTreeView();

            DiagMsg(string.Format("utworzono nową grupę."));
        }
        private void SearchingForChildren(ComputerPart selectedItem)
        {
            var panel = (Panel) comboBoxParts.Parent.Parent.Parent;
            using (var dbContext = new ComputerSetDatabaseContext())
            {
                dbContext.ComputerParts.Attach(selectedItem);

                if (selectedItem.Group.Children.Any())
                {
                    foreach (var panelChild in selectedItem.Group.Children.
                        Select(child => panel.Controls.Find(child.Name.NormalizeAndAddSequence(child.Sequence), false)).SelectMany(
                            panelChilds => panelChilds))
                    {
                        _childControls.Add(panelChild);
                        panelChild.Enabled = true;
                    }
                }
            }
        }
        private void InitializeTreeView()
        {
            using (var dbContext = new ComputerSetDatabaseContext())
            {
                foreach (var g in dbContext.ComputerPartGroups.OrderBy(x => x.Sequence))
                {
                    var nodeGroup = new TreeNode { Tag = g, Text = g.Name };

                    foreach (var p in g.ComputerParts.OrderBy(x => x.Name))
                    {
                        var nodePart = new TreeNode { Tag = p, Text = p.Name };

                        nodeGroup.Nodes.Add(nodePart);
                    }

                    treeViewGroupsAndParts.Nodes.Add(nodeGroup);
                }
            }
        }
        //private static bool ContainsNode(TreeNode node1, TreeNode node2)
        //{
        //    if (node2.Parent == null) return false;
        //    if (node2.Parent.Equals(node1)) return true;
        //    // If the parent node is not null or equal to the first node,
        //    // call the ContainsNode method recursively using the parent of
        //    // the second node.
        //    return ContainsNode(node1, node2.Parent);
        //}
        private void SetGroupControls(ComputerPartGroup g)
        {
            customTabControl.SelectTab(tabPageGroup);

            textBoxName.Text = g.Name;
            checkBoxGroupShow.Checked = g.Show;

            using (var dbContext = new ComputerSetDatabaseContext())
            {
                var computerPartGroups = dbContext.ComputerPartGroups.ToList();
                computerPartGroups.Insert(0, new ComputerPartGroup { Id = -1, Name = "..." });

                comboBoxGroupParents.DataSource = computerPartGroups;
                comboBoxGroupParents.DisplayMember = "Name";
                comboBoxGroupParents.ValueMember = "Id";
            }

            comboBoxGroupParents.SelectedValue = g.Parent == null ? -1 : g.Parent.Id;
        }
        private void InitializePanel()
        {
            using (var dbContext = new ComputerSetDatabaseContext())
            {
                var additionalServiceControl = new AdditionalServicesControl();
                additionalServiceControl.SetServicesValues(dbContext.AdditionalServices.ToList(), "Name", "Id");
                additionalServiceControl.PriceChanged += ControlPriceChanged;
                panelGroupsAndParts.AddControl(additionalServiceControl);

                foreach (var g in dbContext.ComputerPartGroups.OrderByDescending(x => x.Sequence))
                {
                    var parts = g.ComputerParts.ToList();
                    parts.InsertFakeValue();

                    var control = new GroupsAndPartsControl { GroupName = g.Name, Name = g.Name.NormalizeAndAddSequence(g.Sequence) };
                    control.SetPartsValues(parts, "Name", "Id");
                    control.PriceChanged += ControlPriceChanged;

                    if (g.Parent != null)
                    {
                        control.Enabled = false;
                    }

                    panelGroupsAndParts.AddControl(control);
                }
            }
        }