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

如题,在程序的运行过程中我会动态的在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是想当然的写的,错误。应该如何去获得控件的操作权

扬帆大鱼
浏览 276回答 1
1回答

千万里不及你

private void alarmStateClear(object sender, EventArgs e) { PictureBox picturebox = this.markContextMenuStrip.SourceControl as PictureBox; if (picturebox != null) { //这里就可以处理你的PictureBox了 } }
打开App,查看更多内容
随时随地看视频慕课网APP