locked
Accessing public methods of another form in WinForms. RRS feed

  • Question

  • Hi,

    In a winform application I have a form called "Form1" , in this form i have implemented some buttons and a datagridview called "dataGridView1" (which is filled with data imported from excel, after the application run). I have another form called "userValues"  that has a button called "buttonderived" and a textbox called "textboxGridRows". I want to fill the textboxGridRows with the number of rows in  "datagridview1".

    Here is the code of creating an object of "Form1" class inside "userVaules" class:

    public partial class userValues : Form // code of userVaules form { public Form1 aform;// =new Form1(); public userValues(Form1 form1) { aform = form1; } public userValues() { InitializeComponent(); }

    // code continues...

    }

     

    Here is the code for opening the "userVaules" from a buttonclick event in "form1".

     private void button3_Click(object sender, EventArgs e)// code of Form1 form
            {
                userValues us = new userValues();
                us.Show();
            }

    And here is the code for filling the textboxGridRows with the number of rows in dataGridView1:

    private void buttonderived_Click(object sender, EventArgs e)// code of userVaules form { // dataGridView1 is declared as public. int ji = aform.dataGridView1.RowCount;// here i get an exception error of type "system.nullreferenceExc textboxGridRows.Text = ji.ToString()+" rows"; // when using "new" keyword the returned result is "0 rows".

    }


    The problem is that I get an exception error of type:

    An unhandled exception of type 'System.NullReferenceException' occurred in importFromExcel.exe

    Additional information: Object reference not set to an instance of an object.

    It suggests me to use "new" keyword to create an object instance.

    But if I use "new" keyword to create an object instance , i always get the result "0" ( the real output should me "100 rows").

    // code of userVaules form

    public Form1 aform =new Form1();// the case when i use "new" keyword returned result is always 0. public userValues(Form1 form1) { aform = form1; } public userValues() { InitializeComponent(); }

    //...

    }

    The question is what should i do to get the number of rows in dataGridView1 (in Form1) and display number of rows in textboxGridRows (in userVaules form) ? The expected resul should be "100 rows" , not "0 rows".  


    Admir

    Wednesday, April 20, 2016 7:45 AM

Answers

  • Hi Admir,
    There are a lot of methods to transfer the data to another form, perhaps you could refer to the following methods:
    1. Constructor.(Your way)

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
    
            private void ShowChild()
            {
                FrmChild child = new FrmChild(this);
                child.Show();
            }
            //child form
            FrmParent paraForm = new FrmParent();
            public FrmChild(FrmParent frm)
            {
                InitializeComponent();
                paraForm = frm;
            }
            private void Button_click(object sender, EventArgs e)
            {
                if (paraForm != null)
                {
                    int count = paraForm.dgvParent.Rows.Count;
                }
            }
    

    2. Owner.

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
    
            private void ShowChild()
            {
                FrmChild child = new FrmChild();
                child.Show(this);
            }
            //child form
            private void Button_click(object sender, EventArgs e)
            {
                if (this.Owner != null)
                {
                    int count = (this.Owner as FrmParent).dgvParent.Rows.Count;
                }
            }
    

    3. OpenForms.

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
            //child form
            private void Button_click(object sender, EventArgs e)
            {
                if (Application.OpenForms["frmParent"] != null)
                {
                    int count = (Application.OpenForms["frmParent"] as FrmParent).dgvParent.Rows.Count;
                }
            }

    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Proposed as answer by Andy ONeill Wednesday, April 20, 2016 10:38 AM
    • Marked as answer by admiri92 Wednesday, April 20, 2016 10:54 AM
    Wednesday, April 20, 2016 9:40 AM

All replies

  • Hi Admir,
    There are a lot of methods to transfer the data to another form, perhaps you could refer to the following methods:
    1. Constructor.(Your way)

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
    
            private void ShowChild()
            {
                FrmChild child = new FrmChild(this);
                child.Show();
            }
            //child form
            FrmParent paraForm = new FrmParent();
            public FrmChild(FrmParent frm)
            {
                InitializeComponent();
                paraForm = frm;
            }
            private void Button_click(object sender, EventArgs e)
            {
                if (paraForm != null)
                {
                    int count = paraForm.dgvParent.Rows.Count;
                }
            }
    

    2. Owner.

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
    
            private void ShowChild()
            {
                FrmChild child = new FrmChild();
                child.Show(this);
            }
            //child form
            private void Button_click(object sender, EventArgs e)
            {
                if (this.Owner != null)
                {
                    int count = (this.Owner as FrmParent).dgvParent.Rows.Count;
                }
            }
    

    3. OpenForms.

            //parent form
            public DataGridView dgvParent { get { return this.dataGridView1; } }
            //child form
            private void Button_click(object sender, EventArgs e)
            {
                if (Application.OpenForms["frmParent"] != null)
                {
                    int count = (Application.OpenForms["frmParent"] as FrmParent).dgvParent.Rows.Count;
                }
            }

    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Proposed as answer by Andy ONeill Wednesday, April 20, 2016 10:38 AM
    • Marked as answer by admiri92 Wednesday, April 20, 2016 10:54 AM
    Wednesday, April 20, 2016 9:40 AM
  • Thank you man, tried the last one and it worked fine. Thanks again!

    Admir

    Wednesday, April 20, 2016 10:54 AM