Get Source Control Of DropDownMenu












1














I have two click events of menu items in a context menu strip.
I can get the source control of the clicked context menu item by doing this code:



Control c = ((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl;


Screenshot of the context menu item



But when I use this code on a context menu item that is in another level it returns null.



Screenshot of the context menu item in another level



How can I get the sourcecontrol in the click event of the second screenshot's menu item?










share|improve this question






















  • It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
    – Jimi
    Nov 12 at 12:09












  • This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
    – jmcilhinney
    Nov 12 at 13:11










  • Jimi please post your answer as a answer so I can mark it
    – GoldErzYT GoldErzYT
    Nov 12 at 13:12
















1














I have two click events of menu items in a context menu strip.
I can get the source control of the clicked context menu item by doing this code:



Control c = ((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl;


Screenshot of the context menu item



But when I use this code on a context menu item that is in another level it returns null.



Screenshot of the context menu item in another level



How can I get the sourcecontrol in the click event of the second screenshot's menu item?










share|improve this question






















  • It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
    – Jimi
    Nov 12 at 12:09












  • This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
    – jmcilhinney
    Nov 12 at 13:11










  • Jimi please post your answer as a answer so I can mark it
    – GoldErzYT GoldErzYT
    Nov 12 at 13:12














1












1








1







I have two click events of menu items in a context menu strip.
I can get the source control of the clicked context menu item by doing this code:



Control c = ((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl;


Screenshot of the context menu item



But when I use this code on a context menu item that is in another level it returns null.



Screenshot of the context menu item in another level



How can I get the sourcecontrol in the click event of the second screenshot's menu item?










share|improve this question













I have two click events of menu items in a context menu strip.
I can get the source control of the clicked context menu item by doing this code:



Control c = ((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl;


Screenshot of the context menu item



But when I use this code on a context menu item that is in another level it returns null.



Screenshot of the context menu item in another level



How can I get the sourcecontrol in the click event of the second screenshot's menu item?







c# .net vb.net winforms contextmenustrip






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 11:36









GoldErzYT GoldErzYT

104




104












  • It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
    – Jimi
    Nov 12 at 12:09












  • This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
    – jmcilhinney
    Nov 12 at 13:11










  • Jimi please post your answer as a answer so I can mark it
    – GoldErzYT GoldErzYT
    Nov 12 at 13:12


















  • It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
    – Jimi
    Nov 12 at 12:09












  • This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
    – jmcilhinney
    Nov 12 at 13:11










  • Jimi please post your answer as a answer so I can mark it
    – GoldErzYT GoldErzYT
    Nov 12 at 13:12
















It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
– Jimi
Nov 12 at 12:09






It' probably simpler to have a Field (something like Control CurrentContextMenuOwner) that is set when a ContextMenu is opened. Subscribe the Opened() event and set CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of the ToolStripMenuItems you can then use the CurrentContextMenuOwner reference to access a Control's properties. Set CurrentContextMenuOwner to null in the Closing() or Closed() events of the ContextMenu.
– Jimi
Nov 12 at 12:09














This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
– jmcilhinney
Nov 12 at 13:11




This is a bug in the ContextMenuStrip that has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inherit ContextMenuStrip and do it internally if you wanted to but, if it's a one-off, a field is the easiest option.
– jmcilhinney
Nov 12 at 13:11












Jimi please post your answer as a answer so I can mark it
– GoldErzYT GoldErzYT
Nov 12 at 13:12




Jimi please post your answer as a answer so I can mark it
– GoldErzYT GoldErzYT
Nov 12 at 13:12












1 Answer
1






active

oldest

votes


















1














The ContextMenuStrip SourceControl (the reference of the current Control where the Context Menu is activated) can be retrieved, from a ToolStripMenuItem, inspecting the OwnerItem reference and moving upstream until the OwnerItem reference is null, then inspecting the Owner value, which references the ContextMenuStrip.

(Unfortunately, the SourceControl reference is only available in the ContextMenuStrip control).



A simple alternative method is using a Field that references the Control where the current ContextMenuStrip has been activated (you can have just one active ContextMenuStrip).

This Field reference, set when the ContextMenuStrip is opened - by subscribing to the Opened() event - can then be accessed by any of the ToolStripMenuItem.

The Field reference is then set back to null when then ContextMenuStrip is closed.



An example:

(toolStripMenuItem is a generic name, it must be set to an actual control name).



Control CurrentContextMenuOwner = null;

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;
}

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentContextMenuOwner.BackColor = Color.Blue;
//(...)
}

private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
CurrentContextMenuOwner = null;
}





share|improve this answer























  • Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
    – GoldErzYT GoldErzYT
    Nov 12 at 16:24










  • Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
    – Jimi
    Nov 12 at 16:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261340%2fget-source-control-of-dropdownmenu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The ContextMenuStrip SourceControl (the reference of the current Control where the Context Menu is activated) can be retrieved, from a ToolStripMenuItem, inspecting the OwnerItem reference and moving upstream until the OwnerItem reference is null, then inspecting the Owner value, which references the ContextMenuStrip.

(Unfortunately, the SourceControl reference is only available in the ContextMenuStrip control).



A simple alternative method is using a Field that references the Control where the current ContextMenuStrip has been activated (you can have just one active ContextMenuStrip).

This Field reference, set when the ContextMenuStrip is opened - by subscribing to the Opened() event - can then be accessed by any of the ToolStripMenuItem.

The Field reference is then set back to null when then ContextMenuStrip is closed.



An example:

(toolStripMenuItem is a generic name, it must be set to an actual control name).



Control CurrentContextMenuOwner = null;

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;
}

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentContextMenuOwner.BackColor = Color.Blue;
//(...)
}

private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
CurrentContextMenuOwner = null;
}





share|improve this answer























  • Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
    – GoldErzYT GoldErzYT
    Nov 12 at 16:24










  • Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
    – Jimi
    Nov 12 at 16:29


















1














The ContextMenuStrip SourceControl (the reference of the current Control where the Context Menu is activated) can be retrieved, from a ToolStripMenuItem, inspecting the OwnerItem reference and moving upstream until the OwnerItem reference is null, then inspecting the Owner value, which references the ContextMenuStrip.

(Unfortunately, the SourceControl reference is only available in the ContextMenuStrip control).



A simple alternative method is using a Field that references the Control where the current ContextMenuStrip has been activated (you can have just one active ContextMenuStrip).

This Field reference, set when the ContextMenuStrip is opened - by subscribing to the Opened() event - can then be accessed by any of the ToolStripMenuItem.

The Field reference is then set back to null when then ContextMenuStrip is closed.



An example:

(toolStripMenuItem is a generic name, it must be set to an actual control name).



Control CurrentContextMenuOwner = null;

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;
}

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentContextMenuOwner.BackColor = Color.Blue;
//(...)
}

