2016-07-06 39 views
-1

在Form1如何将字符串从新窗体传递给form1 richtextbox?

private void button4_Click(object sender, EventArgs e) 
{ 
    AddText at = new AddText(); 
    at.Show(); 
    richTextBox2.Text = at.text; 
} 

在新的形式

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 test 
{ 
    public partial class AddText : Form 
    { 
     public string text = ""; 

     public AddText() 
     { 
      InitializeComponent(); 
     } 

     private void AddText_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      text = textBox1.Text; 
     } 

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

当我在新的形式,它从分配给textBox1可变文本的文字点击button1

但它没有传递给Form1.richTextBox2。 我想这个问题是我尝试在文本中的按钮单击事件在指定Form1

richTextBox2.Text = at.text; 

但在新的形式按钮Click事件之前发生这种情况。 Form1我应该在richTextBox2的文本中指定文本的位置/方式?

我用ShowDialog()它只有当我关闭新窗体窗口时才起作用。只有当我关闭它时,才会看到richTextBox2中的文字。但是我想看到richTextBox2中的文本,当我单击确定(button1)按钮而不关闭窗体。

回答

2

Form1中

private void button4_Click(object sender, EventArgs e) 
{ 
    AddText at = new AddText(this); 
    at.Show(); 
    richTextBox2.Text = at.text; 
} 

public void SetText(string text) 
{ 
    richTextBox2.Text = text; 
} 

新形式

public partial class AddText : Form 
    { 
    private Form1 _form1; 

    public AddText(Form1 form1) 
    { 
     InitializeComponent(); 
     _form1 = form1; 
    } 

    private void AddText_Load(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     _form1.SetText(textBox1.Text); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
    } 
相关问题