Which NUD called this MenuItem?
-
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 -- -
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 --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 --