ContextMenuStrip and MenuStrip sharing items...

C

Chris Shepherd

Hello group,

Using .NET 2.0 and I'm wondering if it's possible to have one common set
of items for both a ContextMenuStrip (off a right-click) and a regular
MenuStrip. Basically I need a specific menu on the MenuStrip to also be
replicated on right click in a DataGridView. Seems easy to me, however
I've run into some issues with the actual use of
ContextMenuStrip.Items.Add/AddRange.

http://msdn2.microsoft.com/en-us/li....toolstripitemcollection.addrange(VS.80).aspx

The docs state that the void AddRange(ToolStripItemCollection) overload
should add the collection to the current collection, yet in my small
proof of concept app, it doesn't seem to do so. Simple/functional
example is far below.

The currently run line in the constructor which uses this AddRange
overload is throwing an ArgumentOutOfRangeException at runtime:

System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than
the size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.ToolStripItemCollection.get_Item(Int32
index)
at
System.Windows.Forms.ToolStripItemCollection.AddRange(ToolStripItemCollection
toolStripItems)
at TestCMSCopy.Form3..ctor() in C:\Documents and
Settings\chriss\My Documents\Visual Studio
2005\Projects\TestCMSCopy\TestCMSCopy\Form3.cs:line 22
at TestCMSCopy.General.Main(String[] args) in C:\Documents and
Settings\chriss\My Documents\Visual Studio
2005\Projects\TestCMSCopy\TestCMSCopy\General.cs:line 53
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



Commenting that out, and using the foreach below it yields an
InvalidOperationException complaining that the collection was modified:

System.InvalidOperationException was unhandled
Message="Collection was modified; enumeration operation may not execute."
Source="mscorlib"
StackTrace:
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
at TestCMSCopy.Form3..ctor() in C:\Documents and
Settings\chriss\My Documents\Visual Studio
2005\Projects\TestCMSCopy\TestCMSCopy\Form3.cs:line 24
at TestCMSCopy.General.Main(String[] args) in C:\Documents and
Settings\chriss\My Documents\Visual Studio
2005\Projects\TestCMSCopy\TestCMSCopy\General.cs:line 53
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



Any help/suggestions on what (if anything) I am doing wrong is greatly
appreciated.

Chris.

===================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestCMSCopy
{
public class Form3 : Form
{
public Form3()
{
InitializeComponent();

this.dataGridView1.ContextMenuStrip = new ContextMenuStrip();
this.dataGridView1.ContextMenuStrip.Items.Add("Test 1");
this.dataGridView1.ContextMenuStrip.Items.Add("test 2");
this.dataGridView1.ContextMenuStrip.Items.Add("Test 3");


this.dataGridView1.ContextMenuStrip.Items.AddRange(tsmFile.DropDownItems);

//foreach (ToolStripItem t in tsmFile.DropDownItems)
// this.dataGridView1.ContextMenuStrip.Items.Add(t);
}

private void deleteToolStripMenuItem_Click(object sender,
EventArgs e)
{ MessageBox.Show("Clicked delete!"); }

private void editToolStripMenuItem_Click(object sender,
EventArgs e)
{ MessageBox.Show("Clicked edit!"); }

private void addToolStripMenuItem_Click(object sender, EventArgs e)
{ MessageBox.Show("Clicked add!"); }

#region Designer-Generated
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.dataGridView1 = new System.Windows.Forms.DataGridView();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.tsmFile = new System.Windows.Forms.ToolStripMenuItem();
this.addToolStripMenuItem = new
System.Windows.Forms.ToolStripMenuItem();
this.editToolStripMenuItem = new
System.Windows.Forms.ToolStripMenuItem();
this.deleteToolStripMenuItem = new
System.Windows.Forms.ToolStripMenuItem();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(37, 28);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 0;
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new
System.Windows.Forms.ToolStripItem[] {
this.tsmFile});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// tsmFile
//
this.tsmFile.DropDownItems.AddRange(new
System.Windows.Forms.ToolStripItem[] {
this.addToolStripMenuItem,
this.editToolStripMenuItem,
this.deleteToolStripMenuItem});
this.tsmFile.Name = "tsmFile";
this.tsmFile.Size = new System.Drawing.Size(35, 20);
this.tsmFile.Text = "File";
//
// addToolStripMenuItem
//
this.addToolStripMenuItem.Name = "addToolStripMenuItem";
this.addToolStripMenuItem.Size = new
System.Drawing.Size(152, 22);
this.addToolStripMenuItem.Text = "Add";
this.addToolStripMenuItem.Click += new
System.EventHandler(this.addToolStripMenuItem_Click);
//
// editToolStripMenuItem
//
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new
System.Drawing.Size(152, 22);
this.editToolStripMenuItem.Text = "Edit";
this.editToolStripMenuItem.Click += new
System.EventHandler(this.editToolStripMenuItem_Click);
//
// deleteToolStripMenuItem
//
this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
this.deleteToolStripMenuItem.Size = new
System.Drawing.Size(152, 22);
this.deleteToolStripMenuItem.Text = "Delete";
this.deleteToolStripMenuItem.Click += new
System.EventHandler(this.deleteToolStripMenuItem_Click);
//
// Form3
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form3";

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem tsmFile;
private System.Windows.Forms.ToolStripMenuItem
addToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem
editToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem
deleteToolStripMenuItem;
#endregion
}
}
 
C

Chris Shepherd

Chris said:
Commenting that out, and using the foreach below it yields an
InvalidOperationException complaining that the collection was modified: [...]
Any help/suggestions on what (if anything) I am doing wrong is greatly
appreciated.

Yeah, I know what the problem here was -- I noticed right after posting
I wasn't cloning the list like I should be. I'd still like to know if
the first item was a bug or not.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top