public void Add(bool asParent, string achievementCategoryName, string functionValue, bool isLegacy) { var function = functionHandler.GetSelectedFunction(); if (function == null) { MessageBox.Show("Invalid function selected!" + Environment.NewLine + Environment.NewLine + "Category is not added.", "Invalid function", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var selectedCategory = GetSelectedAchievementCategory(); if (selectedCategory == null) { MessageBox.Show("No category selected!" + Environment.NewLine + Environment.NewLine + "Category is not added.", "No category", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int location = asParent ? 1 : selectedCategory.Location + 1; AchievementCategory parent = asParent ? selectedCategory : selectedCategory.Parent; achievementCategoryName = string.IsNullOrEmpty(achievementCategoryName) ? function.Description : achievementCategoryName; var category = new AchievementCategory() { Location = location, Name = achievementCategoryName, Parent = parent, Function = function, FunctionValue = functionValue, Legacy = isLegacy }; dataManager.Add(category); if (!asParent) { var node = FindAchievementCategoryNode(tvwAchievementCategories.Nodes, parent); TreeNodeCollection nodes = null; if (node != null && node.Nodes != null) { nodes = node.Nodes; } else { nodes = tvwAchievementCategories.Nodes; } nodes.Insert(tvwAchievementCategories.SelectedNode.Index + 1, new AchievementCategoryTreeNode(category)); // Need to add this to ensure correct location numbers var categories = nodes.Cast <AchievementCategoryTreeNode>().Select(x => x.AchievementCategory).ToList(); dataManager.UpdateLocations(dataManager.GetLast(), categories); } RefreshTreeView(); // Make sure the last added achievement category is selected category = dataManager.GetLast(); var selectNode = FindAchievementCategoryNode(tvwAchievementCategories.Nodes, category); tvwAchievementCategories.SelectedNode = selectNode; }