Ich habe eine ContextMenuStrip
Setup mit zwei ToolStripItem
s. Die zweite ToolStripItem
hat zwei weitere verschachtelte ToolStripItem
s. Ich definiere dies als:ContextMenuStrip.Owner Eigenschaft null beim Abrufen von Nested ToolStripMenuItem
ContextMenuStrip cms = new ContextMenuStrip();
ToolStripMenuItem contextJumpTo = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmap = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmapStart = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmapLast = new ToolStripMenuItem();
cms.Items.AddRange(new ToolStripItem[] { contextJumpTo,
contextJumpToHeatmap});
cms.Size = new System.Drawing.Size(176, 148);
contextJumpTo.Size = new System.Drawing.Size(175, 22);
contextJumpTo.Text = "Jump To (No Heatmapping)";
contextJumpToHeatmap.Size = new System.Drawing.Size(175, 22);
contextJumpToHeatmap.Text = "Jump To (With Heatmapping)";
contextJumpToHeatmap.DropDownItems.AddRange(new ToolStripItem[] { contextJumpToHeatmapStart,
contextJumpToHeatmapLast });
contextJumpToHeatmapStart.Size = new System.Drawing.Size(165, 22);
contextJumpToHeatmapStart.Text = "From Start of File";
contextJumpToHeatmapLast.Size = new System.Drawing.Size(165, 22);
contextJumpToHeatmapLast.Text = "From Last Data Point";
ich dann Setup ein Ereignis-Listener für die Click-Ereignisse der drei ToolStripMenuItem
s, die ich reagieren soll. Hier sind die Verfahren (I aufgeführt sind nur zwei der drei Methoden):
void contextJumpTo_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
DataGridView dgv = owner.SourceControl as DataGridView;
if (dgv != null)
// DO WORK
}
}
}
void contextJumpToHeatmapStart_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
// Retrieve the ToolStripItem that owns this ToolStripItem
ToolStripMenuItem ownerItem = menuItem.OwnerItem as ToolStripMenuItem;
if (ownerItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = ownerItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
DataGridView dgv = owner.SourceControl as DataGridView;
if (dgv != null)
// DO WORK
}
}
}
}
hier das Problem ist, ich habe:
Meine contextJumpTo_Click
Methode perfekt funktioniert gut. Wir gehen den ganzen Weg bis dahin wo ich feststelle, welcher DataGridView
der Klick kam und ich kann fortfahren. Die contextJumpTo
ToolStripMenuItem
ist jedoch kein verschachtelter Menüpunkt auf der ContextMenuStrip
.
Aber meine Methode für contextJumpToHeatmapStart_Click
funktioniert nicht richtig. Wenn ich zu der Zeile komme, wo ich owner.SourceControl
feststelle, ist SourceControl
null und ich kann nicht fortfahren. Jetzt weiß ich, dass diese ToolStripMenuItem
unter einem anderen in meinem ContextMenuStrip
verschachtelt ist, aber warum ist die SourceControl
Eigenschaft plötzlich null auf meinem ContextMenuStrip
?
Wie erhalte ich die SourceControl
für eine verschachtelte ToolStripMenuItem
für eine ContextMenuStrip
?
ich auf die angesiedelt haben gleiche Schlussfolgerung. Ein bisschen ärgerlich, um eine Eigenschaft für etwas zu erstellen, wo diese Eigenschaft bereits existiert. Aber ich kann nicht viel tun. –
kam auch zu dieser Schlussfolgerung kurz bevor diese Antwort zu sehen ... aber im Rückblick denke ich, es ist eigentlich besser, es auf diese Weise zu tun. – BVernon
Ich bekomme auf diese Weise auch null. –