開発環境 |
Microsoft Visual Studio Express 2013 for Windows Desktop |
実行環境 |
Windows 8.1 (64bit) |
プロジェクト |
Visual C#/Windows フォーム アプリケーション |
名前 |
ContextMenu |
手順
- Labelを作る。
- ContextMenuStripを作る。
- label1.ContextMenuStripにcontextMenuStrip1をセットする。
Program.cs
using System;
using System.Windows.Forms;
namespace ContextMenu
{
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.cs
using System;
using System.Windows.Forms;
namespace ContextMenu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void countUpToolStripMenuItem_Click(object sender, EventArgs e)
{
var tsmi = (ToolStripMenuItem)sender;
var cms = (ContextMenuStrip)tsmi.Owner;
label1.Text = cms.SourceControl.Name;
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
label1.Text = "";
}
}
}
Form1.Designer.cs
namespace ContextMenu
{
partial class Form1
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.countUpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Window;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.ContextMenuStrip = this.contextMenuStrip1;
this.label1.Location = new System.Drawing.Point(90, 81);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.countUpToolStripMenuItem,
this.clearToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(125, 48);
//
// countUpToolStripMenuItem
//
this.countUpToolStripMenuItem.Name = "countUpToolStripMenuItem";
this.countUpToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.countUpToolStripMenuItem.Text = "count up";
this.countUpToolStripMenuItem.Click += new System.EventHandler(this.countUpToolStripMenuItem_Click);
//
// clearToolStripMenuItem
//
this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
this.clearToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.clearToolStripMenuItem.Text = "clear";
this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem countUpToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem;
}
}
最終更新:2014年09月16日 15:59