// When user done with editing the message template category. // Two possible cases: // 1) Add a new category, or // 2) Modify an existed category. private void treeViewMessageTemplate_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Label == null || e.Label.Trim().Length == 0) { e.CancelEdit = true; e.Node.BeginEdit(); return; } String categoryName = e.Label; MessageTemplateCategoryType categoryType = (MessageTemplateCategoryType)e.Node.Tag; if (categoryType != null) { // Modify an existed category. if (categoryName == categoryType.CategoryName) { return; } categoryType.CategoryName = categoryName; MessageTemplateCategoryDAL.UpdateOneMessageTemplateCategory(categoryType); this.treeViewMessageTemplate.LabelEdit = false; this.treeViewMessageTemplate.SelectedNode = e.Node; return; } // Add a new category. categoryType = new MessageTemplateCategoryType(); categoryType.ParentCategoryId = -1; categoryType.CategoryName = categoryName; categoryType.CategoryDescription = ""; // Check if there is any category with the same category name. MessageTemplateCategoryType existedCategoryType = MessageTemplateCategoryDAL.GetMessageTemplateCategory(categoryName); if (existedCategoryType != null) { MessageBox.Show("已存在此消息模板主题,请输入不同模板主题!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; e.Node.BeginEdit(); return; } // Insert the new category to database and return the new category id. int newCatId = 0; MessageTemplateCategoryDAL.InsertOneMessageTemplateCategory(categoryType, out newCatId); categoryType.CategoryId = newCatId; e.Node.EndEdit(false); e.Node.Tag = categoryType; this.treeViewMessageTemplate.SelectedNode = e.Node; this.treeViewMessageTemplate.LabelEdit = false; }
// Delete a message template category, if there are any message templates under // this category, prompt user. private void ToolStripMenuItemDelCategory_Click(object sender, EventArgs e) { TreeNode node = treeViewMessageTemplate.SelectedNode; if (node == null) { return; } if (node.Tag == null) { return; } if (node.Tag.GetType() != typeof(MessageTemplateCategoryType)) { return; } MessageTemplateCategoryType categoryType = (MessageTemplateCategoryType)node.Tag; int categoryId = categoryType.CategoryId; DataTable dtMessageTemplates = MessageTemplateDAL.GetAllMessageTemplatesWithCategoryId(categoryId); if (dtMessageTemplates.Rows.Count > 0) { if (MessageBox.Show(string.Format("确认删除主题 {0}? 我们将删除所有隶属该主题的消息模板", categoryType.CategoryName), "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) { return; } } // Either cases: // 1) The category has message templates and user confirmed to delete the category. // 2) The category has no message templates. // Delete all the relevant message templates first. foreach (DataRow dr in dtMessageTemplates.Rows) { int messageTemplateId = StringUtil.GetSafeInt(dr["TemplateId"]); MessageTemplateDAL.DeleteOneMessageTemplate(messageTemplateId); } MessageTemplateCategoryDAL.DeleteOneMessageTemplateCategory(categoryType.CategoryId); treeViewMessageTemplate.Nodes.Remove(node); }
private void LoadAllMessageTemplates() { this.tvMessageTemplates.Nodes.Clear(); DataTable dtMessageCategories = MessageTemplateCategoryDAL.GetAllMessageTemplateCategories(); DataTable dtMessageTemplates = MessageTemplateDAL.GetAllMessageTemplates(); foreach (DataRow drCategory in dtMessageCategories.Rows) { int categoryId = StringUtil.GetSafeInt(drCategory["CategoryId"]); String categoryName = StringUtil.GetSafeString(drCategory["CategoryName"]); MessageTemplateCategoryType catType = new MessageTemplateCategoryType(); TreeNode catNode = new TreeNode(); catNode.Tag = catType; catNode.Text = categoryName; catNode.Name = categoryId.ToString(); this.tvMessageTemplates.Nodes.Add(catNode); } foreach (DataRow drMessageTemplate in dtMessageTemplates.Rows) { int templateId = StringUtil.GetSafeInt(drMessageTemplate["TemplateId"]); int categoryId = StringUtil.GetSafeInt(drMessageTemplate["TemplateCategoryId"]); String templateName = StringUtil.GetSafeString(drMessageTemplate["TemplateName"]); String templateContent = StringUtil.GetSafeString(drMessageTemplate["TemplateContent"]); MessageTemplateType templateType = new MessageTemplateType(); templateType.TemplateId = templateId; templateType.TemplateCategoryId = categoryId; templateType.TemplateName = templateName; templateType.TemplateContent = templateContent; TreeNode msgNode = new TreeNode(); msgNode.Tag = templateType; msgNode.Text = templateName; TreeNode[] catNodes = this.tvMessageTemplates.Nodes.Find(categoryId.ToString(), false); if (catNodes.Length > 0) { catNodes[0].Nodes.Add(msgNode); } } this.tvMessageTemplates.ExpandAll(); }
// Load all message template categories. private void LoadAllMessageTemplateCategories() { DataTable dtCategories = MessageTemplateCategoryDAL.GetAllMessageTemplateCategories(); this.treeViewMessageTemplate.Nodes.Clear(); foreach (DataRow dr in dtCategories.Rows) { int categoryId = StringUtil.GetSafeInt(dr["CategoryId"]); String categoryName = StringUtil.GetSafeString(dr["CategoryName"]); MessageTemplateCategoryType categoryType = new MessageTemplateCategoryType(); categoryType.CategoryId = categoryId; categoryType.CategoryName = categoryName; TreeNode newNode = new TreeNode(); newNode.Tag = categoryType; newNode.Text = categoryName; newNode.Name = categoryId.ToString(); this.treeViewMessageTemplate.Nodes.Add(newNode); } }