Greetings !
I am working on a project in which i have to transfer a few values between forms,but i get an exception when running the code. I have another forms other than the main form and i am trying to transfer the value which i am reading from the user in a textbox in the main form to a label in the second form.
Could someone point me in the right direction or give me a clue as to how to solve this exception.
The code for the first form (in which i read the data from the user) is as it follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace interform_communication
{
public partial class Form1 : Form
{
// add a delegate
public delegate void usrUpdateHandler(object sender, usrUpdateEventArgs e);//IdentityUpdateEventArgs
// add an event of the delegate type
public event usrUpdateHandler usrUpdated;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 newf = new Form2();
string sNewUsr=textBox1.Text;
usrUpdateEventArgs args = new usrUpdateEventArgs(sNewUsr);
usrUpdated(this,args);//eroare aici(object reference not set to an instance of an object)
this.Dispose();
newf.Show();
}
}
public class usrUpdateEventArgs : System.EventArgs
{
private string mUser;
public usrUpdateEventArgs(string sUser)
{
this.mUser = sUser;
}
public string user
{
get
{
return mUser;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace interform_communication
{
public partial class Form1 : Form
{
// add a delegate
public delegate void usrUpdateHandler(object sender, usrUpdateEventArgs e);//IdentityUpdateEventArgs
// add an event of the delegate type
public event usrUpdateHandler usrUpdated;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 newf = new Form2();
string sNewUsr=textBox1.Text;
usrUpdateEventArgs args = new usrUpdateEventArgs(sNewUsr);
usrUpdated(this,args);//eroare aici(object reference not set to an instance of an object)
this.Dispose();
newf.Show();
}
}
public class usrUpdateEventArgs : System.EventArgs
{
private string mUser;
public usrUpdateEventArgs(string sUser)
{
this.mUser = sUser;
}
public string user
{
get
{
return mUser;
}
}
}
}
And the code for the second form (in which i display the string from the first form) looks like this :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace interform_communication
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, usrUpdateEventArgs e)
{
//string txtUser;
Form1 f=new Form1();
f.usrUpdated += new Form1.usrUpdateHandler(Form2_Load);
label1.Text = e.user;
}
}
}
These are some screenshots with the design of the project and the exception which i get when i test my code :
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace interform_communication
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, usrUpdateEventArgs e)
{
//string txtUser;
Form1 f=new Form1();
f.usrUpdated += new Form1.usrUpdateHandler(Form2_Load);
label1.Text = e.user;
}
}
}
These are some screenshots with the design of the project and the exception which i get when i test my code :
1)The exception that i get
2)The first form
3)The second form
The exception appears after i enter text in the textbox in form 1 and press Submit.
Thank you very much for your time !
VulpesPosted May 21, 2014, 9:02 AM
It worked perfectly when I tried it myself before posting.
Ovidiu RomanPosted May 21, 2014, 8:51 AM
internal void usrUpdated(object sender, usrUpdateEventArgs e)
Jignesh Trivedi, your answer works ! Thank you very much for your time !
And this is the tutorial on which i relied to complete the task,and which is very well explained :
http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx
Jignesh TrivediPosted May 21, 2014, 12:18 AM
hi,
you can also pass value using constructor or properties.
string sNewUsr = textBox1.Text;
Form2 newf = new Form2(sNewUsr);
newf.Show();
// Form2
string sNewUser = string.Empty;
public Form2(string uname)
{
sNewUser = uname;
// do your code here...
}
hope this will help you.
VulpesPosted May 20, 2014, 5:41 PM
// Form1
// Form2
// nothing in here
// add this handler