私はForm1とForm2という2つのフォームを持っています。2つのフォーム間の変数の受け渡し、マルチスレッドC#
Form1には、Form2を呼び出して別のスレッドで実行するボタンがあります。
Form2には3つのチェックボックスがあります。ユーザーが[追加]ボタンをクリックすると、文字列が生成されます。
私の質問は、Form1に文字列を渡して、それをrichtextboxに追加する方法です。
ありがとうございました。
を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();
}
}
}
2番目のフォームを別のスレッドで実行しようとする理由を説明できますか? – Steve
@ Steveこれは単なるサンプルなので、できるだけシンプルにしようとしました。私は処理に時間がかかり、GUIのフリーズを避けるために別のスレッドで実行する必要がある重い作業があります。私はこのサンプルのための解決策を見つけることができます、私は私の実際のアプリでそれを適用することができます。 –
Form2は必ずフォームかバックグラウンドスレッドですか? http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c –