【问题标题】:Passing Data form two different forms以两种不同的形式传递数据
【发布时间】:2013-11-18 05:54:30
【问题描述】:

我的目标是能够在 form1 的文本框中输入一个值,然后按 enter(当按下 enter 按钮时,值将传递给一个名为 setvalue 的方法。当按下切换按钮时,表单将隐藏并打开form2。Form2有两个按钮,显示和退出。单击显示时,我需要显示一个消息框,通过调用getvalue方法在form1的文本框中显示数据。

我愿意接受意见。

这是表格一

public partial class Form1 : Form
{
    private int secretValue;

    public  void SetValue (int value){
          secretValue = value;
    }
    public int GetValue ()
    {

       return secretValue;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Visible = true;
        this.Hide();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnEnter_Click(object sender, EventArgs e)
    {

        secretValue = Convert.ToInt32(txtInput.Text);
        SetValue(secretValue);
    }
}

这个表格二

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Environment.Exit(0);
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
           int val = frm1.GetValue();


           MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
    }
}

【问题讨论】:

  • 也许您可以通过构造函数参数将数据传递给表单2。
  • 这个问题之前已经被问过很多次了——你在问这个问题之前是否搜索过 StackOverflow 或查看了建议的链接?

标签: c# winforms


【解决方案1】:

在表单 2 上添加一个构造函数值。它应该如下所示:

public Form2(int secValue)

然后您可以通过传递值在表单 1 上调用它

Form2 frm2 = new Form2(secretValue);

也许您可以将其分配给表单 2 中的全局变量,以便您的代码在所有表单中都可以引用它。您的表格二现在可以是:

public partial class Form2 : Form
{

    int passedValue = 0;

    public Form2(int secValue)
    {
        InitializeComponent();
        passedValue = secValue;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Environment.Exit(0);
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
           MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
    }
}

在我看来,不需要您创建的属性。永远记住,如果您从一个表单更改为另一个表单,您分配给一个类的值将为空。

【讨论】:

    【解决方案2】:

    以下代码将不起作用,因为您正在创建 form1 的新实例并尝试从那里获取值,它只会具有 int 的默认值。

     private void btnShow_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            int val = frm1.GetValue();
        }
    

    您可以做的就是在Form2 中定义一个public property。并在显示Form2之前设置该属性的值,

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
    
        //settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
        frm2.SecrectValueOfForm2 = ValueInForm1;
        frm2.Visible = true;
        this.Hide();
    }
    

    【讨论】:

      【解决方案3】:

      请尝试如下代码:

      public partial class Form1 : Form
      {
         private int secretValue;
         Form2 frm2 = new Form2();
      
      public  void SetValue (int value){
            secretValue = value;
      }
      public int GetValue ()
      {
      
         return secretValue;
      }
      
      public Form1()
      {
          InitializeComponent();
      }
      
      private void button2_Click(object sender, EventArgs e)
      {
      
          frm2 .Show();
          this.Hide();
      }
      
      private void Form1_Load(object sender, EventArgs e)
      {
            frm2.Owner = this;
      }
      
      private void btnEnter_Click(object sender, EventArgs e)
      {
      
          secretValue = Convert.ToInt32(txtInput.Text);
          SetValue(secretValue);
      }
      }
      

      在 Form2 上:

      public partial class Form2 : Form
      {
      
      public Form2()
      {
          InitializeComponent();
      }
      
      private void btnExit_Click(object sender, EventArgs e)
      {
          Environment.Exit(0);
      }
      
      private void btnShow_Click(object sender, EventArgs e)
      {
          Form1 frm1 = new Form1();
             int val = ((Form1 )this.Owner).GetValue();
      
      
             MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
      }
      }
      

      【讨论】:

        【解决方案4】:

        Form2

         public Int32 _txtval { get; set; }
         private void button1_Click(object sender, EventArgs e)
                {
                    MessageBox.Show(_txtval.ToString());
                }
        

        表格1

         private void button1_Click(object sender, EventArgs e)
         {
        
                    Form2 frm2 = new Form2();
                    frm2._txtval = Convert.ToInt32(textBox1.Text);
                    frm2.Visible = true;
                    this.Hide();
          }
        

        【讨论】:

          猜你喜欢
          • 2014-02-09
          • 2018-04-21
          • 2015-09-23
          • 1970-01-01
          • 2017-05-02
          • 1970-01-01
          • 2021-08-06
          • 2013-11-30
          • 2016-09-23
          相关资源
          最近更新 更多