【发布时间】:2018-05-14 02:10:23
【问题描述】:
我有一个 groupBox 控件(手动创建),里面有一堆文本框控件,用于显示信息...我正在尝试创建多个这些 groupBox 控件,并且对于每个 groupBox 控件我创建,我将不同的文本添加到 groupBox 中的每个单独的文本框中,然后显示 groupBox ...有点像 stackoverflow 在其数千个网页中具有相同的网页设计/布局的方式,但每个网页显示不同的信息或文本。
在我的代码中我尝试这样做,但我无法找到一种方法来创建手动创建的 groupBox 的新实例
// the GetInstance() method is the same as the "This" Keyword - long story
public static void DisplayTask(Task task2Display)
{
Control groupBox = manuallyCreatedGroupBox; // attempt at creating reference
// obviously doesn't and shouldn't work - lol
// This is my major problem,
// dunno how to create instance of the manually created groupBox
// i can only reference to it, meaning i can only have one
// reference of the groupBox, hence only display one groupBox at a time
groupBox.Location = taskLocation;
taskLocation.Offset(0, 10 + groupBox.Size.Height);
// we do this so that the next groupBox is displayed ten "length" downward
// this edits each individual textbox within the "new" groupBox
foreach (Control cntrl in groupBox.Controls)
{
switch (cntrl.TabIndex)
{
case 1:
cntrl.BackColor = task2Display.priorityColor; // Priority Color
break;
case 2:
cntrl.Text = task2Display.taskName; // Task Name
break;
case 3:
cntrl.Text = task2Display.dscription; // task Description
break;
default:
break;
}
}
GetInstance().Controls.Add(groupBox);
// draws the to be the new groupBox control onto the form
GetInstance().Show(); // show the form
}
只是为了澄清我的目标
我正在尝试显示包含文本框控件的手动创建的 groupBox 的多个实例
问题是,我无法创建手动创建的 groupBox 的新实例,这意味着我无法一次显示多个所述 groupBox 控件(一次只能显示一个)
我的问题是,我如何使用所有当前控件创建手动创建的 groupBox 的新实例。另外,如果有更好的方法来实现我想要做的事情,我会全力以赴
谢谢!
【问题讨论】:
-
您似乎在寻找
UserControl。例如看看Walkthrough: Authoring a Composite Control with Visual C#。您可以找到几个有用的链接here。 -
哇,谢谢……这几乎解决了我的问题,而且,我们又见面了……stackoverflow.com/questions/44337300/…
-
@Jamisco 如果您已经创建了您的
UserControl,您应该在此处将其添加为答案(至少具有最低要求代码的基本想法)。它也会对其他人有所帮助。 -
我应该把这个问题的标题改成“如何创建可重复使用的控件”之类的吗?...当前的问题与答案没有正确的关联
-
@Jamisco 当然,如果您正确更改它会更好
标签: c# winforms instance groupbox