【问题标题】:passing data between two forms using properties使用属性在两个表单之间传递数据
【发布时间】:2011-02-23 07:45:05
【问题描述】:

我在 c# 中的 2 个窗口窗体之间传递数据。 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 接收到的文本。

【问题讨论】:

  • @sqlchild,不要这样,使用事件和委托,这是正确的做法,不要通过表单暴露属性
  • 您已经发布过一次这个问题。如果您想添加更多信息而不是发布新信息,请编辑您的原始问题。
  • 你的编码风格非常非常丑陋。如果你认为有人会使用你的代码,你必须遵守常见的 .net 编码约定msdn.microsoft.com/en-us/library/ms229045.aspx
  • @gov,使用属性有什么危害?

标签: c# winforms


【解决方案1】:

我知道这是一个(真的)老问题,但见鬼..

对此的“最佳”解决方案是拥有一个“数据”类来处理您需要传递的任何内容:

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 非常适合简单的表单)

【讨论】:

  • 啊,不是/*block comments*/
  • 你让我发笑,@Saggio。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多