在C#中的表单之间来回传递参数

Fra*_*yer 4 c# forms parameters

我正在创建一个应用程序,用户将单击form1上的按钮,这将导致form2显示.然后,用户将在form2上填写聊天,然后单击将关闭form2的按钮,并将参数发送回form1进行处理.我怎么能在C#中做到这一点?我见过人们使用属性来做这件事,但是这些例子还不够清楚.有人可以给出一些示例代码,告诉我如何传递参数吗?我更喜欢属性方法,但只要它有效,我会把它算作答案.

Bra*_*tie 11

简而言之,就像通常那样将表单元素放在第二种形式中.然后,您可以向该表单添加公共访问者,然后您可以从中提取和引用.例如,如果Form2有一个你想撤回的文本字段,你可以:

class Form2
{
  // Form2.designer.cs
  private TextBox TextBox1;

  // Form2.cs
  public String TextBoxValue // retrieving a value from
  {
    get
    {
      return this.TextBox1.Text;
    }
  }

  public Form2(String InitialTextBoxValue) // passing value to
  {
    IntiializeComponent();

    this.TextBox1.Text = InitialTextBoxValue;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在创建表单时稍后访问它(就像OpenFileDialog为Filename等做的那样).

public void Button1_Click(object sender, EventArgs e)
{
  Form2 form2 = new Form2("Bob");      // Start with "Bob"
  form2.ShowDialog();                  // Dialog opens and user enters "John" and closes it
  MessageBox.Show(form2.TextBoxValue); // now the value is "John"
}
Run Code Online (Sandbox Code Playgroud)

对于Int32,Boolean等也可以做同样的事情.如果您想要转换/验证它,或者以其他方式取决于表单的值.

另外,您可以Modifiers在表单设计器中使用该属性,您可以将控件公开,以便可以从外部访问.我个人建议使用访问器,以便您可以验证并确认返回的结果,而不仅仅是转储值(因为此逻辑通常在表单本身中找到,而不是在您要调用/使用Form2的每个实例中)


Syl*_*ond 5

这是我喜欢将参数传递给另一种形式的方法:

为Form1和Form2提供了以下表单设计:

Form1和Form2

在此示例中,Form1上的“ txtBoxForm1”文本框传递到Form2,在此它用于初始化Form2上的“ txtBoxForm2”文本框。用户与Form2进行交互并通过单击[Return to Form1]按钮将其结束后,“ txtBoxForm2”文本框的值将分配给参数(通过引用)返回到Form1上的“ txtBoxForm1”文本框。

只需两个简单步骤即可完成编码:

步骤1)在Form1中,通过重载ShowDialog()方法将参数传递给Form2:

 public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();

        //Step 1) 
        //Display the form passing parameter(s) via overloading 
        //the ShowDialog() method. 
        //In this example the parameter is the 'txtBoxForm1' on Form1.
        // f2.ShowDialog(); is replaced by
        f2.ShowDialog(ref txtBoxForm1); 

    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,参数是通过引用传递的'txtBoxForm1'文本框本身。通过引用传递它的原因在于,它不仅将文本框值传递给Form2,而且还可以在执行Form2时接收并在Form1上显示对该文本框参数应用的任何修改。

我放置了整个Form1类,以表明该类中没有什么比'f2.ShowDialog()'方法调用的重载特别的了。

步骤2)通过重载的ShowDialog()方法接收和返回参数:

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

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

    //Step 2)
    //Receiving and returning parameter(s) via the overloaded ShowDialog() method.
    //This saves the need to have Properties and or fields associated
    //to parameters when overloading the above Form() constructor instead.
    public void ShowDialog(ref TextBox txtBoxForm1)
    {
        //Assign received parameter(s) to local context
        txtBoxForm2.Text = txtBoxForm1.Text;

        this.ShowDialog(); //Display and activate this form (Form2)

        //Return parameter(s)
        txtBoxForm1.Text = txtBoxForm2.Text;
    }
}
Run Code Online (Sandbox Code Playgroud)

我再次提供了完整的表单类(在本例中为Form2)代码,以说明编码干预的局限性。此处不需要其他字段或属性。那是因为我在这里使用了'ShowDialog()'指令,而不是Form1的Form2()构造函数来传递参数。与Form2()构造函数相反,ShowDialog()方法是与Form2的用户交互阶段周围的信封。因此,它的时间允许'(ref txtBoxForm1)'参数完全和充分地代表我们希望发送和接收的参数。

通过使用一组不同的参数(也称为签名)重新声明方法来重载方法是C#.net的强大功能。在这种情况下,它一方面允许将参数添加到“ ShowDialog()”方法的调用中,另一方面,重载的方法不会因为该方法的原始版本而丢失任何内容。 ShowDialog().net框架方法也可以通过“ this.ShowDialog();”执行。指令。

就目前而言,这就是我的意思。

希望这会有所帮助!