473,471 Members | 4,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic add to context menu only works 1st time??

I am dynamically appending to a context menu when user right mouse clicks on
a treeView. The first time I right-mouse click I see the appended menu.
Subsequently, the menu items I append do not display.

this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem );

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton
Nov 15 '05 #1
6 2561
I think they do display but not every time. ;-)
Saying that the rest of your code is correct,
the only problem you have is that you used

contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, new System.Drawing.Point(e.X, e.Y));

With this call you have associated the contextMenu with the form and not with the treeView Control.
Hope this helps! Greetings,
timtos.

"Ed Sutton" <no****************@nomadics.com> wrote in message news:Ol**************@TK2MSFTNGP12.phx.gbl...
I am dynamically appending to a context menu when user right mouse clicks on
a treeView. The first time I right-mouse click I see the appended menu.
Subsequently, the menu items I append do not display.

this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem );

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton

Nov 15 '05 #2
timtos,

Thank you very much for looking at this.

I have the same problem after changing the code as you suggested. The menu
displays every time I right-mosue click, but only the first time displays
the dynamically created menu.

contextMenu1.Show(this.treeView1, new System.Drawing.Point(e.X, e.Y)).

I will go beat my head on the wall some more...

Thank you very much for your suggestion,

-Ed
I think they do display but not every time. ;-)
Saying that the rest of your code is correct,
the only problem you have is that you used

contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, new System.Drawing.Point(e.X, e.Y));

With this call you have associated the contextMenu with the form and
not with the treeView Control.
Hope this helps! Greetings,
timtos.

"Ed Sutton" <no****************@nomadics.com> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...
I am dynamically appending to a context menu when user right mouse
clicks on
a treeView. The first time I right-mouse click I see the appended
menu.
Subsequently, the menu items I append do not display.

this.contextMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton

Nov 15 '05 #3
You still have the same problem?
That is nearly not possible. I´ve tried it out myself now and for me your source is working!
Have you associated the contextMenu with the treeView?

Greetings,
timtos.

"Ed Sutton" <no****************@nomadics.com> wrote in message news:Oq**************@TK2MSFTNGP09.phx.gbl...
timtos,

Thank you very much for looking at this.

I have the same problem after changing the code as you suggested. The menu
displays every time I right-mosue click, but only the first time displays
the dynamically created menu.

contextMenu1.Show(this.treeView1, new System.Drawing.Point(e.X, e.Y)).

I will go beat my head on the wall some more...

Thank you very much for your suggestion,

-Ed
I think they do display but not every time. ;-)
Saying that the rest of your code is correct,
the only problem you have is that you used

contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, new System.Drawing.Point(e.X, e.Y));

With this call you have associated the contextMenu with the form and
not with the treeView Control.
Hope this helps! Greetings,
timtos.

"Ed Sutton" <no****************@nomadics.com> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...
I am dynamically appending to a context menu when user right mouse
clicks on
a treeView. The first time I right-mouse click I see the appended
menu.
Subsequently, the menu items I append do not display.

this.contextMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton


Nov 15 '05 #4
> You still have the same problem?
That is nearly not possible. I´ve tried it out myself now and for me
your source is working!
Have you associated the contextMenu with the treeView?


Not intentionally. I simply drag and dropped a contextMenu on my main form.

How do you associated a contextMenu a treeView control?

I pasted all code from my main form class below.

-Ed

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem mnuParent;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.mnuParent = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(48, 32);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(184, 224);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.treeVi ew1_MouseDown);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuParent});
//
// mnuParent
//
this.mnuParent.Index = 0;
this.mnuParent.Text = "Append Here";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeView1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
System.Diagnostics.Debug.WriteLine("Build menu..");
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
// Create a list of logger versions to export
string strMenuText = "Version " + (iNumMenuItem);
MenuItem mnu = new MenuItem(strMenuText);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(treeView1, new System.Drawing.Point(e.X, e.Y));
}
}
}
}
Nov 15 '05 #5

"Ed Sutton" <no****************@nomadics.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
How do you associate a contextMenu a treeView control?


You can do it with visual studio. Just look for the property ContextMenu
in the TreeView properties! There is a combobox, where you can select
your contextMenu.

Or do it in the code - like this:
this.treeView.ContextMenu = this.contextMenu1;

Hope this finally helps ;-)
Greetings,
timtos.
Nov 15 '05 #6
timtos,

You did it! IT now works perfectly. You are good. :-)

I will now cease banging my head on the wall.

Thank you, thank you, , thank you..

-Ed
You can do it with visual studio. Just look for the property
ContextMenu
in the TreeView properties! There is a combobox, where you can select
your contextMenu.

Or do it in the code - like this:
this.treeView.ContextMenu = this.contextMenu1;

Hope this finally helps ;-)
Greetings,
timtos.

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
4
by: Mohit Gupta | last post by:
Hi all, Lately I have been working on an application in VB .net CF for Pocket PC device. I have a small question about Context Menu. When I try to close the window after context menu is poped...
4
by: Claire | last post by:
Sorry Ive added this twice (sortof) but if I'd added an addendum to the first one then this would probably have been ignored. This problem affects a ContextMenu attached to a NotifyIcon object. I...
1
by: Chris Murphy via DotNetMonster.com | last post by:
Hi all, I'm just wondering if any one can help me with this development issue I'm having. I've created a customized treeview control to handle the particular tasks to which I'll be using it. Within...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
4
by: Fred | last post by:
Hi, I have a list box with a context menu. When I right click the menu appears as expected. However when I select an option on the context menu the menu does not close. The click event on the...
2
by: jamil | last post by:
I have a tab control on a form with several tab pages. I have also added a context menu to the tab control that contains a couple of menu items-- Add and Delete. What I would like to do is...
0
by: boeroboy | last post by:
Hi all. I know there is a lot of mystery around the desktop context menu. Plenty of brickerbrack about adding shell menu extensions, but not much on shownig it. Using ShellContextMenu interfaces,...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got one Context Menu named mi_EasterEggs with three (3) menu items: * mi_FontArial * mi_FontCourier * mi_RawData All menu items have their Visible properties set to False when the form...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.