使用属性两种形式之间传递数据 [英] passing data between two forms using properties

查看:123
本文介绍了使用属性两种形式之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#2的Windows窗体之间传递数据。 Form1中的主要形式,其文本框将收到form2_textbox和放大器传递给它的文本;在其文本框(form1_textbox)显示。

I am passing data between 2 windows forms in c#. Form1 is the main form, whose textbox will receive the text passed to it from form2_textbox & display it in its textbox (form1_textbox).

首先,打开Form1中,用一个空的文本框和一个按钮,在点击form1_button,Form2的打开。
在窗体2,我进入了form2_textbox和放大器的文本;然后单击该按钮(form2_button)。在点击这个按钮的事件,它将发送文本到Form1的文本框和放大器; Form1上会聚焦其从窗口2收到一个文本空form1_textbox。

First, form1 opens, with an empty textbox and a button, on clicking on the form1_button, form2 opens. In Form2, I entered a text in form2_textbox & then clicked the button (form2_button).ON click event of this button, it will send the text to form1's textbox & form1 will come in focus with its empty form1_textbox with a text received from form2.

我使用的属性来实现这个任务。
FORM2.CS

I am using properties to implement this task. 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);
    }

    }

}



< HR>

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();

        }
    }

现在,在这种情况下,我要再次显示,以自动获取从窗体2的文本框中的文本在Form1,但我想,当我点击窗体2按钮,文本从窗体2发送到Form1,和放大器;在Form1进来的焦点,其含窗体2收到的文字文本框。

Now, in this case I have to again SHOW the form1 in order to automatically get the text in its textbox from form2, but I want that as I click the button on form2, the text is sent from Form2 to Form1, & the form1 comes in focus, with its textbox containing the text received from Form2.

推荐答案

我知道这是一个(真的)旧的问题,而是地狱。

I know this is a (really) old question, but hell..

这个最佳的解决方案是有一个数据类,将手柄保持任何你需要跨传递:

The "best" solution for this is to have a "data" class that will handle holding whatever you need to pass across:

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



然后有一个背景控制器级,能够处理呼叫,显示/隐藏形式(等)

Then have a background "controller" class that can handle calling, showing/hiding forms (etc..)

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..
    }
}

很显然,在你的表单,你可以有一个非常简单的点击监听器就像..

And obviously, in your form, you can have a very simple click listener that's like..

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



然后,当你有你的神奇惊人的形式,他们可以通过使用会话对象相互之间的事情。如果你想有并发访问,你可能想建立一个互斥信号灯根据您的需求(这您还可以存储会话)内的引用。但也没有理由,你为什么不能有类似的控制器逻辑父对话中来控制它的儿童(不要忘了,的DialogResult 是伟大的,简单的表单)

Then, when you have your magical amazing forms, they can pass things between each other using the session object. If you want to have concurrent access, you might want to set up a mutex or semaphore depending on your needs (which you can also store a reference to within Session). There's also no reason why you cannot have similar controller logic within a parent dialog to control its children (and don't forget, DialogResult is great for simple forms)

这篇关于使用属性两种形式之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