- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
类似这个问题: get contextmenustrip from toolstripmenuitem
除此之外,现在我需要找到在单击 ToolStripMenuItem
时打开其上下文菜单的对象。
SourceControl
、Parent
、Owner
和 GetParentControl()
均返回 null。
最初我有一个 TreeView
,它的 ContextMenuStrip
属性已设置。
这是我的初步代码:
ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
ToolStripMenuItem tsmip = (ToolStripMenuItem)tsmi.OwnerItem;
ContextMenuStrip cms = (ContextMenuStrip)tsmip.GetCurrentParent();
TreeView tv = (TreeView)cms.SourceControl;
// returns null, but not in the cms_openHandler
tv.Nodes.Add(new TreeNode("event fired."));
我是否错误地获取了 ContextMenuStrip
?为什么 cms 的 SourceControl
属性在 OpenHandler 中起作用,但在 ToolStripMenuItem
事件处理程序中不起作用?
Form1.Designer.cs:
namespace TestWFA
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.expandToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.childrenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.allDescendantsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.collapseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.childrenToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.parentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.childrenToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
this.allDescendantsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ContextMenuStrip = this.contextMenuStrip1;
this.treeView1.Location = new System.Drawing.Point(64, 75);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(262, 247);
this.treeView1.TabIndex = 0;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.expandToolStripMenuItem,
this.collapseToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
//
// expandToolStripMenuItem
//
this.expandToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.childrenToolStripMenuItem,
this.allDescendantsToolStripMenuItem});
this.expandToolStripMenuItem.Name = "expandToolStripMenuItem";
this.expandToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.expandToolStripMenuItem.Text = "Expand";
//
// childrenToolStripMenuItem
//
this.childrenToolStripMenuItem.Name = "childrenToolStripMenuItem";
this.childrenToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
this.childrenToolStripMenuItem.Text = "Children";
this.childrenToolStripMenuItem.Click += new System.EventHandler(this.childrenToolStripMenuItem_Click);
//
// allDescendantsToolStripMenuItem
//
this.allDescendantsToolStripMenuItem.Name = "allDescendantsToolStripMenuItem";
this.allDescendantsToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
this.allDescendantsToolStripMenuItem.Text = "All Descendants";
this.allDescendantsToolStripMenuItem.Click += new System.EventHandler(this.allDescendantsToolStripMenuItem_Click);
//
// collapseToolStripMenuItem
//
this.collapseToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.childrenToolStripMenuItem1,
this.parentToolStripMenuItem,
this.childrenToolStripMenuItem2,
this.allDescendantsToolStripMenuItem1});
this.collapseToolStripMenuItem.Name = "collapseToolStripMenuItem";
this.collapseToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.collapseToolStripMenuItem.Text = "Collapse";
//
// childrenToolStripMenuItem1
//
this.childrenToolStripMenuItem1.Name = "childrenToolStripMenuItem1";
this.childrenToolStripMenuItem1.Size = new System.Drawing.Size(161, 22);
this.childrenToolStripMenuItem1.Text = "All Ancestors";
this.childrenToolStripMenuItem1.Click += new System.EventHandler(this.childrenToolStripMenuItem1_Click);
//
// parentToolStripMenuItem
//
this.parentToolStripMenuItem.Name = "parentToolStripMenuItem";
this.parentToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
this.parentToolStripMenuItem.Text = "Parent";
this.parentToolStripMenuItem.Click += new System.EventHandler(this.parentToolStripMenuItem_Click);
//
// childrenToolStripMenuItem2
//
this.childrenToolStripMenuItem2.Name = "childrenToolStripMenuItem2";
this.childrenToolStripMenuItem2.Size = new System.Drawing.Size(161, 22);
this.childrenToolStripMenuItem2.Text = "Children";
this.childrenToolStripMenuItem2.Click += new System.EventHandler(this.childrenToolStripMenuItem2_Click);
//
// allDescendantsToolStripMenuItem1
//
this.allDescendantsToolStripMenuItem1.Name = "allDescendantsToolStripMenuItem1";
this.allDescendantsToolStripMenuItem1.Size = new System.Drawing.Size(161, 22);
this.allDescendantsToolStripMenuItem1.Text = "All Descendants";
this.allDescendantsToolStripMenuItem1.Click += new System.EventHandler(this.allDescendantsToolStripMenuItem1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(406, 365);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem expandToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem childrenToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem allDescendantsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem collapseToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem childrenToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem parentToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem childrenToolStripMenuItem2;
private System.Windows.Forms.ToolStripMenuItem allDescendantsToolStripMenuItem1;
}
}
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestWFA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
treeView1.Nodes.Add(new TreeNode("root"));
}
private void childrenToolStripMenuItem1_Click(object sender, EventArgs e)
{
TreeView tv = GetSourceControl(sender) as TreeView;
if (tv != null)
{
tv.Nodes.Add("Tree event catched!");
}
/*ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
ToolStripMenuItem tsmip = (ToolStripMenuItem)tsmi.OwnerItem;
ContextMenuStrip cms = (ContextMenuStrip)tsmip.GetCurrentParent();
ToolStrip ts = tsmip.GetCurrentParent();
IContainer c = cms.Container;
TreeView tv = (TreeView)c;
tv.Nodes.Add(new TreeNode("event fired."));*/
}
private object GetSourceControl(object Sender)
{
if (Sender as ContextMenuStrip != null)
{
return ContextMenuStrip.SourceControl;
}
var item = Sender as ToolStripItem;
// move to root item
while (item.OwnerItem != null)
{
item = item.OwnerItem;
}
// we have root item now, so lets take ContextMenuStrip object
var menuObject = item.Owner as ContextMenuStrip;
if (menuObject != null)
{
return menuObject.SourceControl;
}
return null;
}
private void parentToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void childrenToolStripMenuItem2_Click(object sender, EventArgs e)
{
}
private void allDescendantsToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void childrenToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void allDescendantsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
try
{
ContextMenuStrip cms = (ContextMenuStrip)sender;
}
catch
{
}
try
{
TreeView tv = (TreeView)sender;
}
catch
{
}
}
}
}
程序.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestWFA
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
不幸的是,我不知道有什么方法可以上传文件。我尝试将它们上传为 img,但没有用。
最佳答案
假设您将 ContextMenuStrip 设置为 TreeView。您可以轻松控制从 ContextMenuStrip
的 Opened
处理程序触发的菜单:
private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
TreeView tv = (sender as ContextMenuStrip).SourceControl as TreeView;
tv.Nodes.Add("Tree event catched!");
}
如果将 ContextMenuStrip 设置为 treeView 的项目,此代码也可以工作。
PS:在实际应用中,您不应该使用像 (sender as ContextMenuStrip).SourceControl as TreeView;
这样的构造,因为可能存在空指针。您应该在每次转换后检查 null。
[编辑]
好的,现在我明白你的目的了。
当您处理菜单项时,sender
对象指向项目触发的事件。要找出弹出上下文菜单的控件,您应该做三件事:
SourceControl
属性中找出有趣的控件我写了一个简单的方法来完成所有的工作:
/// <summary>
/// Gets controls for context menu
/// </summary>
/// <param name="Sender">Sender object from menu event handler</param>
/// <returns></returns>
private object GetSourceControl(Object Sender)
{
// ContextMenuStrip sended?
if (Sender as ContextMenuStrip != null)
{
ContextMenuStrip cms = Sender as ContextMenuStrip;
return cms.SourceControl;
}
var item = Sender as ToolStripItem;
// move to root item
while (item.OwnerItem != null)
{
item = item.OwnerItem;
}
// we have root item now, so lets take ContextMenuStrip object
var menuObject = item.Owner as ContextMenuStrip;
if (menuObject != null)
{
return menuObject.SourceControl;
}
return null;
}
您可以简单地使用它。例如我们有菜单项点击事件处理程序:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
TreeView tv = GetSourceControl(sender) as TreeView;
if (tv != null)
{
tv.Nodes.Add("Tree event catched!");
}
}
[编辑2]
事实证明这个问题很早就被讨论过(SourceControl of ContextMenuStrip is Nothing in ToolStripMenuItem Click?)。单击根项时,SourceControl 属性不为空,但如果单击子项,则它等于空。所以解决方案是将 SourceControl 的值存储在某个地方以备后用。我建议在 Tag
属性中执行此操作。所以所有方法看起来像:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
contextMenuStrip1.Tag = (sender as ContextMenuStrip).SourceControl;
}
....
private object GetSourceControl(object Sender)
{
if (Sender as ContextMenuStrip != null)
{
return ContextMenuStrip.SourceControl;
}
var item = Sender as ToolStripItem;
// move to root item
while (item.OwnerItem != null)
{
item = item.OwnerItem;
}
// we have root item now, so lets take ContextMenuStrip object
var menuObject = item.Owner as ContextMenuStrip;
if (menuObject != null)
{
return menuObject.Tag;
}
return null;
}
在链接的问题中有私有(private)字段的解决方法,但 Tag 属性对我来说是习惯性的
关于c# - 通过 toolstripmenuitem 事件处理程序获取控件(treeView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6323958/
我正在尝试为 ContextMenuStrip 中的 ToolStripMenuItems 设置更大的垂直填充。但是,更改 Padding.Top 属性会在底部而不是顶部添加填充。 我还尝试为 Too
有没有办法让 ToolStripMenuItem 在我单击子控件(在其 DropDrowItems 集合中)时不关闭? 在我的例子中,我有一些用作复选框的 ToolStripMenuItems。实际上
在我的 Windows 应用程序中,我有一个带有网格的上下文菜单,问题是我想根据用户权限禁用上下文菜单中的 ToolStripMenuItem。我该怎么做。我已经这样做了,但它不起作用 private
我已将 menuStrip1 项的可见属性设置为 false 为 foreach (ToolStripMenuItem itm in menuStrip1.Items) { itm.Visib
我有一个很大的实体列表,用户需要能够从中进行选择。我的窗口上有一个 ContextMenuStrip,每个类别的实体都有一些 MenuItems。 在图书馆的例子中,想想“员工”、“可借阅者”、“赞助
我在运行时在该 strip 中有一个 ContextMenuStrip 我正在添加一个 ToolStripMenuItem。我在 XtraGridView 的 MouseDown() 事件处理程序中添
我有一个使用 ContextMenuStrip 的 C# winForm 项目。我根据使用交互动态地将 ToolStripMenuItems 添加到 ContextMenuStrip。当我添加一个新的
sqlstr = "select ModuleName,ModuleId,pagename from modules WHERE ParentID='" + parentID + "' AND VIs
例如标签和文本框。 我试过: class MyClass : ToolStripPanel { //... } 等等。但是然后: contextMenuStrip1.Items.Add(n
我正在使用 MenuStrip有两个主要ToolStripMenuItems ,其中每一个都有其“下拉菜单”,其中包含更多具有 CheckOnClick 的 ToolStripMenuItems属性设
我正在尝试找出是否有调用 ToolStripMenuItem 的方法。 例如,我在返回结果时调用 Web 服务(异步)。我根据结果填充下拉项,(在回调方法中) ToolStripMenuItem.D
我有一个每次打开时都会动态填充的下拉菜单,这是执行此操作的逻辑: private void joysticksToolStripMenuItem_DropDownOpening(object send
我的 menuStrip 中的一个 ToolStripMenuItem 包含我可以选择隐藏/取消隐藏的表的所有列 DropDown每次我想这样做时重新打开下拉菜单(单击该项目)。是否有允许此菜单在从中
我试图让我的上下文菜单保留在屏幕上,即使在我单击其中一个下拉项后(当按下 Shift 键时,尽管我认为这对问题并不重要)。当您单击“开始”>“所有程序”>“附件”> [现在按 Shift 键] 并单击
我正在 MenuStrip 中自定义绘制菜单项。我遇到的问题是菜单项坚持根据文本调整自身大小,这不是我想要的(没有文本)。我可以将 AutoSize 设置为 false 并显式指定大小,但包含菜单 (
在 WinForms .Net 2.0 应用程序中,我想创建一个带有 ToolStripMenuItem 的上下文菜单,该菜单项本身具有标签和文本框。我正在谈论的示例可以在 Access 中找到 -
我有一个主要通过 NotifyIcon 的 ContextMenuStrip 操作的应用程序 ToolStripMenuItems 有多个级别,用户可以通过它们。 问题是,当用户有两个屏幕时,Menu
我有一个带有自定义渲染器的菜单: menuMain.Renderer = new ToolStripProfessionalRenderer(new MenuColors()); 有没有办法在将鼠标移
我有一个带有 MenuStrip 的应用程序,每次我将鼠标悬停在 MenuItem 上时,它都会突出显示为蓝色。 我曾尝试更改 BackColor 和 ForeColor,但这不是问题所在。 有没有办
我有我的 ToolStripMenuItem,当我运行该应用程序时,它看起来像这样: 如您所见,ToolStripMenuItem 左边有一点空白。 如何删除它?我试图编辑每个属性,但它仍然存在...
我是一名优秀的程序员,十分优秀!