大家。我的计划是创建一个单独的类,在其中我将声明所有标签和文本框值。但要做到这一点,我必须继承一个表格。问题是,当我从一个表单继承时,我的类成为一个表单,并从自身调用元素。将标签和文本框的属性设置为公共没有帮助。有什么想法吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assignment2v3
{
class Declarations : Form1
{
public List<Label> ErrRep
{ get; set; }
public List<TextBox> TextBoxes
{ get; set; }
public List<ComboBox> ComboBoxes
{ get; set; }
public Declarations()
{
ErrRep = DeclareErrorReports();
TextBoxes = DeclareTextBoxes();
ComboBoxes = DeclareComboBoxes();
}
List<Label> DeclareErrorReports()
{
var ER = new List<Label>();
ER.Add(errorReport1);
ER.Add(errorReport2);
ER.Add(errorReport3);
return ER;
}//Would be used if try catch worked
List<TextBox> DeclareTextBoxes()
{
List<TextBox> TextBoxes = new List<TextBox>();
TextBoxes.Add(textBoxPizza1);
TextBoxes.Add(textBoxPizza2);
TextBoxes.Add(textBoxPizza3);
return TextBoxes;
}//Puts all textBoxes into a list
List<ComboBox> DeclareComboBoxes()
{
var ComboBoxes = new List<ComboBox>();
ComboBoxes.Add(comboBoxPizza1);
ComboBoxes.Add(comboBoxPizza2);
ComboBoxes.Add(comboBoxPizza3);
return ComboBoxes;
}//Puts all comboboxes into a list
// ^ Boring declarations
}
}
答案 0 :(得分:2)
您应该继承UserControl。使用您自己的UserControl,您可以将其添加到一个或多个位置的一个或多个表单中。
有很多教程可以指导您创建自己的WinForms UserControl。