Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am calling a ShowDialog of a form from my parent form and i am Populating some data in the child form Through which i want to call a method in my parent form.

The method in my parent form updates controls values in my form.

this is causing me a threadabort exception

Say like

ChildForm Cform=new ChildForm();
   Cform.ShowDialog();


and in ChildForm

ParentForm PForm=new Parentform();
    PForm.Somemethod();//method in my parentForm



In somemethod I am updating the values of the controls in the form by invoking

I am invoking each Control but still I am getting the *ThreadAbort Exception*

**Note: I am using Compact Framework**

Thanks in Advance
Posted

1 solution

You don't want to create a new ParentForm. What you want is to get an instance of the ParentForm that just created the ChildForm.

There are several ways to achive this. Here's the most useful one (IMHO).
ChildForm.cs:
C#
public class ChildForm : Form
{
    // Prepare parent that threre may be some action.
    public event EventHandler ChildWantedSomething;

    // Instead of new ParentForm().SomeMethod()
    EventHandler eh = ChildWantedSomething;
    if( eh != null)
    {
        eh(this, new EventArgs());
    }
}
ParentForm.cs:
C#
public class ParentForm : Form
{
    ChildForm CForm = new ChildForm();
    CForm.ChildWantedSomething += SomeMethod;
}
Of course, for this simple variant to work, SomeMethod needs to have the signature dictated by EventHandler[^]. If that's not possible, you can either subscribe another method to the event, made up solely to cal the one you want.
Or you can use a custom delegate other than EventHandler that matches the signature you need.

[Edit]
For reasons not totally clear, here a version that I don't like as much as the one above, without events.
ChildForm.cs:
C#
public class ChildForm : Form
{
    // Store instance of parent form
    private ParentForm _parentForm = null;

    // Constructor taking ParentForm parameter
    public ChildForm(ParentForm parent)
    {
        _parentForm = parent;
    }

    // Instead of new ParentForm().SomeMethod()
    _parentForm.SomeMethod();
}
This version links both forms tightly together, which is bad. I hope, whoever put that no-events-restraint on you is aware of what he is imposing on you. If not, chances are that later on, when the drawbacks of this solution are revealed, you get blamed for not doing it right.

Good luck.
[/Edit]
 
Share this answer
 
v2
Comments
naga.cherry 4-Sep-13 5:30am    
Cant we do it without defining the handlers, I am restricted not to create a handler.
lukeer 4-Sep-13 6:18am    
The EventHandler class is built into the framework. You don't have to create it.
Or are you restricted not to create events for the child class? Is that because of limitations of the compact framework? Or what else?
naga.cherry 4-Sep-13 6:55am    
Ya am not allowed to create events in the child classes.I was asked to avoid it,dont know the exact reason.
lukeer 4-Sep-13 8:49am    
I updated my solution.
naga.cherry 4-Sep-13 9:30am    
Currently I am implementing the second way you suggested me. If this creates issues then I dont want to implement it. Could you explain how it affects. Thanks

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