locked
using method from form1 to form2 in c# RRS feed

  • Question

  • 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.

    Wednesday, June 4, 2014 10:03 AM

Answers

  • Hi, maybe this can work:

        private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                int _handle = (int)item.Tag;
                sf = new SendForm(_handle, this);
                sf.Show();
            } 
        }
        Main m;
        int handle;
    
        public SendForm(int handle, Main main)
        {
            InitializeComponent();
            this.m = main;
            this.handle = handle;
        }

    • Marked as answer by avahdon Wednesday, June 4, 2014 10:38 AM
    Wednesday, June 4, 2014 10:28 AM

All replies

  • Hi, maybe this can work:

        private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                int _handle = (int)item.Tag;
                sf = new SendForm(_handle, this);
                sf.Show();
            } 
        }
        Main m;
        int handle;
    
        public SendForm(int handle, Main main)
        {
            InitializeComponent();
            this.m = main;
            this.handle = handle;
        }

    • Marked as answer by avahdon Wednesday, June 4, 2014 10:38 AM
    Wednesday, June 4, 2014 10:28 AM
  • Hi, maybe this can work:

        private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                int _handle = (int)item.Tag;
                sf = new SendForm(_handle, this);
                sf.Show();
            } 
        }
        Main m;
        int handle;
    
        public SendForm(int handle, Main main)
        {
            InitializeComponent();
            this.m = main;
            this.handle = handle;
        }

    thx. i figure that i'm creating new instance of main that's why i'm having null value
    Wednesday, June 4, 2014 10:38 AM