Tuesday 12 March 2013

how to access controls on another form c#

Form1 with 1 textbox and button
this button is for opening new form (form2)

Form2 with datagridview and button is for sending datagraidview value to textbox on Form1


On Form1
 private void button_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }

On Form2

 public Form2()
        {
            InitializeComponent();
        }

        private Form1 f1 = null;


        public Form2(Form callingForm)
        {
            f1 = callingForm as Form1;
            InitializeComponent();
        }

private void button_Click(object sender, EventArgs e)
        {
            this.f1.TextBox = dataGridView1.SelectedRows[0].Cells["columnname"].Value.ToString();
            this.Close();         
        }