传递用C#两个实例化的WinForms之间的数据

传递用C#两个实例化的WinForms之间的数据

本文介绍了传递用C#两个实例化的WinForms之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要形式DOWS计算和打开和关闭由用户创建的项目。

I have a Main Form which dows calculations and opens and closes projects created by a user.

当用户点击在文件打开项目按钮,一个名为打开项目的形式打开如下图,它允许用户加载一个项目:

When the user clicks on the Open Project button under File, a form called Open Project opens as below which allows a user to load a project:

现在,我想点击OK后,从这种形式的数据传递到主窗体。

Now, I want to pass the data from this form into the main form after clicking OK.

我遇到的问题是,主窗体已经打开。

The problem I am having is that the Main Form is already open.

任何解决这个问题的办法是hghly AP preciated。

Any solution to this problem would be hghly appreciated.

推荐答案

尝试在创建一个属性打开项目表格

Try to create a Properties in Open Project Form

主要形式

private void openButton_Click(object sender, EventArgs e)
{
    using(var f = new Open_Project_Form())
    {
      f.ProjectReference = projectRefrencetTextBox.Text;
      f.ProjectNo = projectNoTextBox.Text;
      f.ShowDialog();
    }
}

打开项目表格

public string ProjectReference { get; set; }
public string ProjectNo { get; set; }

private void Open_Project_Form_Load(object sender, EventArgs e)
{
    projectRefrenceComboBox.Text = ProjectReference;
    projectNoTextBox.Text = ProjectReference;
}


更新

我misinter preTED的问题。我的previous答案是从的MainForm OpenProjectForm 这个时间是从 OpenProjectForm 的MainForm


UPDATE

I misinterpreted the question. My previous answer is from MainForm to OpenProjectForm this time is from OpenProjectForm to MainForm

主要形式

//Properties for MainForm
public string ProjectReference { get; set; }
public string ProjectNo { get; set; }

private void openButton_Click(object sender, EventArgs e)
{
    using(var f = new Open_Project_Form() { Owner = this })
    {
        f.ShowDialog();
        if (f.DialogResult == DialogResult.OK)
        {
          projectRefrencetTextBox.Text = ProjectReference;
          projectNoTextBox.Text = ProjectNo;
        }
    }
}

打开项目表格:请注意,你有一个 okButton cancelButton

Open Project Form: Take note that you have a okButton and cancelButton

private void Open_Project_Form_Load(object sender, EventArgs e)
{
    okButton.DialogResult = DialogResult.OK;
    this.AcceptButton = okButton;
    this.CancelButton = cancelButton;
}

现在,在 okButton_Click事件

private void okButton_Click(object sender, EventArgs e)
{
   var f = Owner as MainForm;
   if (f == null) return;
   f.ProjectReference = projectRefrenceComboBox.Text;
   f.ProjectNo = projectNoTextBox.Text;
   Close();
}

参考:

  • AcceptButton
  • CancelButton
  • Button.DialogResult

希望它会帮助你。

这篇关于传递用C#两个实例化的WinForms之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:36