Dynamic loading user controls and saving data?

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
 
J

Jack Jackson

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 think what I would do would be to define an Interface with a
SaveData method. Derive your classes from a base class that
implements the interface, and have the base class method that
implements SaveData be overridable. Override the method in each
derived class. You could also implement the Interface individually in
each of your classes rather than deriving from a base class.

To call the method, loop through the controls in the form's Control
collection. For each control, check if it implements your Interface,
and if so call the SaveData method. You may have to recurse into
containers if not all of the controls are direct children of the form.
 
R

rockdale

Thanks Jack.

I created a base class and all my user controls derived from this base
class and override the abstract method. Thing work fine. The only
issue I got is when I try to open the user control I got the error:
The designer must create an instance of type
'iCare.UIControls.BaseControl' but it cannot because the type is
declared as abstract.


-------------------------
public interface IUIControl
{
bool SaveData();
void LoadData();
}
-------------------

public abstract partial class BaseControl : UserControl, IUIControl
{
public BaseControl()
{
InitializeComponent();
}

public abstract void LoadData();
public abstract bool SaveData();
}

-------------------------
public partial class MyUserControl1 : BaseControl
{
}
--------------------------
in the main form, I have panel1 to load and unload the control:
private void button2_Click(object sender, EventArgs e)
{
ResetFields();
try
{
//Since my panel just have a user control, cast my
user control to basecontrol
BaseControl control =
(BaseControl)this.panel1.Controls[0];
control.SaveData();

this.panel1.Controls.Clear();
this.panel1.Controls.Add(myUserControl2);

}
catch (Exception ex)
{
}
}
------------------------------------------------------------------

Is there a work around for the error I got on design time?

Also, you talked about
each of your classes rather than deriving from a base class.

I tried to implement the IUIControl in myUserControl1, but in my main
form I do not know how to cast the different user controls to a base
control (we do not have one now) and call the savedata method.

Thanks again for any helps.
-rockdale
 
J

Jack Jackson

That is a restriction of the designer. The designer needs to
instantiate the base class, and it can't if the base class is
Abstract.

Thanks Jack.

I created a base class and all my user controls derived from this base
class and override the abstract method. Thing work fine. The only
issue I got is when I try to open the user control I got the error:
The designer must create an instance of type
'iCare.UIControls.BaseControl' but it cannot because the type is
declared as abstract.


-------------------------
public interface IUIControl
{
bool SaveData();
void LoadData();
}
-------------------

public abstract partial class BaseControl : UserControl, IUIControl
{
public BaseControl()
{
InitializeComponent();
}

public abstract void LoadData();
public abstract bool SaveData();
}

-------------------------
public partial class MyUserControl1 : BaseControl
{
}
--------------------------
in the main form, I have panel1 to load and unload the control:
private void button2_Click(object sender, EventArgs e)
{
ResetFields();
try
{
//Since my panel just have a user control, cast my
user control to basecontrol
BaseControl control =
(BaseControl)this.panel1.Controls[0];
control.SaveData();

this.panel1.Controls.Clear();
this.panel1.Controls.Add(myUserControl2);

}
catch (Exception ex)
{
}
}
------------------------------------------------------------------

Is there a work around for the error I got on design time?

Also, you talked about
each of your classes rather than deriving from a base class.

I tried to implement the IUIControl in myUserControl1, but in my main
form I do not know how to cast the different user controls to a base
control (we do not have one now) and call the savedata method.

Thanks again for any helps.
-rockdale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top