By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,455 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,455 IT Pros & Developers. It's quick & easy.

VS 2008 and context menus

P: n/a

I have a class which includes a context menu. The class, and context menu, appears to work fine when I run the application but when
I look at the designer for the class I get the errors :

The variable renameSwatchMenuItem is either undeclared or was never assigned.
The variable deleteSwatchMenuItem is either undeclared or was never assigned.

Any ideas of how I can track the problem down would be welcome. The following is the relevant code
Thanks,
Steve

....
protected System.Windows.Forms.ContextMenu meContextMenu;
protected MenuItem renameSwatchMenuItem;
protected MenuItem deleteSwatchMenuItem;
protected bool meContextMenuEnabled = true;

....

protected SwatchPanel()
{
InitializeComponent();

this.meContextMenuEnabled = true;
EnableContextMenu(false);
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.meColorTip = new System.Windows.Forms.ToolTip(this.components);
this.deleteSwatchMenuItem = new System.Windows.Forms.MenuItem("Delete Swatch", new EventHandler(this.m_contextMenu_Popup));
this.renameSwatchMenuItem = new System.Windows.Forms.MenuItem("Rename Swatch", new EventHandler(this.m_contextMenu_Popup));
this.SuspendLayout();

this.meColorTip.Active = false;

this.meContextMenu = new ContextMenu(new System.Windows.Forms.MenuItem[] { this.deleteSwatchMenuItem,
this.renameSwatchMenuItem});
this.meContextMenu.Popup += new System.EventHandler(this.m_contextMenu_Popup);

this.ContextMenu = this.meContextMenu;
this.ResumeLayout(false);
}
private void m_contextMenu_Popup(object sender, System.EventArgs e)
{
MenuItem miClicked = null;

if (sender is MenuItem)
miClicked = (MenuItem)sender;
else
return;

string item = miClicked.Text;

switch (item)
{
case "Delete Swatch" : DeleteSwatch(this.meRightClickPoint);
break;

case "Rename Swatch" : RenameSwatch(this.meRightClickPoint);
break;

}
}

internal void EnableContextMenu(bool enable)
{
if (enable)
{
if (!this.meContextMenuEnabled)
{
this.meContextMenu.Popup += new System.EventHandler(this.m_contextMenu_Popup);
this.meContextMenuEnabled = true;
}
}
else
{
if (this.meContextMenuEnabled)
{
this.meContextMenu.Popup -= new System.EventHandler(this.m_contextMenu_Popup);
this.meContextMenuEnabled = false;
}
}
}
Jun 30 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a

as soon as I moved all the MenuItem and EventHandler assignments from InitializeComponent the errors disappeared.

Thanks anyway,
Steve

On Mon, 30 Jun 2008 18:33:04 -0400, steve <s_******@yahoo.comwrote:
>
I have a class which includes a context menu. The class, and context menu, appears to work fine when I run the application but when
I look at the designer for the class I get the errors :

The variable renameSwatchMenuItem is either undeclared or was never assigned.
The variable deleteSwatchMenuItem is either undeclared or was never assigned.

Any ideas of how I can track the problem down would be welcome. The following is the relevant code
Thanks,
Steve

...
protected System.Windows.Forms.ContextMenu meContextMenu;
protected MenuItem renameSwatchMenuItem;
protected MenuItem deleteSwatchMenuItem;
protected bool meContextMenuEnabled = true;

...

protected SwatchPanel()
{
InitializeComponent();

this.meContextMenuEnabled = true;
EnableContextMenu(false);
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.meColorTip = new System.Windows.Forms.ToolTip(this.components);
this.deleteSwatchMenuItem = new System.Windows.Forms.MenuItem("Delete Swatch", new EventHandler(this.m_contextMenu_Popup));
this.renameSwatchMenuItem = new System.Windows.Forms.MenuItem("Rename Swatch", new EventHandler(this.m_contextMenu_Popup));
this.SuspendLayout();

this.meColorTip.Active = false;

this.meContextMenu = new ContextMenu(new System.Windows.Forms.MenuItem[] { this.deleteSwatchMenuItem,
this.renameSwatchMenuItem});
this.meContextMenu.Popup += new System.EventHandler(this.m_contextMenu_Popup);

this.ContextMenu = this.meContextMenu;
this.ResumeLayout(false);
}
private void m_contextMenu_Popup(object sender, System.EventArgs e)
{
MenuItem miClicked = null;

if (sender is MenuItem)
miClicked = (MenuItem)sender;
else
return;

string item = miClicked.Text;

switch (item)
{
case "Delete Swatch" : DeleteSwatch(this.meRightClickPoint);
break;

case "Rename Swatch" : RenameSwatch(this.meRightClickPoint);
break;

}
}

internal void EnableContextMenu(bool enable)
{
if (enable)
{
if (!this.meContextMenuEnabled)
{
this.meContextMenu.Popup += new System.EventHandler(this.m_contextMenu_Popup);
this.meContextMenuEnabled = true;
}
}
else
{
if (this.meContextMenuEnabled)
{
this.meContextMenu.Popup -= new System.EventHandler(this.m_contextMenu_Popup);
this.meContextMenuEnabled = false;
}
}
}
Jul 1 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.