Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Which NUD called this MenuItem?

Which NUD called this MenuItem?

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    james cxx
    wrote on last edited by
    #1

    I have a ContextMenu with about a dozen MenuItems in it. I've assigned the ContextMenu property of several NumericUpDown controls. When the user right-clicks one of the NumericUpDown controls and then selects a MenuItem from the ContextMenu. This triggers the MenuItem's Click event. Within the MenuItem's event handler, I need to determine which NumericUpDown control called the MenuItem so I can set the Value property of the NumericUpDown to the appropriate value. The problem is, I cannot seem to make this happen. The SourceControl of the ContextMenu has a type of "UpDownBase+UpDownEdit". I've found UpDownBase in the docs, but I get casting errors whenever I try to get to the value of the NumericUpDown. Such as... private void miProg010_Click(object sender, System.EventArgs e) { MenuItem mi = (MenuItem) sender; ContextMenu cm = mi.GetContextMenu(); Control c = (Control) cm.SourceControl; UpDownBase udb = (UpDownBase) c; } Any ideas? -- James --

    J 1 Reply Last reply
    0
    • J james cxx

      I have a ContextMenu with about a dozen MenuItems in it. I've assigned the ContextMenu property of several NumericUpDown controls. When the user right-clicks one of the NumericUpDown controls and then selects a MenuItem from the ContextMenu. This triggers the MenuItem's Click event. Within the MenuItem's event handler, I need to determine which NumericUpDown control called the MenuItem so I can set the Value property of the NumericUpDown to the appropriate value. The problem is, I cannot seem to make this happen. The SourceControl of the ContextMenu has a type of "UpDownBase+UpDownEdit". I've found UpDownBase in the docs, but I get casting errors whenever I try to get to the value of the NumericUpDown. Such as... private void miProg010_Click(object sender, System.EventArgs e) { MenuItem mi = (MenuItem) sender; ContextMenu cm = mi.GetContextMenu(); Control c = (Control) cm.SourceControl; UpDownBase udb = (UpDownBase) c; } Any ideas? -- James --

      J Offline
      J Offline
      james cxx
      wrote on last edited by
      #2

      So I kept working on it and discovered the following: The NumericUpDown is a contol composed of other controls. If the user pulls up the context menu by right-clicking the TextBox area of the NumericUpDown, the the ContextMenu's SourceControl can be cast to a TextBox, or you can get the parent of the TextBox which will be the NumericUpDown I was originally seeking. If the user right-clicks the spinner portion, the ContextMenu's SourceContol is the NumericUpDown directly. So the code that does what I was asking is here: private void miProg010_Click(object sender, System.EventArgs e) { MenuItem mi = (MenuItem) sender; ContextMenu cm = mi.GetContextMenu(); NumericUpDown nud = null; if (cm.SourceControl.GetType() != typeof(System.Windows.Forms.NumericUpDown)) nud = (NumericUpDown) cm.SourceControl.Parent; else nud = (NumericUpDown) cm.SourceControl; nud.Value = 10; } Hopefully this saves someone out there some grief. -- James --

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups