2017-06-05 58 views
0

我一直有這個問題的麻煩,我想我需要學習如何工作,我有FormMain(我的主窗體)和第二個窗體(FormAddUrls)當我打開form2(FormAddUrls)我想要通過multitextbox值返回到主窗體(FormMain)在窗體之間傳遞txtBox值

我知道在VB中就像說:FormMain.txtBoxUrls.Text = finalOutput;但在C#中並不那麼容易。

(Form1上) - FormMain

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Windows.Forms; 
using WraithProjectCreator; 
using IronPdf; 
using System.Text; 

namespace GSAProjectCreator 
{ 
    public partial class FormMain : Form 
    { 
     private IniParser m_Parser = null; 

     public FormMain() 
     { 
      InitializeComponent(); 
     } 

     private void btnShowUrlsForm_Click(object sender, EventArgs e) 
     { 
      FormAddUrls fau = new FormAddUrls(); 
      fau.Show(); 
     } 

    } 
} 

(窗口2) - FormAddUrls

using GSAProjectCreator; 
using System; 
using System.Text; 
using System.Windows.Forms; 

namespace WraithProjectCreator 
{ 
    public partial class FormAddUrls : Form 
    { 
     public FormAddUrls() 
     { 
      InitializeComponent(); 
     } 

     private void btnAddUrls_Click(object sender, EventArgs e) 
     { 
      StringBuilder builder = new StringBuilder(); 
      builder.Append("{"); 
      foreach (string line in txtBoxURLsMass.Lines) 
      { 
       //Helpers.returnMessage(line); 
       builder.Append(line + "|"); 
      } 
      builder.Append("}"); 
      string finalOutput = ""; 
      if (builder.ToString().Contains("|}")) { 
       finalOutput = builder.ToString().Replace("|}", "}"); 
      } 
      //FormMain.txtBoxUrls.Text = finalOutput; 
      this.Close(); 
     } 

    } 
} 

我已經中省略了很多的Form1的代碼留下的基本結構,我試圖回傳finalOutput從form2到form1(txtBoxUrls.Text)文本框,任何幫助將不勝感激。

回答

1

這取決於你想如何使用主窗體 - 你可以有一個靜態屬性,你可以設置(這將更新文本的所有主窗體實例)。

如果您只有一個主窗體,並在創建FormAddUrls對象時對其進行引用,那麼您應該將構造函數更改爲接受MainForm(或儘可能泛型),將引用存儲爲並單擊按鈕時更新其上的屬性:

public partial class FormAddUrls : Form 
{ 
    private readonly Form _parentForm; 
    public FormAddUrls(MainForm parent) 
    { 
     _parentForm = parent; 
     InitializeComponent(); 
    } 

    private void btnAddUrls_Click(object sender, EventArgs e) 
    { 
     StringBuilder builder = new StringBuilder(); 
     builder.Append("{"); 
     foreach (string line in txtBoxURLsMass.Lines) 
     { 
      //Helpers.returnMessage(line); 
      builder.Append(line + "|"); 
     } 
     builder.Append("}"); 
     string finalOutput = ""; 
     if (builder.ToString().Contains("|}")) { 
      finalOutput = builder.ToString().Replace("|}", "}"); 
     } 
     if(_parentForm != null) 
      _parentForm.txtBoxUrls.Text = finalOutput; 
     this.Close(); 
    } 

} 
+0

謝謝你的幫助。 – colinreedy674