menustrip项的可见性未更改 [英] menustrip items visibility is not changing
问题描述
在这里,我将代码设置为对子项目设置可见性
语句正在执行,但可见性未更改.您能告诉我这里的问题是我的代码吗?
here i make the code to set visibility true for sub items
the statement is executing but visibility is not changed can you tell me whats the problem here is my code
public void formm_display_function(string display_form_str)
{
foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
{
foreach (ToolStripMenuItem subitem in menuItem.DropDownItems)
{
if(subitem.Name.Trim() == display_form_str)
{
subitem.Visible = true;
break;
}
}
}
}
[edit]添加了代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]
[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
推荐答案
最明显的原因是,没有与您提供的字符串具有相同名称的项存在:您是否尝试过调试并查看输入和Name属性?
或者,您可以通过控制台执行以下操作:
The most obvious reason is that no item with the same name as the string you are providing exists: Have you tried debugging this and looking at the input, and Name properties?
Or, you could do the same via the console:
public void formm_display_function(string display_form_str)
{
foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
{
Console.WriteLine("> " + menuItem.Name);
foreach (ToolStripMenuItem subitem in menuItem.DropDownItems)
{
Console.WriteLine("> " + subitem.Name);
if (subitem.Name.Trim() == display_form_str)
{
subitem.Visible = true;
break;
}
}
}
}
"现在我像这样了
"now i make it like this
menuStrip1.Items[subitem.Name.Trim()].Enabled = true;
错误出现,对象引用未设置为对象的实例"
而您感到惊讶吗?
假设您的菜单井井有条
the error came object reference not set to an instance of an object"
And you are surprised?
Assuming your menu is organised
Menu.Items
Menu1
submenu11
submenu12
Menu2
submenu21
submenu22
因为您的subItem是menu.Items.DropDownItems的成员,所以它不在menuStrip1.Items中-包含两个名称:"Menu1"和"Menu2"
从整体上看待您的代码:如果对Visible所做的更改正在执行但没有任何效果,则您做错了什么-正如我说的,我尝试了您的代码,它就起作用了...
Since your subItem is a member of the menu.Items.DropDownItems, it isn''t in menuStrip1.Items - that contains two names: "Menu1" and "Menu2"
Look at your code as a whole: You are doing something very wrong if your change to Visible is being executed but not having any effect - as I said, I tried your code and it worked...
您对Visible
的使用是正确的,并且应该始终有效. (然后将其分配给false
,在其他地方,对吗?)问题必须在其他地方,最有可能是在字符串比较subitem.Name.Trim() == display_form_str
中.您如何确定display_form_str
的值正确?在调试器下运行它,然后查看比较操作数.首先,您将检查该代码是否全部运行…:-)
—SA [edit]true
更改为false
-OriginalGriff [/edit]
Your usage ofVisible
is correct and should always work. (And you assign it tofalse
somewhere else, right?) The problem must be somewhere else, most likely in string comparisonsubitem.Name.Trim() == display_form_str
. How can you be sure the value ofdisplay_form_str
is correct? Run it under debugger and look at the operands of comparison. First of all, you will check up if this code is run at all… :-)
—SA [edit]true
changed tofalse
- OriginalGriff[/edit]
而不是:
Instead of:
subitem.Visible = true;
试试:
try:
subitem.Available = true;
请参阅此MSDN页面上的注释,其中描述了.Visible和.Available之间的区别:
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.available.aspx [ ^ ]
See the remarks on this MSDN page, describing the difference between .Visible and .Available:
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.available.aspx[^]
这篇关于menustrip项的可见性未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!