Reputation: 5039
everybody. My plan is to create a separate class, in which I would declare all the labels and textbox values. But to do so I have to inherit from a form. The problem is that when I inherit from a form my class becomes a form and calls elements from itself. Setting the properties of labels and textboxes to public did not help. Any ideas?
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
}
}
Upvotes: 0
Views: 65
Reputation: 18513
You should probably inherit from UserControl instead. With your own UserControl, you can add it to one or more forms in one or more places.
There are plenty of tutorials out there which guide you in creating your own WinForms UserControl.
Upvotes: 2