2011-02-23 91 views
0

我在c#中的兩個窗體窗體之間傳遞數據。 Form1是主窗體,其文本框將接收從form2_textbox &傳遞給它的文本,並將其顯示在其文本框(form1_textbox)中。使用屬性在兩個窗體之間傳遞數據

首先,打開form1,用空的文本框和按鈕,單擊form1_button,打開form2。 在Form2中,我在form2_textbox &中輸入了一個文本,然後點擊了按鈕(form2_button).ON點擊這個按鈕的事件,它會將文本發送到form1的文本框中form1將以空的form1_textbox爲焦點,並從form2接收文本。

我正在使用屬性來實現此任務。 FORM2.CS

public partial class Form2 : Form 
{ 
    //declare event in form 2 
    public event EventHandler SomeTextInSomeFormChanged; 

    public Form2() 
    { 
     InitializeComponent(); 

    } 
    public string get_text_for_Form1 
    { 
     get { return form2_textBox1.Text; } 
    } 

    //On the button click event of form2, the text from form2 will be send to form1: 

    public void button1_Click(object sender, EventArgs e) 
    { 
     Form1 f1 = new Form1(); 
     f1.set_text_in_Form1 = get_text_for_Form1; 

    //if subscribers exists 
    if(SomeTextInSomeFormChanged != null) 
    { 
     SomeTextInSomeFormChanged(this, null); 
    } 

    } 

} 

Form1.cs的

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

     public string set_text_in_Form1 
     { 
      set { form1_textBox1.Text = value; } 
     } 

     private void form1_button1_Click(object sender, EventArgs e) 
     { 
      Form2 f2 = new Form2(); 
      f2.Show(); 
      f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged); 
     } 

     //in form 1 subcribe to event 
     Form2 form2 = new Form2(); 

     public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e) 
     { 
      this.Focus(); 

     } 
    } 

現在,在這種情況下,我不得不再次顯示,以自動獲得在其文本框中的文本在Form1從form2,但我希望當我點擊form2上的按鈕時,文本從Form2發送到Form1,& form1進入焦點,其文本框包含文本接收從Form2編輯。

+3

@sqlchild,不要這樣做,使用事件和委託,這是正確的做法,不要暴露財產的形式 – kobe 2011-02-23 07:52:10

+0

可能重複[在兩個窗體之間使用屬性傳遞數據](http://stackoverflow.com/questions/5087934/passing-data-between-two- forms-using-properties) – 2011-02-23 07:57:16

+1

您已經發布了此問題一次。如果您想添加其他信息而不是發佈新信息,請修改您的原始問題。 – 2011-02-23 07:57:44

回答

3

我知道這是一個(真的)老問題,但地獄..

這個「最佳」的解決辦法是有一個「數據」類,將手柄保持任何你需要跨傳:

class Session 
{ 
    public Session() 
    { 
     //Init Vars here 
    } 
    public string foo {get; set;} 
} 

然後有背景「控制器」類,它可以處理調用,顯示/隱藏表格(等)

class Controller 
{ 
    private Session m_Session; 
    public Controller(Session session, Form firstform) 
    { 
     m_Session = session; 
     ShowForm(firstform); 
    } 

    private ShowForm(Form firstform) 
    { 
     /*Yes, I'm implying that you also keep a reference to "Session" 
     * within each form, on construction.*/ 
     Form currentform = firstform(m_Session); 
     DialogResult currentresult = currentform.ShowDialog(); 
     //.... 

     //Logic+Loops that handle calling forms and their behaviours.. 
    } 
} 

很顯然,在你的表格,你可以有一個非常簡單的點擊監聽器就像..

//... 
    m_Session.foo = textbox.Text; 
    this.DialogResult = DialogResult.OK; 
    this.Close(); 
//... 

然後,當你有你神奇的神奇表單時,他們可以使用會話對象在彼此之間傳遞事物。如果您想同時訪問,您可能需要根據需要設置mutexsemaphore(您也可以在Session之內存儲引用)。也沒有理由不能在父對話框中使用類似的控制器邏輯來控制它的子項(並且不要忘記,DialogResult對於簡單的表單來說很不錯)

+0

啊,不是'/ *塊註釋* /'! – Saggio 2013-06-20 16:30:22

+0

你讓我輕笑,@Saggio。 – Izzy 2013-06-21 07:32:35

相關問題