Hello! Can somebody help me. It's got to be quite easy but still I can't solve the problem.
I have Form1 then I press the button and create another Form2. If I click the button for example three times there will be three Form2. All the Forms have textBox. What I want is from one Form2 sent the text to anothe Form2.
That's my code. But the only thing that I can do is to send messages between Form1 and Form2

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        List<Form> frm = new List<Form>();
       
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            frm.Add(f);
            f.Owner = this;

           
           string s = this.textBox1.Text;
                f.textBox1.Text = this.textBox1.Text;
            f.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
          
                
               
            }
            
        }
    }
///And for Form2
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 main = this.Owner as Form1;
            if (main != null)
            {
                string s = main.textBox1.Text;
                main.textBox1.Text = this.textBox1.Text;
            }
        }

Recommended Answers

All 5 Replies

You can use a get set modifier ( a static one, so you will not need to initialize a new form1), and one time you read the text from it, 2nd time you set the text from it:

//Form1:
       public static string MyText { get; set; } //this is the point of all here!

        public Form1()
        {
            InitializeComponent();
        }

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

//Form2:
        public Form2()
        {
            InitializeComponent();

            //1st: read the text if exist:
            this.textBox1.Text = Form1.MyText; 
      
            //2nd create an event which will pass characters to get,set modifier:
            this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //set the text:
            Form1.MyText = textBox1.Text;
        }

I am not sure that that's exactly what I need. Your code helps to connect textboxes of form1 and form2 but I want to sent the text from form2 to form2.

I think that maybe there should be created a List of Forms2 so that we can refer to each of Form2

I am not sure that that's exactly what I need. Your code helps to connect textboxes of form1 and form2 but I want to sent the text from form2 to form2.

On a button click or how?

And lets say you open 3 form2.
In one of them you write some text. And when pressing a button, you want this text to be sent to other 2 form2?

On a button click or how?

And lets say you open 3 form2.
In one of them you write some text. And when pressing a button, you want this text to be sent to other 2 form2?

yes, on the button

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.