主题:请教contextMenuStrip控件的问题
aiwo4444
[专家分:0] 发布于 2008-04-19 08:23:00
基类中:
virtual 菜单条 获取菜单条()
{
生成菜单项 指定其处理函数OnXXXX
生成菜单项 指定其处理函数OnXXXX2
生成菜单项 指定其处理函数OnXXXX3
return 菜单条;
}
void OnXXXX(object sender, EventArgs e)
{
...
}
子类中:
override 菜单条获取菜单条()
{
base.获取菜单条()
添加生成菜单项a
添加生成菜单项b
添加生成菜单项c
return 菜单条;
}
这个代码怎么写的啊?我是新手,查了两天的资料查不到。
最后更新于:2008-04-19 08:35:00
回复列表 (共4个回复)
沙发
aiwo4444 [专家分:0] 发布于 2008-04-19 08:35:00
哪个高手帮帮忙,急啊。
谢谢了!
板凳
termuser [专家分:380] 发布于 2008-04-26 16:02:00
用不着这么复杂的, 在窗体上放一个contextMenuStrip 控件,在上面编辑子项,双击子项,程序自动会在
代码里添加事件的,你在里面写入详细处理方法就行了
3 楼
aiwo4444 [专家分:0] 发布于 2009-06-30 13:27:00
主要是封装,不然当然是拖控件好。
这是以前做毕业设计时用到的。
下面是写好的代码:
//右键菜单——————————————————————————————
public virtual ContextMenuStrip GetContextMenuStrip()
{
ContextMenuStrip strip = new ContextMenuStrip();
ToolStripMenuItem LineWidth = new ToolStripMenuItem("线宽");
strip.Items.Add(LineWidth + "", null, SetLineWidth);
ToolStripMenuItem LineColor = new ToolStripMenuItem("线色");
strip.Items.Add(LineColor + "", null, SetLineColor);
ToolStripMenuItem LineStyle = new ToolStripMenuItem("线型");
ToolStripMenuItem Dash = new ToolStripMenuItem("虚线");
ToolStripMenuItem DashDot = new ToolStripMenuItem("点划线");
ToolStripMenuItem DashDotDot = new ToolStripMenuItem("点点划线");
ToolStripMenuItem Dot = new ToolStripMenuItem("点线");
ToolStripMenuItem Solid = new ToolStripMenuItem("实线");
ToolStripMenuItem MenuItemDelete = new ToolStripMenuItem("删除");
strip.Items.Add(MenuItemDelete + "", null, SetDelete);
LineStyle.DropDownItems.Add(Dash+"",null,SetDash);
LineStyle.DropDownItems.Add(DashDot+"",null,SetDashDot);
LineStyle.DropDownItems.Add(DashDotDot+"",null,SetDashDotDot);
LineStyle.DropDownItems.Add(Dot+"",null,SetDot);
LineStyle.DropDownItems.Add(Solid+"",null,SetSolid);
strip.Items.Add(LineStyle);
return strip;
}
protected void SetDelete(object sender, System.EventArgs e)
{
graphControl1.Shapes.Remove(this);
graphControl1.Invalidate();
}
protected void SetLineWidth(object sender, System.EventArgs e)
{
PenWidthFrm frm = new PenWidthFrm();
if (frm.ShowDialog() == DialogResult.OK)
{
PenWidth = Convert.ToInt32(frm.outText);
graphControl1.Invalidate();
}
}
protected void SetLineColor(object sender, System.EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
PenColor = colorDialog1.Color;
BrushColor = colorDialog1.Color;
graphControl1.Invalidate();
}
}
protected void SetLineStyle(object sender, System.EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
PenColor = colorDialog1.Color;
graphControl1.Invalidate();
}
}
protected void SetDash(object sender, System.EventArgs e)
{
Style = DashStyle.Dash;
graphControl1.Invalidate();
}
protected void SetDashDot(object sender, System.EventArgs e)
{
Style = DashStyle.DashDot;
graphControl1.Invalidate();
}
protected void SetDashDotDot(object sender, System.EventArgs e)
{
Style = DashStyle.DashDotDot;
graphControl1.Invalidate();
}
protected void SetDot(object sender, System.EventArgs e)
{
Style = DashStyle.Dot;
graphControl1.Invalidate();
}
protected void SetSolid(object sender, System.EventArgs e)
{
Style = DashStyle.Solid;
graphControl1.Invalidate();
}
调用时:
this.ContextMenuStrip = obj.GetContextMenuStrip();
4 楼
aiwo4444 [专家分:0] 发布于 2009-06-30 13:28:00
其实这样做更加灵活,方便。
我来回复