[英]Adding 2 Event Handler in 1 Control
im doing on a context-menu can be found on rightclick like in actual code-editor something like this:我在上下文菜单上做的事情可以在右键单击时找到,就像在实际的代码编辑器中一样:
and im already done with the cut,copy and paste by using this code:我已经使用以下代码完成了剪切、复制和粘贴:
private void rtb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MenuItem[] menuItems = new MenuItem[] {
new MenuItem("Cut", new System.EventHandler(this.CutMenuItemClick)),
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick)),
new MenuItem("Paste", new System.EventHandler(this.PasteMenuItemClick)),
ContextMenu rightcontext = new ContextMenu(menuItems);
int xOffset = Cursor.Position.X - DtexteditoR.ActiveForm.Location.X;
int yOffset = Cursor.Position.Y - DtexteditoR.ActiveForm.Location.Y;
rightcontext.Show(DtexteditoR.ActiveForm, new Point(xOffset, yOffset));
}
}
private void CutMenuItemClick(object sender, EventArgs e)
{
rtb.Cut();
}
private void CopyMenuItemClick(object sender, EventArgs e)
{
rtb.Copy();
}
private void PasteMenuItemClick(object sender, EventArgs e)
{
rtb.Paste();
}
im using winforms with a dynamic control (dont use designer) and my question was on how to make multiple event handler in a control (different handler) something like this:我使用带有动态控件的 winforms (不要使用设计器),我的问题是如何在控件(不同的处理程序)中创建多个事件处理程序,如下所示:
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),
private void MeasureCopy(object obj,
MeasureItemEventArgs miea)
{
MenuItem mi = (MenuItem)obj;
// Get standard menu font so that the text in this
// menu rectangle doesn't look funny with a
// different font
Font menuFont = SystemInformation.MenuFont;
StringFormat strfmt = new StringFormat();
SizeF sizef =
miea.Graphics.MeasureString(mi.Text, menuFont, 1000, strfmt);
// Get image so size can be computed
Bitmap bmMenuImage = new Bitmap(typeof(NewForm), "COPY.BMP");
// Add image height and width to the text height and width when
// drawn with selected font (got that from measurestring method)
// to compute the total height and width needed for the rectangle
miea.ItemWidth = (int)Math.Ceiling(sizef.Width) + bmMenuImage.Width;
miea.ItemHeight = (int)Math.Ceiling(sizef.Height) + bmMenuImage.Height;
}
for me to enable to add image beside "Copy" .让我能够在“复制”旁边添加图像。
how to do this thing:如何做这件事:
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),
the right way .thanks!正确的方法。谢谢!
I would just use我只会用
MenuItem item = new MenuItem("Copy");
item.Click += this.CopyMenuItemClick;
item.Click += this.MeasureCopy;
The MeasureItem
event cannot be set from the constructor, try:无法从构造函数设置MeasureItem
事件,请尝试:
MenuItem item = new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick));
item.MeasureItem += this.MeasureCopy;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.