Home > Blockchain >  Close this and send values to parent(Main) window
Close this and send values to parent(Main) window

Time:08-18

Hello all,

I use this but i get an error.

   private void button1_Click(object sender, EventArgs e)
    {
        // When i use this,
        // -------------------------
        // Form1 frm1 = new Form1();
        // frm1.ShowDialog();
        // -------------------------
        // This works for sure...
        // But i don't wanna open that already open form one more time.
        // So i use as u see it below.

        Form1 frm1 = (Form1)this.Owner;
        int MyTotal, a, b;
        a = Convert.ToInt32(TxtPrice.Text);
        b = Convert.ToInt32(TxtQty.Text);
        MyTotal = a * b;
        frm1.dataGridView1.Rows.Add(TxtName.Text,TxtCode.Text,TxtPrice.Text,TxtMt.Text,TxtQty.Text,MyTotal);
        this.Close();

        // But i get error and it says:
        // System.NullReferenceException: 'Object reference not set to an instance of an object.'

    }

anybody can tell me why this doesn't work ? Thank you in advance.

CodePudding user response:

From the error It seems like something from thisTxtName.Text,TxtCode.Text,TxtPrice.Text,TxtMt.Text,TxtQty.Text,MyTotal is null. I recommend you to use breakpoint if you use Visual Studio (Code or any version of VS) and then you will able to see what exactly is null. Anyway, you also can make a filed in your class nullable like this: public string Text{ get; set; } => public string? Text{ get; set; }

CodePudding user response:

Ok guys,

My fault, my mistake. I just forgot to type "this" between the brackets () frm.ShowDialog(this)

Now it works fine. Tnx for all answers.

  • Related