Hi.
I am trying to understand design pattern problems. I am trying to modify the code like this in winforms and trying to see if any design pattern suits my requirement. Please suggest which is the best design pattern in this scenario. This is very basic code containing 2 tab pages which might have different controls can be added dynamically and read out different files on click of particular tab. To elaborate more... I have written this code to learn and understand design pattern. This is just a scenario where user click on a particular tab which will show dynamic controls generated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Name.Equals("tabPage1"))
{
GeneratedynamicControlsForTab1();
}
else if (tabControl1.SelectedTab.Name.Equals("tabPage2"))
{
GeneratedynamicControlsForTab2();
}
}
private void GeneratedynamicControlsForTab1()
{
Label label1 = new Label();
label1.Text = "Label1";
tabPage1.Controls.Add(label1);
}
private void GeneratedynamicControlsForTab2()
{
tabPage1.Controls.Clear();
Label label2 = new Label();
label2.Text = "Label2";
tabPage2.Controls.Add(label2);
}
}
Please let me know if this below implementation is correct for above scenario Please let me know if this implementation is correct to modify above code to state pattern.
public partial class Form1 : Form
{
void GenerateControl(iState state)
{
switch (state.value)
{
case 1:
GenerateControlforTab1();
break;
case 2:
GenerateControlforTab2();
break;
}
}
void GenerateControlforTab1()
{
}
void GenerateControlforTab2()
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
IState state = new State()
if (tabControl1.SelectedTab.Name.Equals("tabPage1"))
{
state.value =1 ; }
else if (tabControl1.SelectedTab.Name.Equals("tabPage2"))
{
state.value =2;
}
this.GenerateControls();
}
}
interface iState {
void GenerateControls();
}