2011-02-23 57 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

相关问题