-2

I have 2 forms: Form1 and Form2.

Form1 contains a button to call Form2 and run it on a another thread.

Form2 contains 3 checkboxes. When an user clicks on Add button, it generate a string.

My question is how can I pass the string to Form1 then add it to the richtextbox?

Thanks.

enter image description here

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

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

        private void call_form_2()
        {
            for (int i = 0; i<10; i++) { 
            Form2 inst_form2 = new Form2();
            inst_form2.ShowDialog();
            }
        }

        private void f1_but_01_Click(object sender, EventArgs e)
        {
            Thread extra_thread_01 = new Thread(() => call_form_2());
            extra_thread_01.Start();            
        }
    }
}

Form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PassingData2Forms
{

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

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private string clean_string(string process_string)
        {
            process_string = process_string.Replace(",,", ",");
            process_string = process_string.Trim(new char[] {','});
            return process_string;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] selected_array = new string[3];

            if (checkBox1.Checked == true)
            {
                selected_array[0] = "Summer";
            }

            if (checkBox2.Checked == true)
            {
                selected_array[1] = "Spring";
            }

            if (checkBox3.Checked == true)
            {
                selected_array[2] = "Fall";
            }

            string selected_string = clean_string(string.Join(",", selected_array));

            //---------------------------------------------------------------
            // How can I pass "selected_string" to RichTextBox in Form1 here?
            //---------------------------------------------------------------

            Close();
        }
    }
}
Louis Tran
  • 1,154
  • 1
  • 26
  • 46
  • Can you explain why you try to run your second form in a different thread? – Steve May 14 '16 at 07:15
  • @Steve Because this is just a sample, I tried to make this as simple as possible. I have a heavy task that requires a lot of time to process and must be run on a different thread to avoid freezing of GUI. If I can find a solution for this sample, I can apply it in my real app. – Louis Tran May 14 '16 at 07:21
  • Is Form2 necessarily a form or just some background thread? See http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – Benji Wa May 14 '16 at 08:06
  • @LouisTran Running your form in a different thread is **not** simple ... Typically, you run all forms, in fact all UI, on the main thread, and do background work on a worker-thread/thread-pool and then communicate the results back. – MicroVirus May 14 '16 at 09:55

1 Answers1

1

You could add an Event to your Form2 class declared in this way

public delegate void onMessageReady(string message);
public event onMessageReady MessageReady;

and when your Form2 has a message ready to send back to the clients interested to know about it call the MessageReady event

private void button1_Click(object sender, EventArgs e)
{
    .....
    string selected_string = clean_string(string.Join(",", selected_array));
    if(MessageReady != null) 
         MessageReady(selected_string);
    .....
}

The last step is to subscribe to the event from your Form1 when you build the instances of Form2

private void call_form_2()
{
    for (int i = 0; i<10; i++) { 
    Form2 inst_form2 = new Form2();
    inst_form2.MessageReady += MessageReceived;
    inst_form2.ShowDialog();
    }
}
private void MessageReceived(string message)
{
        if (form1RichTextBox.InvokeRequired)
           form1RichTextBox.Invoke(new Form2.onMessageReady(messageReady), new object[] {message});
        else
           form1RichTextBox.AppendText(message + Environment.NewLine);
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    If Form2 runs on another thread, you have to invoke the control you're modifying. Also see http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – Benji Wa May 14 '16 at 08:04