ContextMenu Problem

G

Guest

Hi

I have the following problem
I'm trying to create a dynamic ContextMenu (used by a NotifyIcon), but when I repopulate the submenus the submenus won't show. the little arrow is there - indicating that there are submenues available - but they won't popup

On the ContextMenu objects Popup eventhandler i have the routine that populates/repopulates the submenus
The submenues is populated like this

menuConnect.MenuItems.Clear()
menuDisconnect.MenuItems.Clear()
string[] names = reg.GetSubKeyNames()
for (int i=0; i<names.Length; i++)
if (names.ToLower().Equals("startup") || names.ToLower().Equals("")) continue
menuConnect.MenuItems.Add(new MenuItem(names, new EventHandler(menuConnect_Click)))
menuDisconnect.MenuItems.Add(new MenuItem(names, new EventHandler(menuDisconnect_Click)))


where menuContent and menuDisconnect are menuitems added to the contextMenu in design-time
Any help is appriciated. I've run out of ideas on how to solve this

The menu "looks" like this
Exi
Setting
------ (break
Connec
- Submenu to Connect her
Disconnec
- Submenu to Disconnect her

// Fredrik
 
G

Guest

You are trying to assign the same menu item name to both connect and disconnect.. See your code modified in this pop up routine

using System
using System.Drawing
using System.Collections
using System.ComponentModel
using System.Windows.Forms
using System.Data

namespace ContextMEnuOnFl

/// <summary
/// Summary description for Form1
/// </summary
public class Form1 : System.Windows.Forms.For

private System.Windows.Forms.ContextMenu contextMenu1
private System.Windows.Forms.MenuItem menuItem1
private System.Windows.Forms.MenuItem menuItem2
private System.Windows.Forms.MenuItem menuConnect
private System.Windows.Forms.MenuItem menuDisconnect
private System.Windows.Forms.Label label1
/// <summary
/// Required designer variable
/// </summary
private System.ComponentModel.Container components = null

public Form1(

/
// Required for Windows Form Designer suppor
/
InitializeComponent()

/
// TODO: Add any constructor code after InitializeComponent cal
/


/// <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 cod
/// <summary
/// Required method for Designer support - do not modif
/// the contents of this method with the code editor
/// </summary
private void InitializeComponent(

this.contextMenu1 = new System.Windows.Forms.ContextMenu()
this.menuItem1 = new System.Windows.Forms.MenuItem()
this.menuItem2 = new System.Windows.Forms.MenuItem()
this.menuConnect = new System.Windows.Forms.MenuItem()
this.menuDisconnect = new System.Windows.Forms.MenuItem()
this.label1 = new System.Windows.Forms.Label()
this.SuspendLayout()
//
// contextMenu
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
this.menuItem1
this.menuItem2
this.menuConnect
this.menuDisconnect})
this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup)
//
// menuItem
//
this.menuItem1.Index = 0
this.menuItem1.Text = "Exit"
//
// menuItem
//
this.menuItem2.Index = 1
this.menuItem2.Text = "-"
//
// menuConnec
//
this.menuConnect.Index = 2
this.menuConnect.Text = "Connect"
//
// menuDisconnec
//
this.menuDisconnect.Index = 3
this.menuDisconnect.Text = "Disconnect"
//
// label
//
this.label1.Location = new System.Drawing.Point(64, 56)
this.label1.Name = "label1"
this.label1.Size = new System.Drawing.Size(100, 72)
this.label1.TabIndex = 0
this.label1.Text = "Right Click for your menu..."
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13)
this.ClientSize = new System.Drawing.Size(292, 266)
this.ContextMenu = this.contextMenu1
this.Controls.Add(this.label1)
this.Name = "Form1"
this.Text = "Form1"
this.ResumeLayout(false)


#endregio

/// <summary
/// The main entry point for the application
/// </summary
[STAThread
static void Main()

Application.Run(new Form1())

private void contextMenu1_Popup(object sender, System.EventArgs e

menuConnect.MenuItems.Clear()
menuDisconnect.MenuItems.Clear()
string[] names = new String[] {"Option1","Option2","Option3"}
for (int i=0; i<names.Length; i++)

if (names.ToLower().Equals("startup") || names.ToLower().Equals("")) continue
menuConnect.MenuItems.Add(new MenuItem(names+"C", new EventHandler(menuConnect_Click)))
menuDisconnect.MenuItems.Add(new MenuItem(names+"D", new EventHandler(menuDisconnect_Click)))


private void menuConnect_Click(object sender, EventArgs e

this.label1.Text = ((MenuItem)sender).Text

private void menuDisconnect_Click(object sender, EventArgs e

this.label1.Text = ((MenuItem)sender).Text


}
 
G

Guest

Hi Fredrik

Try deleting the top level menu items and then add them again so, in your menu structure ..

The menu "looks" like this
Exi
Setting
------ (break
Connec
- Submenu to Connect her
Disconnec
- Submenu to Disconnect her

1) Delete the Connect and Disconnect menu item
2) Create new Menu Items for Connect and Disconnec
3) Add the sub-menu items to the Connect and Disconnec
4) Add the Connect and Disconnect menu items back to the main menu

e.g

menuConnect.MenuItems.Clear()
menuDisconnect.MenuItems.Clear()

// Remove menuConnec
// Remove menuDisconnec

// Create new menuConnec
menuConnect = new MenuItem( "Connect" ); // et

// Create new menuConnec
menuDisconnect = new MenuItem( "Disconnect" ); // et

string[] names = reg.GetSubKeyNames()
for (int i=0; i<names.Length; i++)
if (names.ToLower().Equals("startup") || names.ToLower().Equals("")) continue
menuConnect.MenuItems.Add(new MenuItem(names, new EventHandler(menuConnect_Click)))
menuDisconnect.MenuItems.Add(new MenuItem(names, new EventHandler(menuDisconnect_Click)))

I am not sure why but i had exactly the same problem and this worked fine :

HT

RS
 
Top