📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MenuSample
{
/// <summary>
/// Form1 的摘要说明
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.ContextMenu contextMenu1;
/// <summary>
/// 必需的设计器变量
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.ContextMenu = this.contextMenu1;
this.checkBox1.Location = new System.Drawing.Point(16, 8);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.ThreeState = true;
//
// radioButton1
//
this.radioButton1.ContextMenu = this.contextMenu1;
this.radioButton1.Location = new System.Drawing.Point(144, 8);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 1;
this.radioButton1.Text = "radioButton1";
//
// contextMenu1
//
this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 37);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioButton1,
this.checkBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
// 清除菜单内容
contextMenu1.MenuItems.Clear();
// 增加一个状态为Checked的菜单项
contextMenu1.MenuItems.Add("Checked",new System.EventHandler(this.Checked_OnClick));
// 增加一个状态为Unchecked的菜单项
contextMenu1.MenuItems.Add("Unchecked",new System.EventHandler(this.Unchecked_OnClick));
// 判断是哪个控件
if (contextMenu1.SourceControl == checkBox1)
{
this.contextMenu1.MenuItems.Add("Indeterminate", new System.EventHandler(this.Indeterminate_OnClick));
}
}
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;
}
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;
}
protected void Indeterminate_OnClick(System.Object sender, System.EventArgs e)
{
if (contextMenu1.SourceControl == checkBox1)
checkBox1.CheckState = System.Windows.Forms.CheckState.Indeterminate;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -