using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MenuTool { public partial class Form1 : Form { List ListMenu = null; SysMenu CurrentMenu = null; bool IsDeleteMenu = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ReadMenu(); } #region 读取菜单 public void ReadMenu() { ListMenu = StaticModel.GetListMenu(); List list = GetListMenu(0); treeView1.Nodes.Clear(); foreach (var item in list) { TreeNode node = new TreeNode(); node.Text = item.ModuleName; node.Tag = item; if (item.IsUse == 1) { node.Checked = true; } else { node.Checked = false; } treeView1.Nodes.Add(node); BindChildTreeView(node, item.ModuleNo); } } #endregion #region 绑定树节点 public void BindChildTreeView(TreeNode ParentNode, int ParentModuleNo) { List list = GetListMenu(ParentModuleNo); foreach (var item in list) { TreeNode node = new TreeNode(); node.Text = item.ModuleName; node.Tag = item; if (item.IsUse == 1) { node.Checked = true; } else { node.Checked = false; } ParentNode.Nodes.Add(node); BindChildTreeView(node, item.ModuleNo); } } #endregion #region 读取子菜单 public List GetListMenu(int ParentModuleNo) { if (ListMenu == null) return new List(); var query = from item in ListMenu where item.ParentModuleNo == ParentModuleNo orderby item.SortNo select item; List list = query.ToList(); //int index = 11; //foreach (var item in list) //{ // item.SortNo = index.ToString(); // index++; //} return list; } #endregion #region 保存选择菜单 void SaveTreeView() { foreach (TreeNode item in treeView1.Nodes) { SysMenu model = (SysMenu)item.Tag; if (item.Checked == true) { model.IsUse = 1; } else { model.IsUse = 0; } StaticModel.ChangeMenuState(model.id, model.IsUse); SaveTreeNode(item); } } void SaveTreeNode(TreeNode node) { foreach (TreeNode item in node.Nodes) { SysMenu model = (SysMenu)item.Tag; if (item.Checked == true) { model.IsUse = 1; } else { model.IsUse = 0; } StaticModel.ChangeMenuState(model.id, model.IsUse); SaveTreeNode(item); } } #endregion #region 选择所有下属性节点 bool TreeNodeCheckEvent = true; public void SelectNextTreeViewChecked(TreeNode node) { foreach (TreeNode item in node.Nodes) { SysMenu model = (SysMenu)item.Tag; item.Checked = node.Checked; if (item.Checked == true) { model.IsUse = 1; } else { model.IsUse = 0; } StaticModel.ChangeMenuState(model.id, model.IsUse); SelectNextTreeViewChecked(item); } } #endregion #region 选择所有上属节点 public void SelectPrevTreeViewChecked(TreeNode node) { if (node.Checked == false) return; if (node.Parent == null) return; TreeNode item = (TreeNode)node.Parent; item.Checked = true; SysMenu model = (SysMenu)item.Tag; if (item.Checked == true) { model.IsUse = 1; } else { model.IsUse = 0; } StaticModel.ChangeMenuState(model.id, model.IsUse); SelectPrevTreeViewChecked(item); } #endregion #region 清空 void Clear() { IsDeleteMenu = false; txtMenuNo.Text = ""; txtParentMenuNo.Text = ""; txtMenuName.Text = ""; txtModuleUrl.Text = ""; chk_PopedomType.Checked = true; CurrentMenu = null; } #endregion private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { SysMenu model = (SysMenu)e.Node.Tag; txtMenuNo.Text = model.ModuleNo.ToString(); txtParentMenuNo.Text = model.ParentModuleNo.ToString(); txtMenuName.Text = model.ModuleName; txtModuleUrl.Text = model.ModuleUrl; txtSortNo.Value = Convert.ToInt32(model.SortNo); if (model.PopedomType == 0) { chk_PopedomType.Checked = false; } else { chk_PopedomType.Checked = true; } CurrentMenu = model; IsDeleteMenu = false; } private void cmdAdd_Click(object sender, EventArgs e) { //IsDeleteMenu = false; //txtMenuNo.Text = ""; //txtParentMenuNo.Text =""; //txtMenuName.Text = ""; //txtModuleUrl.Text = ""; //chk_PopedomType.Checked = true; //CurrentMenu = null; string MenuNo = txtMenuNo.Text.Trim(); string ParentMenuNo = txtParentMenuNo.Text.Trim(); if (ParentMenuNo == "") ParentMenuNo = "0"; if (string.IsNullOrEmpty(MenuNo) == true) { MessageBox.Show("菜单编号必须填写"); return; } if (DataRegex.IsNumberInteger(MenuNo) == false) { MessageBox.Show("菜单编号必须是数字"); return; } if (string.IsNullOrEmpty(ParentMenuNo) == false && DataRegex.IsNumberInteger(ParentMenuNo) == false) { MessageBox.Show("父菜单编号必须是数字"); return; } if (CurrentMenu == null) CurrentMenu = new SysMenu(); CurrentMenu.id = 0; CurrentMenu.ModuleNo = Convert.ToInt32(MenuNo); CurrentMenu.ParentModuleNo = Convert.ToInt32(ParentMenuNo); CurrentMenu.ModuleName = txtMenuName.Text.Trim(); CurrentMenu.ModuleUrl = txtModuleUrl.Text.Trim(); CurrentMenu.SortNo = txtSortNo.Value.ToString(); if (chk_PopedomType.Checked == false) { CurrentMenu.PopedomType = 0; } else { CurrentMenu.PopedomType = 1; } List list = GetListMenu(CurrentMenu.ParentModuleNo); if (CurrentMenu.id == 0 || IsDeleteMenu == true) { //CurrentMenu.SortNo = Convert.ToString(list.Count + 11); } if (string.IsNullOrEmpty(CurrentMenu.ModuleName) == true) { MessageBox.Show("菜单名称必须填写"); return; } foreach (var item in list) { if (item.id == CurrentMenu.id) continue; StaticModel.SaveMenu(item); } bool bk = StaticModel.SaveMenu(CurrentMenu); if (bk == false) { MessageBox.Show("菜单编号已经存在"); return; } CurrentMenu = null; ReadMenu(); MessageBox.Show("新增成功"); } private void cmdSave_Click(object sender, EventArgs e) { string MenuNo = txtMenuNo.Text.Trim(); string ParentMenuNo = txtParentMenuNo.Text.Trim(); if (ParentMenuNo == "") ParentMenuNo = "0"; if (string.IsNullOrEmpty(MenuNo) == true) { MessageBox.Show("菜单编号必须填写"); return; } if (DataRegex.IsNumberInteger(MenuNo) == false) { MessageBox.Show("菜单编号必须是数字"); return; } if (string.IsNullOrEmpty(ParentMenuNo) == false && DataRegex.IsNumberInteger(ParentMenuNo) == false) { MessageBox.Show("父菜单编号必须是数字"); return; } if (CurrentMenu == null) CurrentMenu = new SysMenu(); CurrentMenu.ModuleNo = Convert.ToInt32(MenuNo); CurrentMenu.ParentModuleNo = Convert.ToInt32(ParentMenuNo); CurrentMenu.ModuleName = txtMenuName.Text.Trim(); CurrentMenu.ModuleUrl = txtModuleUrl.Text.Trim(); CurrentMenu.SortNo = txtSortNo.Value.ToString(); if (chk_PopedomType.Checked == false) { CurrentMenu.PopedomType = 0; } else { CurrentMenu.PopedomType = 1; } //List list = GetListMenu(CurrentMenu.ParentModuleNo); if (CurrentMenu.id == 0 || IsDeleteMenu == true) { //CurrentMenu.SortNo = Convert.ToString(list.Count + 11); } if (string.IsNullOrEmpty(CurrentMenu.ModuleName) == true) { MessageBox.Show("菜单名称必须填写"); return; } //foreach (var item in list) //{ // if (item.id == CurrentMenu.id) continue; // StaticModel.SaveMenu(item); //} bool bk = StaticModel.SaveMenu(CurrentMenu); if (bk == false) { MessageBox.Show("菜单编号已经存在"); return; } CurrentMenu = null; ReadMenu(); MessageBox.Show("修改成功"); } private void cmdDel_Click(object sender, EventArgs e) { //真删除 if (CurrentMenu == null) { MessageBox.Show("请选择菜单"); return; } StaticModel.DeleteMenu(CurrentMenu.id); Clear(); ReadMenu(); MessageBox.Show("成功"); } private void cmdDelete2_Click(object sender, EventArgs e) { //假删除 if (CurrentMenu == null) { MessageBox.Show("请选择菜单"); return; } StaticModel.DeleteMenu2(CurrentMenu.id); Clear(); ReadMenu(); MessageBox.Show("成功"); } private void cmdPrev_Click(object sender, EventArgs e) { if (CurrentMenu == null) { MessageBox.Show("请选择菜单"); return; } if (Convert.ToInt32(CurrentMenu.SortNo) - 10 == 1) { MessageBox.Show("已经是第一个了"); return; } List list = GetListMenu(CurrentMenu.ParentModuleNo); if (list.Count == 1) { MessageBox.Show("只有一个菜单不能调整"); return; } for (int i = 0; i < list.Count; i++) { if (CurrentMenu.SortNo == list[i].SortNo) { list[i].SortNo = Convert.ToString(Convert.ToInt32(list[i].SortNo) - 1); StaticModel.SetMenuPosition(list[i].id, list[i].SortNo); list[i - 1].SortNo = Convert.ToString(Convert.ToInt32(list[i - 1].SortNo) + 1); StaticModel.SetMenuPosition(list[i - 1].id, list[i - 1].SortNo); } else { StaticModel.SetMenuPosition(list[i].id, list[i].SortNo); } } Clear(); ReadMenu(); //MessageBox.Show("成功"); } private void cmdNext_Click(object sender, EventArgs e) { if (CurrentMenu == null) { MessageBox.Show("请选择菜单"); return; } List list = GetListMenu(CurrentMenu.ParentModuleNo); if (list.Count == 1) { MessageBox.Show("只有一个菜单不能调整"); return; } if (Convert.ToInt32(CurrentMenu.SortNo) - 10 == list.Count) { MessageBox.Show("已经是最后了"); return; } bool bk = false; for (int i = 0; i < list.Count; i++) { if (bk == true) continue; if (CurrentMenu.SortNo == list[i].SortNo) { list[i].SortNo = Convert.ToString(Convert.ToInt32(list[i].SortNo) + 1); StaticModel.SetMenuPosition(list[i].id, list[i].SortNo); list[i + 1].SortNo = Convert.ToString(Convert.ToInt32(list[i + 1].SortNo) - 1); StaticModel.SetMenuPosition(list[i + 1].id, list[i + 1].SortNo); bk = true; } else { StaticModel.SetMenuPosition(list[i].id, list[i].SortNo); } } Clear(); ReadMenu(); //MessageBox.Show("成功"); } private void txtMenuNo_Leave(object sender, EventArgs e) { string MenuNo = txtMenuNo.Text.Trim(); if (string.IsNullOrEmpty(txtMenuNo.Text) == true) return; if (DataRegex.IsNumberInteger(MenuNo) == false) return; int ModuleNo = Convert.ToInt32(MenuNo); SysMenu model = StaticModel.GetDeleteMenu(ModuleNo); if (model == null) return; IsDeleteMenu = true; CurrentMenu = model; txtMenuNo.Text = model.ModuleNo.ToString(); txtParentMenuNo.Text = model.ParentModuleNo.ToString(); txtMenuName.Text = model.ModuleName; txtModuleUrl.Text = model.ModuleUrl; if (model.PopedomType == 0) { chk_PopedomType.Checked = false; } else { chk_PopedomType.Checked = true; } } private void cmdMenuCheck_Click(object sender, EventArgs e) { SaveTreeView(); MessageBox.Show("保存成功"); } private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { if (TreeNodeCheckEvent == false) return; TreeNodeCheckEvent = false; TreeNode item = e.Node; SysMenu model = (SysMenu)item.Tag; if (item.Checked == true) { model.IsUse = 1; } else { model.IsUse = 0; } StaticModel.ChangeMenuState(model.id, model.IsUse); SelectNextTreeViewChecked(e.Node); SelectPrevTreeViewChecked(e.Node); TreeNodeCheckEvent = true; } private void cmdSql_Click(object sender, EventArgs e) { if (CurrentMenu == null) { MessageBox.Show("请选择菜单"); return; } txtsql.Text = StaticModel.GetMenuSql(CurrentMenu.id); } private void cmdCopySql_Click(object sender, EventArgs e) { Clipboard.SetDataObject(txtsql.Text); } private void cmdMenuSort_Click(object sender, EventArgs e) { List ListModel = StaticModel.GetListChildMenu(CurrentMenu.ModuleNo); if (ListModel == null) { MessageBox.Show("该菜单下面没有子菜单"); return; } foreach (var item in ListModel) { int sort = 101 + ListModel.IndexOf(item); StaticModel.SetMenuPosition(item.id, sort.ToString()); } Clear(); ReadMenu(); MessageBox.Show("保存成功"); } } }