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;
}
}
}
}
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 :
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 !