private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
CurrentContextMenuOwner = null;
}





share|improve this answer























  • Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
    – GoldErzYT GoldErzYT
    Nov 12 at 16:24










  • Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
    – Jimi
    Nov 12 at 16:29
















1












1








1






The ContextMenuStrip SourceControl (the reference of the current Control where the Context Menu is activated) can be retrieved, from a ToolStripMenuItem, inspecting the OwnerItem reference and moving upstream until the OwnerItem reference is null, then inspecting the Owner value, which references the ContextMenuStrip.

(Unfortunately, the SourceControl reference is only available in the ContextMenuStrip control).



A simple alternative method is using a Field that references the Control where the current ContextMenuStrip has been activated (you can have just one active ContextMenuStrip).

This Field reference, set when the ContextMenuStrip is opened - by subscribing to the Opened() event - can then be accessed by any of the ToolStripMenuItem.

The Field reference is then set back to null when then ContextMenuStrip is closed.



An example:

(toolStripMenuItem is a generic name, it must be set to an actual control name).



Control CurrentContextMenuOwner = null;

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;
}

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentContextMenuOwner.BackColor = Color.Blue;
//(...)
}

private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
CurrentContextMenuOwner = null;
}





share|improve this answer














The ContextMenuStrip SourceControl (the reference of the current Control where the Context Menu is activated) can be retrieved, from a ToolStripMenuItem, inspecting the OwnerItem reference and moving upstream until the OwnerItem reference is null, then inspecting the Owner value, which references the ContextMenuStrip.

(Unfortunately, the SourceControl reference is only available in the ContextMenuStrip control).



A simple alternative method is using a Field that references the Control where the current ContextMenuStrip has been activated (you can have just one active ContextMenuStrip).

This Field reference, set when the ContextMenuStrip is opened - by subscribing to the Opened() event - can then be accessed by any of the ToolStripMenuItem.

The Field reference is then set back to null when then ContextMenuStrip is closed.



An example:

(toolStripMenuItem is a generic name, it must be set to an actual control name).



Control CurrentContextMenuOwner = null;

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
CurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;
}

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentContextMenuOwner.BackColor = Color.Blue;
//(...)
}

private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
CurrentContextMenuOwner = null;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 17:44

























answered Nov 12 at 13:57









Jimi

6,56931533




6,56931533












  • Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
    – GoldErzYT GoldErzYT
    Nov 12 at 16:24










  • Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
    – Jimi
    Nov 12 at 16:29




















  • Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
    – GoldErzYT GoldErzYT
    Nov 12 at 16:24










  • Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
    – Jimi
    Nov 12 at 16:29


















Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
– GoldErzYT GoldErzYT
Nov 12 at 16:24




Sorry for asking here but how can I get the opposite color of a color? When I have white, black is the opposite and so on...
– GoldErzYT GoldErzYT
Nov 12 at 16:24












Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
– Jimi
Nov 12 at 16:29






Color OppositeColor = Color.FromArgb(Color.White.ToArgb() ^ 0xffffff); Simplified. The opposite color depends on color domain. It could be much more complex to calculate, depending on the destination.
– Jimi
Nov 12 at 16:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261340%2fget-source-control-of-dropdownmenu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly