// Invoked when user right click one message template and chose to delete that message template. private void ToolStripMenuItemDelMessageTemplate_Click(object sender, EventArgs e) { TreeNode node = treeViewMessageTemplate.SelectedNode; if (node == null) { return; } if (node.Tag == null) { return; } if (node.Tag.GetType() != typeof(MessageTemplateType)) { return; } MessageTemplateType messageTemplate = (MessageTemplateType)node.Tag; if (messageTemplate == null) { return; } MessageTemplateDAL.DeleteOneMessageTemplate(messageTemplate.TemplateId); // Find the category node. TreeNode[] categoryNodes = treeViewMessageTemplate.Nodes.Find(messageTemplate.TemplateCategoryId.ToString(), false); if (categoryNodes.Length > 0) { categoryNodes[0].Nodes.Remove(node); } }
// 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); }