I want to assign a specific richtextbox contextmenustrip through mdi parent form code in a mdi child form but it does not work. How can I set richtextbox1 to use contextMenuStrip2?
The mdi child contains two contextmenustrip already created called contextMenuStrip1 and contextMenuStrip2. The default value of the richtextbox1 is contextMenuStrip1.
Using the following piece of code, the richTextBox1 text attribute is changed as expected however trying to change the contextmenustrip does nothing. It keeps the contextMenuStrip1 displayed meanwhile contextMenuStrip2 would be expected because of the line childForm.Controls["richTextBox1"].ContextMenuStrip.Name = "contextMenuStrip2";.
Mdiparent.cs:
Form childForm = new Form1("contextMenuStrip2");
childForm.Show();
Form1.cs:
public Form1(String correctcontextmenu)
{
InitializeComponent();
richTextBox1.ContextMenuStrip = correctcontextmenu;
}
Error list contains:
Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.ContextMenuStrip'
As you found out, a string is not a ContextMenuStrip. Since the ContextMenuStrips are private to the ChildForm, you should maybe just pass a flag to tell the form which menu it should use:
public Form1(bool useOtherMenu)
{
InitializeComponent();
if (useOtherMenu)
{
richTextBox1.ContextMenuStrip = contextMenuStrip2;
}
}
Then to call it:
Form childForm = new Form1(true);
childForm.MdiParent = this;
childForm.Show();