I'm using the enclosed code to change the font of a Label. When right clicking the label shows a context menu. Only the click events isn't triggered when clicking a menu item. Nothing happens...
I'm using VS2005 Beta 2. (And new to C#)
--Goos van Beek.
//**************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace GB.VinChecker.Controls
{
class nwLabel : System.Windows.Forms.Label
{
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip ctm = new ContextMenuStrip();
MenuItem itmFont = new MenuItem();
itmFont.Name = "itmFont";
itmFont.Text = "Change Font";
itmFont.Click += delegate {
FontDialog fd = new FontDialog();
fd.Font =Font;
if (fd.ShowDialog() == DialogResult.OK)
{
Font = fd.Font;
}; };
ctm.Items.Add("itmFont");
MenuItem itmColor = new MenuItem();
itmColor.Text = "Change Color";
itmColor.Name = "itmColor";
itmColor.Click += delegate
{
FontDialog fd = new FontDialog();
fd.Font = Font;
if (fd.ShowDialog() == DialogResult.OK)
{
Font = fd.Font;
};
};
ctm.Items.Add("itmColor");
this.ContextMenuStrip = ctm;
}
}
}
}
//*******************************

ContextMenuStrip MenuItem Click event isn't triggered?
Ranjay
When I change the code a bit the following line appears:
throw new Exception("The method or operation is not implemented.");Maybe that explains why it doesn't work.
//***
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
class clsLabel : System.Windows.Forms.Label
{
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip ctm = new ContextMenuStrip();
MenuItem itmFont = new MenuItem();
itmFont.Name = "itmFont";
itmFont.Text = "Change Font";
itmFont.Click += new EventHandler(itmFont_Click);
MenuItem itmColor = new MenuItem();
itmColor.Text = "Change Color";
itmColor.Name = "itmColor";
itmColor.Click += new EventHandler(itmColor_Click);
ctm.Items.Add("itmFont");
ctm.Items.Add("itmColor");
this.ContextMenuStrip = ctm;
}
}
void itmColor_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.Font = Font;
if (fd.ShowDialog() == DialogResult.OK)
{
Font = fd.Font;
}
//throw new Exception("The method or operation is not implemented.");
}
void itmFont_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.Font = Font;
if (fd.ShowDialog() == DialogResult.OK)
{
Font = fd.Font;
}
//throw new Exception("The method or operation is not implemented.");
}
}
}
//*****
Ink Master
you need to put the font change code in the menuitems click event and not in the OnMouseDOwn event of the label..
Also the label object has a property "contextmenu" that will fire the referenced menu automaticallly on a right click
June Shi
--Goos
Jaswant Singh Rana
It's no problem to use the font change code on the click event. What I want is a context menu with more then one menuitems. Not only change the font, but also the color.
Using the referenced context menu to change the font works fine for a control that can have the focus, but not for a label. When I use it that way, the font of the control that has the focus changes, not the font of the label which is clicked.
Goos.