首页 新闻 会员 周边

winform中动态创建控件的contextMenuStrip的使用问题

0
悬赏园豆:40 [已解决问题] 解决于 2011-11-07 13:35

如题,在程序的运行过程中我会动态的在form中添加一些图标,生成图标的时候也顺便给其添加了右键菜单
  PictureBox newpic = new PictureBox();
  newpic.Name = m_dataframe.TagID;
  newpic.Size = new System.Drawing.Size(48, 48);
  newpic.Location = new System.Drawing.Point(posInfo[0], posInfo[1]);
  newpic.Image = (Image)TheOldManCareSystem.Properties.Resources.normalState;
  newpic.BackColor = Color.Transparent;
  //add context menu to do some operate of the mark
  newpic.ContextMenuStrip = this.markContextMenuStrip;
在程序运行过程中我右击已经生成的图标,希望通过选项来改变图标使用的image
我该如何在右键菜单的处理程序(下视函数)中找到我点击的图标呢
  private void alarmStateClear_Click(object sender, EventArgs e)
  {
    
  PictureBox picturebox = (PictureBox)sender;
  //picturebox.Image = (Image)TheOldManCareSystem.Properties.Resources.normalState;
  }
上面操作sender是想当然的写的,错误。应该如何去获得控件的操作权

aerkate的主页 aerkate | 初学一级 | 园豆:74
提问于:2011-11-07 12:17
< >
分享
最佳答案
0
       private void alarmStateClear(object sender, EventArgs e)
{
PictureBox picturebox = this.markContextMenuStrip.SourceControl as PictureBox;
if (picturebox != null)
{
//这里就可以处理你的PictureBox了
}
}
收获园豆:40
LCM | 大侠五级 |园豆:6876 | 2011-11-07 13:25

谢谢 采用了你的方法果然可以了,非常感谢

aerkate | 园豆:74 (初学一级) | 2011-11-07 13:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册