I tried to change the text of a textbox from another form and was unsuccessful.
I have zip the complete program.

please help

deleted

and looking at the program u added.... u might want to add a textbox mayb on the 2nd form :)

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

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".

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");
        }
    }

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

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");

        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.