winform - 工具栏/菜单栏

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace 菜单和工具栏
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 #region  窗体载入
 20 
 21         // 窗体载入
 22         private void Form1_Load(object sender, EventArgs e)
 23         {
 24             // 状态栏ToolStripMenuItem处于【√】勾选状态
 25             状态栏ToolStripMenuItem.Checked = true;
 26             状态栏ToolStripMenuItem.CheckState = CheckState.Checked;
 27             // 设置 状态栏菜单的CheckOnClick 为 true, 当鼠标单击 状态栏菜单时,会根据CheckedChanged设置勾选状态
 28             // 即: 处于【√】时,取消勾选, 否则勾选
 29             状态栏ToolStripMenuItem.CheckOnClick = true;
 30             // 显示控件: statusStrip1
 31             statusStrip1.Visible = true;
 32 
 33             // 工具栏ToolStripMenuItem处于【√】勾选状态
 34             工具栏ToolStripMenuItem.Checked = true;
 35             工具栏ToolStripMenuItem.CheckState = CheckState.Checked;
 36             工具栏ToolStripMenuItem.CheckOnClick = true;
 37             toolStrip1.Visible = true;
 38 
 39             // 工具栏按钮的显示样式 : 图片+文字
 40             toolStripButton1.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
 41             toolStripButton1.TextImageRelation = TextImageRelation.ImageBeforeText;     // 图片在文字前面
 42 
 43             toolStripButton2.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
 44             toolStripButton2.TextImageRelation = TextImageRelation.ImageAboveText;      // 图片在文字上面
 45 
 46             toolStripButton3.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
 47             toolStripButton3.TextImageRelation = TextImageRelation.Overlay;                     // 文字覆盖图片
 48 
 49             toolStripButton4.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
 50             toolStripButton4.TextImageRelation = TextImageRelation.TextBeforeImage;    // 图片在文字后面
 51 
 52             toolStripButton5.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
 53             toolStripButton5.TextImageRelation = TextImageRelation.TextAboveImage;      // 图片在文字下面
 54 
 55         }
 56 #endregion
 57 
 58 #region   退出菜单项
 59         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
 60         {
 61             // 关闭窗口
 62             this.Close();
 63         }
 64 #endregion
 65 
 66 #region 关于菜单项
 67 
 68         private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
 69         {
 70             //【关于对话框】
 71             AboutBox aboutbox = new AboutBox();
 72             aboutbox.ShowDialog();      // 模态
 73         }
 74 #endregion
 75 
 76 #region  状态栏菜单 : CheckedChanged 事件
 77         /// <summary> 根据状态栏ToolStripMenuItem前面的【√】勾选状态, 设置状态栏控件: statusStrip1的显示状态
 78         ///         1. 处于勾选状态:  显示 statusStrip1 (statusStrip1.Visible = true;)
 79         ///         2. 处于未勾选状态:  隐藏  statusStrip1 (statusStrip1.Visible = false;)   
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 83         private void 状态栏ToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
 84         {
 85             // 状态栏ToolStripMenuItem.Checked时, 返回true, 用于设置statusStrip1的Visible状态
 86             statusStrip1.Visible = 状态栏ToolStripMenuItem.Checked;
 87         }
 88 #endregion
 89 
 90 #region 工具栏菜单
 91 
 92         /// <summary> 根据工具栏ToolStripMenuItem前面的【√】勾选状态, , 设置状态栏控件: toolStrip1的显示状态
 93         ///         1. 处于勾选状态:  显示 toolStrip1 (toolStrip1.Visible = true;)
 94         ///         2. 处于未勾选状态:  隐藏  toolStrip1 (toolStrip1.Visible = false;)
 95         /// </summary>
 96         /// <param name="sender"></param>
 97         /// <param name="e"></param>
 98         private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
 99         {
100             // 工具栏ToolStripMenuItem.Checked时, 返回true, 用于设置statusStrip1的Visible状态
101             toolStrip1.Visible = 工具栏ToolStripMenuItem.Checked;
102         }
103 
104 #endregion
105 
106         private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
107         {
108             // 判断listBox1当前选中状态:  未选中 -1 
109             if ( -1 == listBox1.SelectedIndex )
110             {
111                 // 不显示上下文菜单(弹出菜单)
112                 //e.Cancel = true;
113 
114                 // 禁用上下文菜单
115                 添加ToolStripMenuItem1.Enabled = false;
116                 编辑ToolStripMenuItem1.Enabled = false;
117                 删除ToolStripMenuItem1.Enabled = false;
118 
119             }
120             else
121             {
122                 添加ToolStripMenuItem1.Enabled = true;
123                 编辑ToolStripMenuItem1.Enabled = true;
124                 删除ToolStripMenuItem1.Enabled = true;
125             }
126         }
127 
128         private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
129         {
130             string steel = "是否删除: " + listBox1.SelectedItem.ToString();
131             if (DialogResult.Yes == MessageBox.Show(steel,"提示: ",MessageBoxButtons.YesNo))
132             {
133                 listBox1.Items.RemoveAt(listBox1.SelectedIndex);
134             }
135         }
136     }
137 }

posted @ 2016-04-09 16:21  C/C++/Python/Java  阅读(596)  评论(0编辑  收藏  举报