R
rockdale
Hi, All:
I have a windows form and in the form I have a panel that dynamic load
different user controls when user click on buttons. the user controls
allows user to enter data and save into database. In the button click
event I need to save current data entry into database before I load
the new user controls. See code snippet bellow.
code under my mainForm
private string current_control;
MyControl2 myControl2 = new MyControl2();
MyControl1 myControl1 = new MyControl1();
Button1_Click(){
SaveCurerentControlData();
panel1.Controls.Clear();
panel1.Controls.Add(myControl1);
current_control = "myControl1";
}
Button2_Click(){
SaveCurerentControlData();
panel1.Controls.Clear();
panel1.Controls.Add(myControl2);
current_control = "myControl2";
}
SaveCurerentControlData(){
Switch(current_control){
case "myControl1":
myControl1.SaveData();
break;
case "myControl2":
myControl2.SaveData();
break;
}
}
---------------------
Now, as I have more than 20 user defined user controls need to load
into this form. I am wondering maybe there is a better way to call the
user controls SaveData function. (In another word, in the above
SaveCurerentControlData() function, how can I get rid of the annoying
switch case, I am thinking about let all my user controls derived from
a basecontrol and overwrite the savedata method in the basecontrol,
and in the SaveCurrentControlData() function, just call the general
savedata method, but I do not know how to do it? Or is there a better
way to achieve what I am trying to do? Hope I explain my question
clearly.
Thanks a lot.
-rockdale
I have a windows form and in the form I have a panel that dynamic load
different user controls when user click on buttons. the user controls
allows user to enter data and save into database. In the button click
event I need to save current data entry into database before I load
the new user controls. See code snippet bellow.
code under my mainForm
private string current_control;
MyControl2 myControl2 = new MyControl2();
MyControl1 myControl1 = new MyControl1();
Button1_Click(){
SaveCurerentControlData();
panel1.Controls.Clear();
panel1.Controls.Add(myControl1);
current_control = "myControl1";
}
Button2_Click(){
SaveCurerentControlData();
panel1.Controls.Clear();
panel1.Controls.Add(myControl2);
current_control = "myControl2";
}
SaveCurerentControlData(){
Switch(current_control){
case "myControl1":
myControl1.SaveData();
break;
case "myControl2":
myControl2.SaveData();
break;
}
}
---------------------
Now, as I have more than 20 user defined user controls need to load
into this form. I am wondering maybe there is a better way to call the
user controls SaveData function. (In another word, in the above
SaveCurerentControlData() function, how can I get rid of the annoying
switch case, I am thinking about let all my user controls derived from
a basecontrol and overwrite the savedata method in the basecontrol,
and in the SaveCurrentControlData() function, just call the general
savedata method, but I do not know how to do it? Or is there a better
way to achieve what I am trying to do? Hope I explain my question
clearly.
Thanks a lot.
-rockdale