Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi to all!

Here I create a code to check the menuitems

C#
private void formm_display_function(string display_form_str)
       {
       foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
       {
           if (menuItem.Text.Trim() == display_form_str){
           menuItem.Visible = true;
           break;
       }
   }


In this code only the first menu is checking

I have five items

display_form_str contains one of the items from menu

but its checking only the first menu items
Posted
Updated 6-Aug-11 19:01pm
v2
Comments
JOAT-MON 7-Aug-11 1:02am    
Added code block.
Sergey Alexandrovich Kryukov 7-Aug-11 13:58pm    
Re-post! I already answered the previous question. Please don't re-post. Use "Improve question", post comments on existing solutions.
--SA

1 solution

Looks like you only have half of the curly braces showing, this would indicate that the break; command is being hit during every pass. Make sure you encapsulate the entire statement:
C#
private void formm_display_function(string display_form_str)
{
       foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
       {
           if (menuItem.Text.Trim() == display_form_str)
           {
               menuItem.Visible = true;
               break;
           } // <-- added to encapsulate 'break;' command so it only fires when a match is made
       }
}


[EDIT]

ToolStripMenuItems are derived from ToolStripDropDownItem, so if the menu items you are trying to compare against are in the same menu drop down (meaning they show vertically), then what you have is one ToolStripMenuItem containing the other ToolStripMenuItems. To access these, you need to iterate through the ToolStripMenuItems in the DropDownItems property.

C#
private void formm_display_function(string display_form_str)
{
       foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
       {
           foreach(ToolStripMenuItem subitem in menuitem.DropDownItems)
           {
                if (subitem.Text.Trim() == display_form_str)
                {
                     subitem.Visible = true;
                     break;
                }
           }
       }
}


[/EDIT]
 
Share this answer
 
v4
Comments
kami124 7-Aug-11 1:17am    
what you have update you write the same code which i wrote
i think you did not understand my question
see again

i have menustrip1
having items
item1
item2
item3
item4

and in dispaly_form_str has
item1 or item2 or item3 or item4
JOAT-MON 7-Aug-11 1:41am    
I understand your problem, maybe you misunderstood my solution? Notice that I have added an ending curly brace in there. It looked to me like you were missing this in your code, which would cause the foreach loop to break out of its iteration after the first comparison, whether it matched or not.
kami124 7-Aug-11 1:48am    
ok that currly bracket i already make it

the problem is some where else
JOAT-MON 7-Aug-11 2:08am    
Are your menu items in the same drop down menu? Do they go down like this:
item1
item2
item3
item4
item5

or are they across the MenuStrip like this:
item1 item2 item3 item4 item5
JOAT-MON 7-Aug-11 2:27am    
If they are vertical, then see my [EDIT] section above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900