在 tablelayoutpanel 单元格中的其他形状旁边添加形状 c# .NET
Adding shapes next to other shapes inside a tablelayoutpanel cell c# .NET
我是 .NET 应用程序的新手,我正在尝试创建一个带有 Label
和两个圆圈的 TableLayoutPanel
:
为此,我正在创建带有标签和 2 个圆圈的对象,并从上面(我有 TableLayoutPanel 的地方)尝试将这些元素添加到单元格中:
this.cellTableLayoutPanel.Controls.Add(this.devList[rowCount].devNameLabel, 0, rowCount);
this.cellTableLayoutPanel.Controls.Add(this.devList[rowCount].circle_in, 0, rowCount);
元素:(标签 devNameLabel,System.Windows.Shapes.Ellipse circle_in)
我不知道该怎么做,因为它不允许我将圆圈添加到 table 单元格。我想添加 3 个项目并在单元格左侧添加边距以设置它们的位置。
我已经搜索了一段时间,但我还没有找到如何执行此操作。有人知道吗?非常感谢!
Ellipse
是一个 WPF 元素,不能直接承载在 windows 表单控件上,因为它不是 Control
。
WPF Ellipse 在 Windows Forms 中的等效项是什么?
在 Windows 表单中,要在表单上添加形状,您可以使用 Visual Studio PowerPacks Controls 线、椭圆形和矩形等 Shape
组件。
您可以在 Form
或 Panel
上放置 Shape
并使用它们。但即使使用 PowerPack Shape
,您也无法使用代码直接在 Control
中托管 Shape
。
注意 1: 使用设计器时,您可以在 Form
上放置 Shape
,但对于 TableLayoutPanel
,您不能放置 Shape
在 TableLayoutPanel
!
上使用设计师
注意 2: 此外,当尝试使用代码将 Shape
添加到 Controls
collection 时,您会发现您不能将其添加到 Controls
collection!
使用设计器,为什么我可以在 Form 或 Panel[ 中承载 Shape =81=] 但不在 TableLayoutPanel?
中
事实上,当您将形状拖放到窗体上时,设计者会创建一个不可见的 ShapeContainer
,它拥有 Shape
。 Shape
将绘制在 ShapeContainer
的表面上,而 ShapeContainer
托管在 Form
或 Panel
上。这是设计师的把戏。
对于 TableLayoutPanel
此功能未在设计器中实现。
如何使用代码向控件 collection 添加形状?
您可以将 Shape
添加到 ShapeContainer
,然后将 Panel
添加到 TableLayoutPanel
:
var container = new ShapeContainer();
var oval = new OvalShape(container) { Width = 20, Height = 20 };
this.tableLayoutPanel1.Controls.Add(container, 0, 0);
还有其他选择吗?
是的,作为替代方案,您可以简单地从 Control
派生并覆盖 OnPaint
方法来创建自定义 Oval 控件。
我是 .NET 应用程序的新手,我正在尝试创建一个带有 Label
和两个圆圈的 TableLayoutPanel
:
为此,我正在创建带有标签和 2 个圆圈的对象,并从上面(我有 TableLayoutPanel 的地方)尝试将这些元素添加到单元格中:
this.cellTableLayoutPanel.Controls.Add(this.devList[rowCount].devNameLabel, 0, rowCount);
this.cellTableLayoutPanel.Controls.Add(this.devList[rowCount].circle_in, 0, rowCount);
元素:(标签 devNameLabel,System.Windows.Shapes.Ellipse circle_in)
我不知道该怎么做,因为它不允许我将圆圈添加到 table 单元格。我想添加 3 个项目并在单元格左侧添加边距以设置它们的位置。
我已经搜索了一段时间,但我还没有找到如何执行此操作。有人知道吗?非常感谢!
Ellipse
是一个 WPF 元素,不能直接承载在 windows 表单控件上,因为它不是 Control
。
WPF Ellipse 在 Windows Forms 中的等效项是什么?
在 Windows 表单中,要在表单上添加形状,您可以使用 Visual Studio PowerPacks Controls 线、椭圆形和矩形等 Shape
组件。
您可以在 Form
或 Panel
上放置 Shape
并使用它们。但即使使用 PowerPack Shape
,您也无法使用代码直接在 Control
中托管 Shape
。
注意 1: 使用设计器时,您可以在 Form
上放置 Shape
,但对于 TableLayoutPanel
,您不能放置 Shape
在 TableLayoutPanel
!
注意 2: 此外,当尝试使用代码将 Shape
添加到 Controls
collection 时,您会发现您不能将其添加到 Controls
collection!
使用设计器,为什么我可以在 Form 或 Panel[ 中承载 Shape =81=] 但不在 TableLayoutPanel?
中事实上,当您将形状拖放到窗体上时,设计者会创建一个不可见的 ShapeContainer
,它拥有 Shape
。 Shape
将绘制在 ShapeContainer
的表面上,而 ShapeContainer
托管在 Form
或 Panel
上。这是设计师的把戏。
对于 TableLayoutPanel
此功能未在设计器中实现。
如何使用代码向控件 collection 添加形状?
您可以将 Shape
添加到 ShapeContainer
,然后将 Panel
添加到 TableLayoutPanel
:
var container = new ShapeContainer();
var oval = new OvalShape(container) { Width = 20, Height = 20 };
this.tableLayoutPanel1.Controls.Add(container, 0, 0);
还有其他选择吗?
是的,作为替代方案,您可以简单地从 Control
派生并覆盖 OnPaint
方法来创建自定义 Oval 控件。