C# 如何在使用ToolStripMenuItem所有者项时实现空异常
我正在尝试实现一些代码,通过这些代码,我可以将单击的toolstipmenuitem的父项一直带到根。如果我公开查询,我最终会从最后一个父级之后的异常中得到一个空异常,正如您所预期的,在没有父级到父级的情况下,可以得到以下的父级: ToolStripMenuItem miOwnerItem=(ToolStripMenuItem)(mi.GetCurrentParent()作为ToolStripDropDown) 我在下面尝试实现了一个try-catch,但没有成功,我想知道是否有人知道在查询OwnerItems时处理这个null异常的最佳方法C# 如何在使用ToolStripMenuItem所有者项时实现空异常,c#,exception,C#,Exception,我正在尝试实现一些代码,通过这些代码,我可以将单击的toolstipmenuitem的父项一直带到根。如果我公开查询,我最终会从最后一个父级之后的异常中得到一个空异常,正如您所预期的,在没有父级到父级的情况下,可以得到以下的父级: ToolStripMenuItem miOwnerItem=(ToolStripMenuItem)(mi.GetCurrentParent()作为ToolStripDropDown) 我在下面尝试实现了一个try-catch,但没有成功,我想知道是否有人知道在查询Ow
private void MenuClick2(object sender, EventArgs e)
{
//Click event begins by setting up variables and bringing in the sender as a menu item
MenuResultsString.Visible = false;
MenuResultsString.Text = "";
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
string WhatClicked = mi.ToString();
ToolStripMenuItem miOwnerItem = (ToolStripMenuItem)(mi.GetCurrentParent() as ToolStripDropDown).OwnerItem;
string WhatClicked1up = miOwnerItem.ToString();
string WhatClicked2up = "";
string WhatClicked3up = "";
string WhatClicked4up = "";
string WhatClicked5up = "";
float howDeep = 1;
try
{
ToolStripMenuItem miGrandpapaOwnerItem = (ToolStripMenuItem)(miOwnerItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
WhatClicked2up = miGrandpapaOwnerItem.ToString();
howDeep = 2;
try
{
ToolStripMenuItem miGreatGrandpapaOwnerItem = (ToolStripMenuItem)(miGrandpapaOwnerItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
WhatClicked3up = miGreatGrandpapaOwnerItem.ToString();
howDeep = 3;
CSMorWhut = IsThisTheSystemMenu(WhatClicked3up);
try
{
ToolStripMenuItem miGreatestGrandpapaOwnerItem = (ToolStripMenuItem)(miGreatGrandpapaOwnerItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
WhatClicked4up = miGreatestGrandpapaOwnerItem.ToString();
howDeep = 4;
try
{
ToolStripMenuItem miAncestralGrandpapaOwnerItem = (ToolStripMenuItem)(miGreatestGrandpapaOwnerItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
WhatClicked4up = miGreatestGrandpapaOwnerItem.ToString();
howDeep = 5;
}
catch (Exception f)
{
}
}
catch (Exception f)
{
}
}
catch (Exception f)
{
}
}
catch (Exception f)
{
}
}
以下是我的例外情况副本:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Solution
StackTrace:
at Solution.App.MenuClick2(Object sender, EventArgs e) in E:\C02\Development\Visual Studio 2019\Source\Repos\Solution\App.cs:line 523
任何指导都将不胜感激。您可以用一个循环来替换所有这些,以获得一个
列表
包含单击项的所有者项
分支,并在属性返回null时中断。项目的索引是级别(深度)
private void MenuClick2(对象发送方,事件参数e)
{
var tsmi=发送方作为ToolStripMenuItem;
var tsmiOwnerItem=tsmi.OwnerItem;
var tsmItems=新列表();
tsmItems.Add(tsmi);
while(tsmiOwnerItem!=null)
{
tsmItems.Insert(0,(ToolStripMenuItem)tsmiOwnerItem);
tsmiOwnerItem=tsmiOwnerItem.OwnerItem;
}
对于(变量i=0;i
注意,这里不需要异常处理程序
旁注
尽可能使用通用异常。如果希望取消引用null
对象引用,则捕获。阅读更多
float howDeep=1代码>。例如,不可能有级别1.5
。因此,请改用整数值类型。i、 e.int howDeep=1代码>
您可以先检查null(if(miOwnerItem==null)
…),然后决定在出现null的情况下该怎么办。我遇到的问题是以下生成方式:ToolStripMenuItem MigreatestGrandPaowneritem=(ToolStripMenuItem)(MigreateGrandPaowneritem.GetCurrentParent()作为ToolStripDropDown)。OwnerItem;上面的migreatedGrandPaOwnerItem不会为null,但是形成migreatestGrandPaowerItem将失败,例如,如果我们使用VS作为示例,如果我选择了File>Exit。第一个是Exit,第二个是File,第三个是失败的,上面有一个异常,因为我的问题是,由于另一个函数中的字符串匹配,它过早地完成了它的过程,我在另一个函数中添加了另一个参数,并对刚刚调用它的函数进行了子字符串检查,因此本质上与您所说的相同,但是在抛出异常的函数之前使用逻辑检查。谢谢你的回复。