Click here to Skip to main content
15,908,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form in C# that will be called from some other forms. Some controls such as text boxes will be filled from the called form using the following code:

C#
if (mdiparent is ADDSickFiling)
{
    ADDSickFiling _ADDSickFiling = (ADDSickFiling)mdiparent;
    _ADDSickFiling.ReferenceAccountId = Convert.ToInt32(GridViewService.CurrentRow
            .Cells["AccountId"].Value.ToString());
    _ADDSickFiling.ReferenceAccountTextBox.Text = GridViewService.CurrentRow
            .Cells["AccountName"].Value.ToString();
}
else if (mdiparent is ReportCashierByAccOne)
{
    ReportCashierByAccOne _ReportCashierByAccOne 
            = (ReportCashierByAccOne)mdiparent;
    _ReportCashierByAccOne.CashierID = Convert.ToInt32(GridViewService.CurrentRow
            .Cells["AccountId"].Value.ToString());
    _ReportCashierByAccOne.CashierNameTextBox.Text = GridViewService.CurrentRow
            .Cells["AccountName"].Value.ToString();
}
else if (mdiparent is ReportDrugStoreBill)
{
    ReportDrugStoreBill _ReportDrugStoreBill = (ReportDrugStoreBill)mdiparent;
    _ReportDrugStoreBill.CashierID = Convert.ToInt32(GridViewService.CurrentRow
            .Cells["AccountId"].Value.ToString());
    _ReportDrugStoreBill.CashierNameTextBox.Text = GridViewService.CurrentRow
            .Cells["AccountName"].Value.ToString();
}


Based on the parent form that has called this form, any of these three conditions block will be executed. These blocks have a lot of code in common. How can I reduce these codes into one block of code? How can I dynamically cast the type of parent form to mdiparent and access the controls of parent form like TextBox?

What I have tried:

I have tried these code:
C#
var parentForm = mdiparent.GetTypeof()


But it seems to be wrong.
Posted
Updated 7-Oct-20 4:39am

Have the parent forms implement an interface:
C#
public interface ISetAccountDetails
{
    int AccountId { set; }
    string AccountName { set; }
}

public class ADDSickFiling : Form, ISetAccountDetails
{
    ...
    int ISetAccountDetails.AccountId
    {
        set { ReferenceAccountId = value; }
    }
    
    string ISetAccountDetails.AccountName
    {
        set { ReferenceAccountTextBox.Text = value; }
    }
}

public class ReportCashierByAccOne : Form, ISetAccountDetails
{
    ...
    int ISetAccountDetails.AccountId
    {
        set { CashierID = value; }
    }
    
    string ISetAccountDetails.AccountName
    {
        set { CashierNameTextBox.Text = value; }
    }
}

public class ReportDrugStoreBill : Form, ISetAccountDetails
{
    ...
    int ISetAccountDetails.AccountId
    {
        set { CashierID = value; }
    }
    
    string ISetAccountDetails.AccountName
    {
        set { CashierNameTextBox.Text = value; }
    }
}
Then use that interface to update the parent:
C#
if (mdiparent is ISetAccountDetails setAccount)
{
    setAccount.AccountId = Convert.ToInt32(GridViewService.CurrentRow.Cells["AccountId"].Value.ToString());
    setAccount.AccountName = GridViewService.CurrentRow.Cells["AccountName"].Value.ToString();
}
interface - C# Reference | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
BillWoodruff 8-Oct-20 12:49pm    
Interesting ! In this case it appears the OP is using the MDI architecture feom the late neolithic.
Ali Majed HA 8-Oct-20 13:43pm    
Thanks for your response. It is nice and clear to me :)
Basically, don't.
Child forms shouldn't know the parent even exists, much less what type it is, or what properties or controls it contains.
Instead, your child form should tell the parent that it has information available via an Event, and let the parent pick up the data and do what it needs to with it.

See here: Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
Comments
BillWoodruff 8-Oct-20 12:52pm    
Unfortunately, the now deprecated MDI app model forces the exposure of the main app window in each MDIChildForm's MDIParent property.
OriginalGriff 8-Oct-20 12:56pm    
Doesn't mean you have to use it though! :laugh:
Ali Majed HA 8-Oct-20 13:44pm    
Thanks a lot for your help and your great article :)
OriginalGriff 8-Oct-20 13:54pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900