2

NewForm当单击新表单中的按钮时,我有一个并且我需要它在我的主表单中执行某些操作。

    public Newform()
        {
            InitializeComponent();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dontsave_click(object sender, EventArgs e)
        {

        }
    }
}

我有一个 dontsave按钮,当单击并关闭新窗体时,我需要它来清除主窗体中的文本框。

4

4 回答 4

1

在 MainForm.cs

public partial class MainForm : Form
{
    NewForm frm2;

    public MainForm()
    {
        InitializeComponent();

        frm2 = new NewForm();
        frm2.Show();
        frm2.dontSaveButton += new DontSaveButtonHandler(frm2_dontSaveButton);
    }

    void frm2_dontSaveButton()
    {
        textBox1.Clear();
        frm2.Close();

    }

}

在 NewForm.cs

public delegate void DontSaveButtonHandler();

public partial class NewForm : Form
{
    public event DontSaveButtonHandler dontSaveButton;

    public NewForm()
    {
        InitializeComponent();
    }

    private void btnDontSave_Click(object sender, EventArgs e)
    {
        if (dontSaveButton != null)
        {
            dontSaveButton();
        }
    }
}

我想我的答案应该是什么。使用委托是一种很好的做法。

于 2013-11-14T01:34:59.140 回答
1

我强烈建议不要盲目地将一种形式传递给另一种形式的构造函数。相反,公开子窗体的一些属性事件:

public Form1() {
   var childForm = new ChildForm();
   childForm.DontSave += // event handler
}

class ChildForm : Form {

   public event EventHandler DontSave {
      add { dontSaveButton.Click += value; }
      remove { dontSaveButton.Click -= value; }
   }

}
于 2013-11-13T23:29:25.237 回答
1

这将使您开始:

using System;
using System.Windows.Forms;

// Form1 code.
namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
      Form2 frm2 = new Form2();  // Instantiate your form2 object.
      public Form1()
      {
         InitializeComponent();
         frm2.Show();  // Show the form.
      }

      private void button_save_Click(object sender, EventArgs e)
      {
         SaveFileDialog saveDlg = new SaveFileDialog();
         saveDlg.ShowDialog();  // This shows a 'Save' dialog.

         if (saveDlg.ShowDialog() == DialogResult.OK)  // Capture user input from the dialog.
         {
            // do some work here
         }
      }

      private void dontsave_Click(object sender, EventArgs e)
      {
        frm2.ClearTextBox(frm2); // Call the 'ClearTextBox' function from form2.
      }

      private void cancel_Click(object sender, EventArgs e)
      {
        this.Close(); // NOTE:  Probably better to use Application.Exit() here.
      }
   }
}    

//Form2 code.
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public partial class Form2 : Form
   {
      public Form2()
      {
         InitializeComponent();            
      }

      public void ClearTextBox(Form form) // Pass a form as an overload.
      {
         textBox1.Text = ""; // Clear the textbox.
      }
   }
}
于 2013-11-13T23:13:55.673 回答
1

创建 NewForm 时,您需要:

1)创建一个重载构造函数来接受你的父表单 2)有一个公共属性,它包含对你的父表单的引用,然后最终显示你的 NewForm

然后当您按下“dontsave”时 - 只需引用父表单并清除文本框,并确保文本框属性是公共的或最好是方法调用(不要从其他表单完全访问 UI 控件!)

于 2013-11-13T23:14:57.477 回答