本文介绍了ContextMenuStrip的SourceControl DropDownItem返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForm项目中使用ContextMenuStrip。我在设计时添加了两个项目 - 1.添加焦点和2.删除焦点。在运行时,我在添加焦点项中添加了几个下拉项。问题是,我在尝试检索源控件名称时变为null。这是我的代码



I am using a ContextMenuStrip in a WinForm project. I have added two item in it at design time- 1. Add Focus and 2. Remove Focus. In runtime, i am adding few dropdown items in "Add Focus" item. Problem is, i am getting null when trying to retrieve source control name. Here is my code

ToolStripDropDownItem dropDownItem = sender as ToolStripDropDownItem;

ToolStripMenuItem mainItem = dropDownItem.OwnerItem as ToolStripMenuItem;

ContextMenuStrip contextMenuStrip = mainItem.Owner as ContextMenuStrip;

Control control =contextMenuStrip.SourceControl; //getting null here





这段代码有什么问题?



问候

SKPaul



What is the problem in this code?

Regards
SKPaul

推荐答案



这篇关于ContextMenuStrip的SourceControl DropDownItem返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:57