private void listView1_SelectedIndexChanged(object sender, EventArgs e) { try { if (listView1.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listView1.SelectedItems[0]; ToolItemConfig tic = lvi.Tag as ToolItemConfig; txtName.Text = tic.称; txtImgName.Text = tic.图标; cbxButType.SelectedIndex = (int)tic.钮类型; cbxDisplyStyle.SelectedIndex = (int)tic.显示样式; txtButTag.Text = tic.标记; cbxParentName.Text = tic.父级名称; cbxIconPostion.SelectedIndex = (int)tic.图标位置; chkRight.Checked = tic.右对齐; } catch (Exception ex) { MsgBox.ShowException(ex, this); } }
private void UpdateToolItem(ToolItemConfig toolItem) { toolItem.称 = txtName.Text; toolItem.钮类型 = (ToolType)cbxButType.SelectedIndex; toolItem.图标 = txtImgName.Text; toolItem.图标位置 = (ToolIconStyle)cbxIconPostion.SelectedIndex; toolItem.显示样式 = (ToolDisplayStyle)cbxDisplyStyle.SelectedIndex; toolItem.右对齐 = chkRight.Checked; toolItem.父级名称 = cbxParentName.Text; toolItem.标记 = txtButTag.Text; }
private void butAdd_Click(object sender, EventArgs e) { try { ToolItemConfig newItem = new ToolItemConfig(); UpdateToolItem(newItem); AddItemToList(newItem); } catch (Exception ex) { MsgBox.ShowException(ex, this); } }
private void AddItemToList(ToolItemConfig tic) { int pIndex = -1; string caption = ""; if (string.IsNullOrEmpty(tic.父级名称) == false) { pIndex = listView1.Items.IndexOfKey(tic.父级名称); if (pIndex >= 0) { ToolItemConfig tiParent = listView1.Items[pIndex].Tag as ToolItemConfig; caption = " " + listView1.Items[pIndex].Text.Replace(tiParent.称, "") + tic.称; } } else { caption = tic.称; } ListViewItem itemNew = new ListViewItem(new string[] { caption, GetToolTypeAlias(tic.钮类型), tic.图标, GetToolIconStyleAlias(tic.图标位置), GetDisplayStyleAlias(tic.显示样式), (tic.右对齐)?"√":"" }, 0); itemNew.Tag = tic; itemNew.Name = tic.称; if (pIndex >= 0 && pIndex + 1 <= listView1.Items.Count - 1) { listView1.Items.Insert(pIndex + 1, itemNew); } else { listView1.Items.Add(itemNew); } if (tic.钮类型 == ToolType.ttDrowDownButton) { if (cbxParentName.Items.IndexOf(tic.称) < 0) { cbxParentName.Items.Add(tic.称); } } }
private void butDel_Click(object sender, EventArgs e) { try { if (listView1.SelectedItems.Count <= 0) { MessageBox.Show("请选择需要删除的项目。", "提示"); return; } ListViewItem delItem = listView1.SelectedItems[0]; ToolItemConfig ticDel = delItem.Tag as ToolItemConfig; int nextIndex = delItem.Index + 1; if (nextIndex <= listView1.Items.Count - 1) { ToolItemConfig ticNext = listView1.Items[nextIndex].Tag as ToolItemConfig; if (string.IsNullOrEmpty(ticNext.父级名称) == false && ticNext.父级名称 == ticDel.称) { MessageBox.Show("存在子项,不允许删除。", "提示"); return; } } DialogResult dr = MessageBox.Show("确认删除改项目吗", "提示", MessageBoxButtons.YesNo); if (dr == DialogResult.No) { return; } if (ticDel != null && ticDel.钮类型 == ToolType.ttDrowDownButton) { cbxParentName.Items.Remove(ticDel.称); } delItem.Remove(); } catch (Exception ex) { MsgBox.ShowException(ex, this); } }
private void butModify_Click(object sender, EventArgs e) { try { if (listView1.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listView1.SelectedItems[0]; ToolItemConfig tic = lvi.Tag as ToolItemConfig; UpdateToolItem(tic); listView1.Items.Remove(lvi); AddItemToList(tic); } catch (Exception ex) { MsgBox.ShowException(ex, this); } }