I have a problem with using a method from form2 to form1. The error is "Object reference not set to an instance of an object." and I can't figure out what I'm doing wrong. I'm still beginner in form programming and I'm having a hard time.
Here is my code in form1:
// showing form2 and pass the value of the _handle
private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
int _handle = (int)item.Tag;
sf = new SendForm(_handle);
sf.Show();
}
}
// sending message using socket
public void sendT(int _handle, string msg)
{
byte[] sdata = Encoding.ASCII.GetBytes(msg);
serverSocket[_handle].Send(sdata, 0, sdata.Length, 0);
}
and here is my form2 code:
Main m = new Main();
int handle;
public SendForm(int handle)
{
InitializeComponent();
this.handle = handle;
}
private void button1_Click(object sender, EventArgs e)
{
m.sendT(handle, "msgbox||test message||warning");
}
I can't figure out how to fix this.
nullvalue at runtime and your code assumes that it would have a valid value. - DavidSendFormneeds some knowledge of your main form, at the moment, it doesnt. - Sayse