Get Source Control Of DropDownMenu
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
add a comment |
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
It' probably simpler to have a Field (something likeControl CurrentContextMenuOwner) that is set when aContextMenuis opened. Subscribe theOpened()event and setCurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of theToolStripMenuItemsyou can then use theCurrentContextMenuOwnerreference to access a Control's properties. SetCurrentContextMenuOwnerto null in theClosing()orClosed()events of theContextMenu.
– Jimi
Nov 12 at 12:09
This is a bug in theContextMenuStripthat has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inheritContextMenuStripand 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
add a comment |
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
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
c# .net vb.net winforms contextmenustrip
asked Nov 12 at 11:36
GoldErzYT GoldErzYT
104
104
It' probably simpler to have a Field (something likeControl CurrentContextMenuOwner) that is set when aContextMenuis opened. Subscribe theOpened()event and setCurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of theToolStripMenuItemsyou can then use theCurrentContextMenuOwnerreference to access a Control's properties. SetCurrentContextMenuOwnerto null in theClosing()orClosed()events of theContextMenu.
– Jimi
Nov 12 at 12:09
This is a bug in theContextMenuStripthat has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inheritContextMenuStripand 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
add a comment |
It' probably simpler to have a Field (something likeControl CurrentContextMenuOwner) that is set when aContextMenuis opened. Subscribe theOpened()event and setCurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of theToolStripMenuItemsyou can then use theCurrentContextMenuOwnerreference to access a Control's properties. SetCurrentContextMenuOwnerto null in theClosing()orClosed()events of theContextMenu.
– Jimi
Nov 12 at 12:09
This is a bug in theContextMenuStripthat has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inheritContextMenuStripand 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
add a comment |
1 Answer
1
active
oldest
votes
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;
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
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
add a comment |
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;
}
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
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
It' probably simpler to have a Field (something like
Control CurrentContextMenuOwner) that is set when aContextMenuis opened. Subscribe theOpened()event and setCurrentContextMenuOwner = (sender as ContextMenuStrip).SourceControl;. In any of theToolStripMenuItemsyou can then use theCurrentContextMenuOwnerreference to access a Control's properties. SetCurrentContextMenuOwnerto null in theClosing()orClosed()events of theContextMenu.– Jimi
Nov 12 at 12:09
This is a bug in the
ContextMenuStripthat has been there since the beginning, as far as I'm aware. You basically do have to do what @Jimi suggested. You could probably inheritContextMenuStripand 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