gpt4 book ai didi

c# - 如何基于另一个 UserControl 使用 WinForms 创建一个 UserControl?

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:58 30 4
gpt4 key购买 nike

我在下面添加了一些屏幕截图和重现步骤。

我的数据模型有一个基类,我们称它为 CommonThing,它有很多属性。然后有几个具有附加属性的此类的特殊版本。我现在想创建一个 UI 来将数据输入到这个模型中。

我创建了一个 UserControl,它包含所有通用属性的控件,如下所示:

internal partial class CommonThingControl : UserControl {

public CommonThingControl() {
InitializeComponent();
}

// Controller code
}

这还会添加一个由 GUI 设计器填充的 CommonThingControl.Designer.cs

我现在创建了一个 SpecialFooThingControl 作为 UserControl 并将类头更改为:

internal partial class SpecialFooThingControl : CommonThingControl {

// implementation
}

当我现在在 GUI 设计器中打开 SpecialFooThingControl 时,我看到了 CommonThingControl 的控件,但它们都被锁定了。然而,我在 CommonThingControl 中有一个 TableLayoutPanel 我想向其中添加内容,但我无法更改任何内容,当我尝试将控件拖入 TableLayoutPanel 鼠标光标变成“禁止 parking ”标志,VS 不允许我这样做。当我将 TableLayoutPanel 的访问器设置为 public 时甚至会发生这种情况。

可以通过文档大纲将控件移动到TableLayoutPanel,但是当我随后重建项目时,它会从 UI 中消失。

我试图通过 UI 设计器实现的目标是不可能的吗?我是否必须手动设置它,还是我忘记了一些额外的步骤?

这是我做的:

首先,创建一个用户控件,这很简单。我创建了一个表格布局面板,我将其设置为 Protected 因为我想向其中添加内容。 Common Control

这是 FooControl.cs 的代码:

namespace GuiTest {
public partial class FooControl : UserControl {
public FooControl() {
InitializeComponent();
}
}
}

FooControl.Designer.cs:

namespace GuiTest {
partial class FooControl {
/// <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 Component 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.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.textBox1, 1, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(360, 28);
this.tableLayoutPanel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(3, 7);
this.label1.Margin = new System.Windows.Forms.Padding(3, 7, 3, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(25, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Foo";
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(34, 3);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(323, 20);
this.textBox1.TabIndex = 1;
//
// FooControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "FooControl";
this.Size = new System.Drawing.Size(360, 28);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);

}

#endregion

protected System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}

现在,我添加了 BarControl: Adding UserControl

现在,我在 BarControl.cs 中将扩展从 UserControl 更改为 FooControl:

namespace GuiTest {
public partial class BarControl : FooControl {
public BarControl() {
InitializeComponent();
}
}
}

BarControl.Designer.cs:

namespace GuiTest {
partial class BarControl {
/// <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 Component 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() {
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}

#endregion
}
}

结果是这样的: Locked Layout Panel

最佳答案

CommonThingControl 中的控件被锁定的原因是子类 SpecialFooThingControl 无法访问它们(它们是私有(private)范围的)。

不过,您可以在 CommonThingControl 的设计器中更改它们的修饰符属性,这将允许您在子类中使用/修改它们。您需要在每个单独的控件上执行此操作,并且可以简单地更改“属性”窗口中的下拉列表。您可能可以在 commonthingcontrol.designer.cs 文件中更改它们,但我也很谨慎,因为该文件是自动生成的,如果您不小心,更改很容易被覆盖.

enter image description here

我建议使用类似 Protected 的东西来确保它们对子控件可见,但防止它们在继承链之外被滥用。

编辑

因此进一步研究表明这是 TableLayoutPanel 的限制。取自https://msdn.microsoft.com/en-us/library/ms171689.aspx :

Avoid Visual Inheritance The TableLayoutPanel control does not support visual inheritance in the Windows Forms Designer. A TableLayoutPanel control in a derived class appears as "locked" at design time.

关于c# - 如何基于另一个 UserControl 使用 WinForms 创建一个 UserControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36592090/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com