Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am opening contextmenustrip on treeview node right click,
but when i press down key on contextmenustrip, selection moves down & at last it is showing blank item,
why it is happening.
Some contextmenustrip item are visible=false.
When all items of contextmenustrip are visible, contextmenustrip works properly,
but if some contextmenustrip item are visible=false it shows blank item at last.

I have snap but i am unble to paste it here...:-(
Posted
Comments
Karthik_Mahalingam 15-Jan-14 5:11am    
post your code...
RhishikeshLathe 15-Jan-14 6:04am    
how can i paste snap here?

In a simple experiment, I am unable to duplicate the behavior you describe.

1. put a TreeView on a Form

2. add some Nodes

3. add a ContextMenuStrip to the Form. Add four items.

4. set the TreeView's ContextMenuStrip Property to the ContextMenuStrip

5. in the NodeMouseClick EventHandler for the TreeView:

C#
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    contextMenuStrip1.Items[0].Visible = (! (e.Node.Name == "Node0")); 
    contextMenuStrip1.Items[2].Visible = (! (e.Node.Name == "Node2")); 
}
I see no blank items when I right-context-click on "Node0" or "Node2," and the up/down arrow keys work as expected.
 
Share this answer
 
Comments
RhishikeshLathe 15-Jan-14 6:03am    
exactly same code, but in my project there is MDI form which contains treeview.
There is lots of other code on form
BillWoodruff 15-Jan-14 7:37am    
MDI is weird ! In general, it is not a good idea to put Controls directly on the MDI Form because of the way they will interact with MDIChildForms. Perhaps you will recall the discussion here:

http://www.codeproject.com/Answers/705138/MDI-form-click-event
RhishikeshLathe 15-Jan-14 8:28am    
thank you sir,
i found it and posted the solution.
Please go through it & if need suggest optimization.
I didn't found why above problem happening in my project:-
But i found following solution on it:-

1.Set following property of ContextMenustrip:-
contextMenuStrip1.AutoSize = false;

2.Register contextMenuStrip Opened event as follows:-
C#
private void contextMenuStrip1_Opened(object sender, EventArgs e)
      {
          int iHeight = 0;
          foreach (ToolStripMenuItem t in contextMenuStrip1.Items)
          {
              if (t.Visible)
                  iHeight += t.Size.Height;
          }
          contextMenuStrip1.Height = iHeight + 5;
      }
 
Share this answer
 

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