c# - ContextMenuStrip DropDownItem 的 SourceControl 返回 null

标签 c# winforms

我在 WinForm 项目中使用 ContextMenuStrip。我在设计时在其中添加了两项 - 1) 添加焦点和 2) 移除焦点。在运行时,我在“添加焦点”项目中添加了几个下拉项目。问题是,我在尝试检索源代码管理名称时得到 null。

这是我的代码:

ToolStripDropDownItem dropDownItem = sender as ToolStripDropDownItem;
ToolStripMenuItem mainItem = dropDownItem.OwnerItem as ToolStripMenuItem;
ContextMenuStrip contextMenuStrip = mainItem.Owner as ContextMenuStrip;
Control control =contextMenuStrip.SourceControl; //getting null here

这段代码有什么问题?我在这里遗漏了什么吗?

最佳答案

上下文菜单条必须设置为用户控件或窗体的 ContextMenuStrip 属性:)

来源:https://www.codeproject.com/Questions/487557/SourceControlplusofplusContextMenuStripplusDropDow

关于c# - ContextMenuStrip DropDownItem 的 SourceControl 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13178614/

相关文章:

c# - MassTransit RabbitMQ AspNetCore无法启动总线并注册接收端点

c# - 为 API 请求禁用 StatusCodePages 中间件

c# - Web Api 中的 Post 请求中的 Request Body 为 null

c# - 使用 STA 时表单表现异常,线程耗时过长

c# - 有没有办法避免为每个 Func 参数计数使用 Func<T, ...> 参数重载方法

c# - 如何访问 docx 默认样式

.NET Windows 窗体剖析

c# - 如何从 WinForms 迁移到 WPF

c# - 从 WPF 窗口获取 System.Windows.Forms.IWin32Window

c# - 以其他用户身份打开文档