控件为窗体提供标准菜单。
本演练演示了以下任务:
-
创建 Windows 窗体项目。
-
创建标准菜单。
-
控件。
-
处理菜单项的选择。
控件中显示菜单项的选择情况。
如何:向窗体提供标准菜单项。
|
使用设置。 |
若要完成本演练,您需要:
-
足以在安装了 Visual Studio 的计算机上创建和运行 Windows 窗体应用程序项目的权限。
第一步是创建项目并设置窗体。
创建项目
-
创建一个名为“StandardMenuForm”的 Windows 应用程序项目。
如何:创建新的 Windows 窗体应用程序项目。
-
在 Windows 窗体设计器中,选择该窗体。
控件。
创建标准菜单
-
控件拖动到窗体上。
-
“插入标准项”。
控件会用标准菜单项进行填充。
-
“文件”菜单项以查看其默认菜单项和对应的图标。
控件中。
创建 StatusStrip 控件
-
控件拖动到窗体上。
控件自动停靠于窗体的底部。
-
控件中。
事件以在用户选择菜单项时作出响应。
处理菜单项的选择
-
“文件”菜单项。
-
“事件”。
-
事件。
事件生成一个事件处理程序。
-
将下面的代码插入到事件处理程序中。
// This method is the DropDownItemClicked event handler. // It passes the ClickedItem object to a utility method // called UpdateStatus, which updates the text displayed // in the StatusStrip control. private void fileToolStripMenuItem_DropDownItemClicked( object sender, ToolStripItemClickedEventArgs e) { this.UpdateStatus(e.ClickedItem); }
-
实用工具方法的定义插入到窗体中。
// This utility method assigns the value of a ToolStripItem // control's Text property to the Text property of the // ToolStripStatusLabel. private void UpdateStatus(ToolStripItem item) { if (item != null) { string msg = String.Format("{0} selected", item.Text); this.statusStrip1.Items[0].Text = msg; } }
测试窗体
-
按 F5 编译并运行窗体。
-
“文件”菜单项以打开该菜单。
-
“文件”菜单上,单击其中一个菜单项以选择该项。
控件显示了已选择的项。
系列控件有很多其他用途:
-
ContextMenu 组件概述(Windows 窗体)。
-
演练:创建具有菜单合并功能和 ToolStrip 控件的 MDI 窗体。
-
如何:为应用程序设置 ToolStrip 呈现程序。