Error when using delegate.... ( Error in binding the target method)...
-
Hi... My requirement is to retrieve the child form values into parent form in windows application. For this I am using delegate . And i m getting the following error: " An unhandled exception of type 'System.NullReferenceException' occurred in Delegates.exe Additional information: Error binding to target method. " Please give me the solution if anyone knows . Its urgent. Code which I have written is: Parent Form: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Delegates { public class Form1 : System.Windows.Forms.Form { private string str; public string strPro { set { str=value; } get { return str; } } public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } public void fncForm1Test() { textBox1.Text="Hi"; } private void button1_Click(object sender, System.EventArgs e) { Form2 obj2=new Form2(); obj2.Owner=this; obj2.ParentMain = (Form)this.Owner;//Giving ownership ePCRCustomizeControl page to Main page obj2.HostForm = this;//Giving ownership ePCRCustomizeControl page to ePCR_BtnCompositeControl page obj2.Show(); } } } Child Form: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Delegates { public class Form2 : System.Windows.Forms.Form { public delegate void MyDelegate(); private Form _Parent; public Form ParentMain { get { return _Parent; } set { _Parent = value; } } private Form _Host; public Form HostForm { get { return _Host; } set { _Host = value; } } public Form2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
-
Hi... My requirement is to retrieve the child form values into parent form in windows application. For this I am using delegate . And i m getting the following error: " An unhandled exception of type 'System.NullReferenceException' occurred in Delegates.exe Additional information: Error binding to target method. " Please give me the solution if anyone knows . Its urgent. Code which I have written is: Parent Form: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Delegates { public class Form1 : System.Windows.Forms.Form { private string str; public string strPro { set { str=value; } get { return str; } } public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } public void fncForm1Test() { textBox1.Text="Hi"; } private void button1_Click(object sender, System.EventArgs e) { Form2 obj2=new Form2(); obj2.Owner=this; obj2.ParentMain = (Form)this.Owner;//Giving ownership ePCRCustomizeControl page to Main page obj2.HostForm = this;//Giving ownership ePCRCustomizeControl page to ePCR_BtnCompositeControl page obj2.Show(); } } } Child Form: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Delegates { public class Form2 : System.Windows.Forms.Form { public delegate void MyDelegate(); private Form _Parent; public Form ParentMain { get { return _Parent; } set { _Parent = value; } } private Form _Host; public Form HostForm { get { return _Host; } set { _Host = value; } } public Form2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
peter rankel wrote:
Please give me the solution if anyone knows . Its urgent.
Of course it is... But not enough for you to post some code that points out the exact error, instead of the whole project ?
peter rankel wrote:
obja=new MyDelegate(objForm1.fncForm1Test); obja();
This is weird code, what is it trying to achieve ?
peter rankel wrote:
Form1 objForm1=(Form1)_Parent;
As far as I can see, you should make _Parent an instance of Form1, instead of assuming it is and then trying to cast it.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )