【问题标题】:How to set the Control.Parent proprety in C#如何在 C# 中设置 Control.Parent 属性
【发布时间】:2015-07-05 22:58:31
【问题描述】:

我正在为我的 C# Web 浏览器制作插件解析器。

在为插件制作按钮时,我需要将ToolStripButton添加到ToolStrip中。

这是我的代码:

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

我在第 2 行遇到错误:
System.Windows.ToolStripItem.Parent 由于其权限级别而无法访问。

我已经在 Microsoft 上查找了如何设置它。它在某处说 .NET 保护。我研究了它,它没有任何意义。

【问题讨论】:

  • 好问题,从我这边投票
  • @SQLPolice 感谢您的反馈!

标签: c# parent toolstripbutton


【解决方案1】:

看msdn,Parent属性的定义是:

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

由于它被标记为内部,它只能在程序集 System.Windows.Forms 中使用。这意味着您不能在程序集中使用此属性。但是,它确实公开了 GetCurrentParent() 方法,因此您可以获得当前父级。它没有公开 setter 方法,但是对于这个对象,您需要将它添加到它的父项集合中。所以像

ToolStripButton pluginButton = new ToolStripButton();

toolStrip1.Items.Add(pluginButton);

会的。

【讨论】:

  • 感谢您与我分享。您因提供帮助而获得 +1,但他先回答了问题,然后您对其进行了编辑,抱歉;p
【解决方案2】:

不要设置pluginButton.Parent = toolStrip1,而是使用toolStrip1.Items.Add( pluginButton )

【讨论】:

  • 谢谢老兄!我困惑了很长时间,最后决定在 SOF 上问它顺便说一句,你获得了 +1 ;)
  • @SQLPolice 嗯 .. 嗯 ;p 我没注意到,抱歉 :P 好吧,一旦我得到足够的代表,我会的。
猜你喜欢
  • 2011-11-03
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多