I'm making a Windows Form Application called FMP. I've got a class called Form1, a class called Panels. Then I use inheritance to make different Panels with different properties.
The reason for doing this is because the teacher doesn't want us to initialize all the panels in the Form-class.
But I'm not sure how to do this. Found some things here @Stackoverflow, but they couldn't help me either.
The Size, The Location and the color are for all the Panels the same. (By clicking on a button, an other panel will appear ;) ) But the Name, Controls on the panel, and BackgroundImages are different. The controls are the most important aspect here.
The question is:
The Width and the Height should be equal to the Widht and the Height from the Form. What is best in programming C#? To set the Width and the Height from Panels in the Form1 (but i made them protected) or declarate the form in the Panels class and use Form1.Width?
The code I'm having right know:
The Form1
public Form1()
{
InitializeComponent();
buttonsProperties();
panelsProperties();
}
private void button1_Click(object sender, EventArgs e)
{
panelsChanged(1);
}
private void button2_Click(object sender, EventArgs e)
{
panelsChanged(2);
}
private void panelsChanged(int panelNr)
{
if (panelNr == 1)
{
panel1.Visible = true;
panel1.Enabled = true;
panel2.Visible = false;
panel2.Enabled = false;
}
else if (panelNr == 2)
{
panel1.Visible = false;
panel1.Enabled = false;
panel2.Visible = true;
panel2.Enabled = true;
}
}
The Panels
class Panels
{
Form1 f = new Form1();
//Color Property
protected Color color { get; set; }
//Size
protected Int32 Width { get; set; }
protected Int32 Height{ get; set; }
//Location
protected Point Location { get; set; }
public Panels()
{
initMembers();
}
private void initMembers()
{
this.Width = f.Width;
this.Height = f.Height;
this.Location = new Point(0, 0);
}
}
public class Panel1 : Panels
{
//Nothing yet.
}
Using the name Panels
as a base class for each panel is confusing:
System.Windows.Forms.Panel
class If I were you I'd let your base class derive from System.Windows.Forms.Panel
:
abstract class MyPanelBase : Panel
{
public MyPanelBase()
{
Dock = DockStyle.Fill;
}
}
class MyPanel1 : MyPanelBase
{
}
This way you automatically get the behavior (and properties) of the Panel
and it allows you to add it to a parent control (in your case, the form).
If all functionality you want is already supported by Panel
you could even skip the MyPanelBase
bit and let MyPanel1
derive directly from Panel
.