我有两种形式:Form1和Form2。
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();
}
}
}
窗体2
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();
}
}
}
答案 0 :(得分:1)
您可以向以这种方式声明的Form2类添加一个事件
private void button1_Click(object sender, EventArgs e)
{
.....
string selected_string = clean_string(string.Join(",", selected_array));
if(MessageReady != null)
MessageReady(selected_string);
.....
}
当您的Form2准备好发送回有兴趣了解它的客户的消息时,请调用MessageReady事件
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);
}
最后一步是在构建Form2实例时从Form1订阅事件
var rootSubjectsSubjectAdminWordsWord = {
name: 'r.s.s.a.w.w',
template: "<div ui-view='wordData'></div><div ui-view='wordForms'></div>",
url: '/:wordId',
};
var rootSubjectsSubjectAdminWordsWordDelete = {
name: 'r.s.s.a.w.w.delete',
views: {
"wordData@r.s.s.a.w.w": {
templateProvider: ['$templateCache', ($templateCache) => {
return $templateCache.get('/app/admin/word/wordData.html');
}]
},
"wordForms@r.s.s.a.w.w": {
templateProvider: ['$templateCache', ($templateCache) => {
return $templateCache.get('/app/admin/word/wordForms.html');
}]
}
},
url: '/delete',
};
var rootSubjectsSubjectAdminWordsWordEdit = {
name: 'r.s.s.a.w.w.edit',
views: {
"wordData@r.s.s.a.w.w": {
templateProvider: ['$templateCache', ($templateCache) => {
return $templateCache.get('/app/admin/word/wordData.html');
}]
},
"wordForms@r.s.s.a.w.w": {
templateProvider: ['$templateCache', ($templateCache) => {
return $templateCache.get('/app/admin/word/wordForms.html');
}]
}
},
url: '/edit',
};