locked
change textbox value from another form RRS feed

  • Question

  • I created window form application project in microsoft visual studio.  Using the toolbox i created few button and textbox and then I looked at the code. Isn't there any main method? like
    public static void main(string []args){} found in console application. If there isn't any main method does it call the constructor first?

    second Question:
    I have two form(form1 and form2)
    form1 has a button and a textbox.
    form2 has a button only.
    I wanted to click on button in form2, which will change the textbox.text found in form1.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
           
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f = new Form2();       
                f.Show();
               
            }
        }
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 f = new Form1();
                f.text1.Text = "dsf";
                
            }
        }

    Thursday, March 18, 2010 11:27 AM

Answers

  • Hi,

    You can do the same in different way,
    Check another one, if you need i will go on and on..

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.Show();
            }
        }

    public partial class Form2 : Form
        {
            Form _frm;
            public Form2(Form frm)
            {
                _frm=frm;
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
               Form1 objMain = (Form1)_frm ;
                objMain.textBox1.Text = "Hello";
            }
        }
    In this technique, i pass the form1 to Form2 in constructor, then create an object of existing  Form1 and change there value
    Do let me know, if you want more details
    Cheers, Sanjiv Kumar Sanjiv Blog | LinkedIn
    • Marked as answer by csckid Thursday, March 18, 2010 6:30 PM
    Thursday, March 18, 2010 2:53 PM
  • This should work.(Though not sure if this is the clean way to do it)

    In Form1 While Instantiating Form2 , use below code

     

    Form2 form2 = new Form2();
    form2.Owner = (
    Form)this;
    form2.Show();

    and in Form2's button Click , use below Code (Assumming TextBox1 is the name of textbox ib Form1)

    this.Owner.Controls.Find("TextBox1",true).First().Text="Hi!!!!"
    • Proposed as answer by Abhicha Thursday, March 18, 2010 4:05 PM
    • Marked as answer by csckid Thursday, March 18, 2010 4:05 PM
    Thursday, March 18, 2010 1:59 PM

All replies

  • In Windows Form Application , there is a Program.cs file which calls Application.Run() which starts the Execution of code. and then Main form's Constructor gets called. Regarding Changing TextBox's values , you should be able to  change the value by referring the "calling WebForm's" instance and Changing text Box value.
    Thursday, March 18, 2010 11:43 AM
  • main method is in the Program.cs.

    and to communicate forms there exists many methods

    you can pass one form another forms constuctor and make the textbox public

    and you can change the text value

    or you can write a singleton class has an event mechanism triggers events in classes
    Thursday, March 18, 2010 11:43 AM
  • This should work.(Though not sure if this is the clean way to do it)

    In Form1 While Instantiating Form2 , use below code

     

    Form2 form2 = new Form2();
    form2.Owner = (
    Form)this;
    form2.Show();

    and in Form2's button Click , use below Code (Assumming TextBox1 is the name of textbox ib Form1)

    this.Owner.Controls.Find("TextBox1",true).First().Text="Hi!!!!"
    • Proposed as answer by Abhicha Thursday, March 18, 2010 4:05 PM
    • Marked as answer by csckid Thursday, March 18, 2010 4:05 PM
    Thursday, March 18, 2010 1:59 PM
  • Use delegates , create an event handler for that button click in form2 and use click event delegegate in form1. It is the best way to handle such situation

     

    Here is some good article on this C# Events Lesson 2: Passing data to a consumer

     

    Using delegates to communication between windows forms


    Thanks Mike --------Please mark as answer if it is useful----------
    • Proposed as answer by dan001 Monday, May 9, 2011 12:13 PM
    Thursday, March 18, 2010 2:11 PM
  • Hi,

    You can do the same in different way,

    Check this...

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.Show();
            }
        }

    public partial class Form2 : Form
        {
            Form _frm;
            public Form2(Form frm)
            {
                _frm=frm;
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Control[] cl = _frm.Controls.Find("textBox1", true);
                TextBox textBox1 = (TextBox)cl[0];
                textBox1.Text = "hello";
            }
        }
    In this technique, i pass the form1 to Form2 in constructor, then find any control of Form1 and change there value
    Do let me know, if you want more details
    Cheers, Sanjiv Kumar Sanjiv Blog | LinkedIn
    Thursday, March 18, 2010 2:49 PM
  • Hi,

    You can do the same in different way,
    Check another one, if you need i will go on and on..

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.Show();
            }
        }

    public partial class Form2 : Form
        {
            Form _frm;
            public Form2(Form frm)
            {
                _frm=frm;
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
               Form1 objMain = (Form1)_frm ;
                objMain.textBox1.Text = "Hello";
            }
        }
    In this technique, i pass the form1 to Form2 in constructor, then create an object of existing  Form1 and change there value
    Do let me know, if you want more details
    Cheers, Sanjiv Kumar Sanjiv Blog | LinkedIn
    • Marked as answer by csckid Thursday, March 18, 2010 6:30 PM
    Thursday, March 18, 2010 2:53 PM