简体   繁体   中英

Copy all the data from one user control to another user control of Same Type

I want to transfer all data from HomeAddressUC to PermanentAddressUC with checkBox SameAsPrevious

Each UserControl has same Type(AddressUserControl)

在此处输入图片说明

DataSource to Fill HomeAddressUC is code is like this

private void SetTabPageDetails(string tabPageName, CustomerDetails customerDetailsCache)
{
     customerDetailsCache = // calling stored procedure
     PermanentAddressUC.SetDetails(customerDetailsCache.Addresses[0]);
}

Scope of that DataSource is upto it method SetTabPageDetails() only logic I was trying to implement is on checkbox changed event is

if (chkSameAsPervious.Checked)
{
    foreach (var addressCtl in from Control ctl in this.ADDRESS_TAB.Controls select ctl as BankSys24.UI.UserControls.AddressUserControl)
    {
        if (addressCtl.GroupBoxText == "Mailing Address")
        {
            // want to do something here
        }
    }
}

I try to follow the relevant link

Best practice when you need two user controls (winforms) to communicate

it says to use the Third Common User Control a Container or interface

What is the optimized way to do it?

Why you need to different user controls for home and mailing address if all the fields same?

you can use one user control for both and create property for set the group name as "Home address" or "Mailing address"

You can have two public methods to set address fields by passing address object and get address object by reading fields of the form.

On check change event of the checkbox you can get address object by calling home address user control instant get address method and then you can set that details on mailing address user control instant by calling set address method by passing address object.

hope this helps

ok , Thnaks for Help

Possible Shortest Solution i Found is like this :

if (chkSameAsPervious.Checked)
            {
                    MailingAddressUC.SetDetails(PermanentAddressUC.GetDetails() as BankSys24.DTO.Customer.Address);
            }

where

 public object GetDetails()
        {
            return new BankSys24.DTO.Customer.Address { //data };
        }

and

  public void SetDetails(object input)
        {
           //intermediate Object
            if (details != null)
            {
//Mapping

}

Getdetails maps the all data as Container Class Object which can be Directly pass to SetDetails() ...

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM