5
Contributors
7
Replies
16
Views
6 Years
Discussion Span
Last Post by samee1985
0

add a constructer to the 2nd form. which accepts a string value.

then instead doing this..

form2 f = new form2();

do this

form2 f = new form2(text1.Text); //the textbox whose string you want to send to the other form

and in the constructor just add this code

textbox1.Text = txt  //txt being the variable name of the string it gets

attachment added of the latter example

Edited by phoenix911: n/a

Attachments
0

let me explain completely what I wanted.
I wanted to click on button on form1 and open the form2.
form2 has a button, when the button of this form2 is clicked the textbox in form1 changes it text to "dsf".

1

If you wish to change values on your instance of Form1 from Form2, then Form2 will need to be given a handle to that instance. The following code shows how to pass and store a handle in Form2's constructor:

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

        //button handler to create new form 2
        private void btnForm2_Click(object sender, EventArgs e)
        {
            //pass a reference to this instance of Form1
            Form2 newForm = new Form2(this);
            newForm.Show();
        }

        //public method to change textbox text value
        public void WriteToTextbox(string inputText)
        {
            this.txtForm1Text.Text = inputText;
        }
    }

    public partial class Form2 : Form
    {

        //default constructor
        public Form2()
        {
            InitializeComponent();
        }

        //overloaded constructor with handle to Form1
        public Form2(Form1 frm1Handle)
        {
            //its important to include this in your custom constructors
            //it calls the designer generated code to create the controls on your form
            InitializeComponent();

            HandleToForm1 = frm1Handle;
        }

        //local variable to store handle to Form1
        private Form1 HandleToForm1;

        //when button on form 2 is clicked the text is written to Form1 via the handle
        private void btnWriteToForm1_Click(object sender, EventArgs e)
        {
            HandleToForm1.WriteToTextbox("This came from Form2");
        }
    }
0

if we want to create many text boxes ,but we don't know the exact number then how can we create

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


        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(this);
            text1.Text = "from form 1";

            f.Show();

        }

        public void changeText(string text)
        {
            text1.Text = text;
        }
    }




 public partial class Form2 : Form
    {
        private Form1 fr1;

        public Form2(Form1 fr1)
        {
            InitializeComponent();
            this.fr1 = fr1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Form1 f = new Form1();
            //f.text1.Text = "dsf";
            fr1.changeText("dsf");

        }
    }

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.