0

我有一个主窗体,用于计算并打开和关闭用户创建的项目。

当用户单击 File 下的 Open Project 按钮时,将打开一个名为 Open Project 的表单,如下所示,允许用户加载项目:

在此处输入图像描述

现在,我想在单击“确定”后将此表单中的数据传递到主表单中。

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

对此问题的任何解决方案将不胜感激。

4

3 回答 3

1

尝试创建Properties一个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;
}

更新

我误解了这个问题。我之前的回答是从MainFormOpenProjectForm这次是从OpenProjectFormMainForm

主表格

//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;
        }
    }
}

打开项目表格:请注意,您有一个okButtoncancelButton

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

现在,在okButton_Click event

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

参考:

希望它会帮助你。

于 2013-02-09T12:25:41.713 回答
0

可以使用事件吗?创建一个 EventClass 并将值作为事件的结果返回。

于 2013-02-09T12:26:35.813 回答
0

在打开的表单类中创建一个事件:

public event EventHandler<ProjectDetailsArgs> ProjectDetailsSubmitted;
    public class ProjectDetails: EventArgs
    {
        public string projectReference{ get; set; }
        public string projectNo{get;set;}
        //you can add more prop.s here
    }

On your Ok button click event add 

if (ProjectDetailsSubmitted != null)
                {
                    ProjectDetailsArgs argss = new ProjectDetailsArgs();
                    argss.projectReference = projectRefrencetTextBox.Text;
                    argss.projectNo = projectNoTextBox.Text;
                    ProjectDetailsSubmitted(null, argss);
                }

在您的主窗体中为其创建一个处理程序:

  childform.ProjectDetailsSubmitted+=new EventHandler<ProjectDetailsArgs>project_detailsSubmitted);

                public void project_detailsSubmitted(object sender, ProjectDetailsArgs e)
                {
         //Do Your work
                }
于 2013-02-09T12:43:04.140 回答