做过:演练:在 Windows 窗体上创建动态上下文菜单

王朝system·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

若要节省创建应用程序所需的时间并减少代码量,可以让多个控件共享单个上下文菜单对象。利用一个只包含该控件必需菜单项的“动态”上下文菜单(或快捷方式菜单),可以减少应用程序中控件所需的上下文菜单总数。以下演练显示如何更改每个控件的菜单项。

创建应用程序

以下步骤将创建一个 Windows 应用程序,它具有包含两个控件的窗体。在运行时,如果右击每个控件(只要它具有焦点,即被选定),将显示相应的上下文菜单。RadioButton 控件的上下文菜单将包含两个项;CheckBox 控件的上下文菜单将包含三个项。

在 Windows 窗体上创建动态上下文菜单

创建新的 Windows 应用程序。有关详细信息,请参见创建 Windows 应用程序项目

将 CheckBox 控件和 RadioButton 控件从“工具箱”拖到窗体上。

虽然任何两个(或更多个)控件都可以共享一个上下文菜单,但使具有类似命令的控件共享上下文菜单也是有好处的,因为这样可以减少必需动态显示及隐藏的量。

双击“工具箱”中的“ContextMenu”组件,将其添加到窗体中。它将成为共享的上下文菜单。

在“属性”窗口中,将 CheckBox 控件和 RadioButton 控件的 ContextMenu 属性设置为 ContextMenu1(在 Visual Basic 中)或 contextMenu1(在 Visual C# 或 Visual C++ 中)。

在“属性”窗口中,将 CheckBox 控件的 ThreeState 属性设置为 true。

注意 您还可以完全使用代码生成 ContextMenu 和 ThreeState 属性的菜单项和设置。有关详细信息,请参见向 Windows 窗体添加上下文菜单和设置控件、文档和窗体的属性。有关 ThreeState 属性的详细信息,请参见 Windows 窗体 CheckBox 控件介绍

从设计器中双击 ContextMenu 组件,为该组件的 Popup 事件创建默认的处理程序。有关详细信息,请参见在 Windows 窗体设计器上创建事件处理程序

在事件处理程序中插入执行以下任务的代码:

添加两个菜单项,一个表示控件的 Checked 状态,另一个表示 Unchecked 状态。

用 If 语句检验 CheckBox 控件是否为窗体上的 SourceControl。根据检验结果,动态地添加第三个菜单项,该菜单项表示控件的 Indeterminate 状态。

以下示例显示如何使用 Add 方法来设置菜单项的 Text 属性以及如何定义与该菜单项相关联的事件处理程序。有关其他具有不同签名的 Add 方法,请参见 Menu.MenuItemCollection.Add 方法。 ' Visual Basic

Protected Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup

' Clear the contents of the context menu.

ContextMenu1.MenuItems.Clear()

' Add a menu item for the Checked state.

ContextMenu1.MenuItems.Add("Checked", New System.EventHandler(AddressOf Me.Checked_OnClick))

' Add a menu item for the Unchecked state.

ContextMenu1.MenuItems.Add("Unchecked", New System.EventHandler(AddressOf Me.Unchecked_OnClick))

' Test which control it is.

' If it is the CheckBox, add a menu item for the Indeterminate state.

If ContextMenu1.SourceControl Is CheckBox1 Then

ContextMenu1.MenuItems.Add("Indeterminate", New System.EventHandler(AddressOf Me.Indeterminate_OnClick))

End If

End Sub

// C#

protected void contextMenu1_Popup(System.Object sender, System.EventArgs e)

{

// Clear the contents of the context menu.

contextMenu1.MenuItems.Clear();

// Add a menu item for the Checked state.

contextMenu1.MenuItems.Add("Checked",new System.EventHandler(this.Checked_OnClick));

// Add a menu item for the Unchecked state.

contextMenu1.MenuItems.Add("Unchecked",new System.EventHandler(this.Unchecked_OnClick));

// Test which control it is.

// If it is the CheckBox, add a menu item for the Indeterminate state.

if (contextMenu1.SourceControl == checkBox1)

{

this.contextMenu1.MenuItems.Add("Indeterminate", new System.EventHandler(this.Indeterminate_OnClick));

}

}

// C++

private:

System::Void contextMenu1_Popup(System::Object * sender,

System::EventArgs * e)

{

// Clear the contents of the context menu.

contextMenu1->MenuItems->Clear();

// Add a menu item for the Checked state.

contextMenu1->MenuItems->Add("Checked",

new System::EventHandler(this, Checked_OnClick));

// Add a menu item for the Unchecked state.

contextMenu1->MenuItems->Add("Unchecked",

new System::EventHandler(this, Unchecked_OnClick));

// Test which control it is.

// If it is the CheckBox, add a menu item

// for the Indeterminate state.

if (contextMenu1->SourceControl == checkBox1)

{

this->contextMenu1->MenuItems->Add("Indeterminate",

new System::EventHandler(this, Indeterminate_OnClick));

}

}

为 MenuItem1 创建一个事件处理程序。添加如下代码,检验窗体的 SourceControl 属性,然后根据检验结果设置 RadioButton 或 CheckBox 控件的 Checked 属性: ' Visual Basic

Protected Sub Checked_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

If ContextMenu1.SourceControl Is RadioButton1 Then

RadioButton1.Checked = True

ElseIf ContextMenu1.SourceControl Is CheckBox1 Then

CheckBox1.Checked = True

End If

End Sub

// C#

protected void Checked_OnClick(System.Object sender, System.EventArgs e)

{

if (contextMenu1.SourceControl == radioButton1)

radioButton1.Checked = true;

else if (contextMenu1.SourceControl == checkBox1)

checkBox1.Checked = true;

}

// C++

private:

System::Void Checked_OnClick(System::Object * sender,

System::EventArgs * e)

{

if (contextMenu1->SourceControl == radioButton1)

radioButton1->Checked = true;

else if (contextMenu1->SourceControl == checkBox1)

checkBox1->Checked = true;

}

注意 此示例使用 CheckState 属性在 Indeterminate_OnClick 事件处理程序中将 CheckBox 控件设置为 Indeterminate。

有关如何在 Visual Basic 中创建事件处理程序的详细信息,请参见在 Visual Basic 代码编辑器中创建事件处理程序。有关如何在 C# 中创建事件处理程序的详细信息,请参见在 Windows 窗体设计器上创建默认事件处理程序。有关如何在 C++ 中创建事件处理程序的详细信息,请参见 Visual C++ 中的事件处理。

为 MenuItem2 创建类似的事件处理程序。为该事件处理程序输入如下代码: ' Visual Basic

Protected Sub Unchecked_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

If ContextMenu1.SourceControl Is RadioButton1 Then

RadioButton1.Checked = False

ElseIf ContextMenu1.SourceControl Is CheckBox1 Then

CheckBox1.Checked = False

End If

End Sub

// C#

protected void Unchecked_OnClick(System.Object sender, System.EventArgs e)

{

if (contextMenu1.SourceControl == radioButton1)

radioButton1.Checked = false;

else if (contextMenu1.SourceControl == checkBox1)

checkBox1.Checked = false;

}

// C++

private:

System::Void Unchecked_OnClick(System::Object * sender,

System::EventArgs * e)

{

if (contextMenu1->SourceControl == radioButton1)

radioButton1->Checked = false;

else if (contextMenu1->SourceControl == checkBox1)

checkBox1->Checked = false;

}

为 MenuItem3 创建类似的事件处理程序。为该事件处理程序输入如下代码,确保将事件命名为 Indeterminate_OnClick: ' Visual Basic

Protected Sub Indeterminate_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

If ContextMenu1.SourceControl Is CheckBox1 Then

CheckBox1.CheckState = System.Windows.Forms.CheckState.Indeterminate

End If

End Sub

// C#

protected void Indeterminate_OnClick(System.Object sender, System.EventArgs e)

{

if (contextMenu1.SourceControl == checkBox1)

checkBox1.CheckState = System.Windows.Forms.CheckState.Indeterminate;

}

// C++

private:

System::Void Indeterminate_OnClick(System::Object * sender,

System::EventArgs * e)

{

if (contextMenu1->SourceControl == checkBox1)

checkBox1->CheckState =

System::Windows::Forms::CheckState::Indeterminate;

}

测试应用程序

此时,请运行该应用程序,并观察在动态添加和移除上下文菜单中的菜单项时,应用程序的行为。

调试该应用程序,然后按 F5 键运行它。有关调试的详细信息,请参见调试基础知识。

单击 RadioButton 控件将其选定,然后右击 RadioButton 显示上下文菜单。

请注意,其中有两个菜单项,它们会将 RadioButton 控件设置为 Checked 或 Unchecked。

单击 CheckBox 控件将其选定,然后右击 CheckBox 显示上下文菜单。

请注意,其中有三个菜单项,它们会将 CheckBox 控件设置为 Checked、Unchecked 或 Indeterminate。

请参见

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航