2

I'm making a plugin parser for my C# web browser.

When making the button for the plugin, I need to add the ToolStripButton to the ToolStrip.

Here's my code:

ToolStripButton pluginButton = new ToolStripButton();
pluginButton.Parent = toolStrip1;
pluginButton.Image = Simple_Browse.Properties.Resources.plugin;
pluginButton.Alignment = ToolStripItemAlignment.Left;
pluginButton.ToolTipText = TitlePlugin;

I get an error on line 2:
System.Windows.ToolStripItem.Parent is inaccessible due to its permission level.

I've looked it up on Microsoft on how to set it. It says .NET Protection somewhere. I studied that and it makes NO sense.

Community
  • 1
  • 1
Joyesh
  • 27
  • 7

2 Answers2

2

Looking at msdn, the definition of the Parent property is:

[BrowsableAttribute(false)]
protected internal ToolStrip Parent { get; set; }

Since it's marked as internal it can only be used within the assembly System.Windows.Forms. This means you can't use this property in your assembly. However, it does expose the method GetCurrentParent() so you can get the current parent. It does not expose a setter method, but for this object you need to add it to it's parent's item collection. So something like

ToolStripButton pluginButton = new ToolStripButton();

toolStrip1.Items.Add(pluginButton);

will do.

Leigh Shepperson
  • 1,043
  • 5
  • 13
  • Thanks for sharing that with me. You got a +1 for being helpful, but he answered the question first and you edited it, sorry ;p – Joyesh Jul 05 '15 at 23:12
1

Rather than setting pluginButton.Parent = toolStrip1, use toolStrip1.Items.Add( pluginButton ) instead.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • Thanks dude! I was left confused for a **long** time and finally decided to ask it on SOF _you earned a +1 by the way ;)_ – Joyesh Jul 05 '15 at 23:08
  • @SQLPolice Well.. um ;p i didnt notice that, sorry :P Well once i get enough rep i will. – Joyesh Jul 05 '15 at 23:42