简体   繁体   English

传递“不是新对象”作为参数

[英]Passing “not a new” object as parameter

Probably a simple one, I am just learning C#. 可能很简单,我只是在学习C#。

I am using VS2013, having two forms. 我正在使用VS2013,具有两种形式。

Goal: A textbox has a default value. 目标:文本框具有默认值。 At the time of a button (still on form1) is pressed, pass the new, entered value from the textbox on form1, from form1 to form2. 按下按钮时(仍在form1上),将新输入的值从form1的文本框中传递到form1。

I have one textbox on Form1 and on Form2 a button, that is telling me what's in the textbox of Form1. 我在Form1上有一个文本框,在Form2上有一个按钮,它告诉我Form1的文本框中有什么。 This is all right, but I am handling TextChangedEvent so if I modify the text, the creation of a new object of Form1 will result in having a new instance, thus the changed text will be wiped out. 没关系,但是我正在处理TextChangedEvent,因此,如果我修改文本,则Form1的新对象的创建将导致具有新的实例,因此将清除掉已更改的文本。 It remains the default value. 它仍然是默认值。

Question: how to reference to the current value of the object, not create a new instance and have default values? 问题:如何引用对象的当前值,而不创建新实例并具有默认值?

Form1: Form1中:

public partial class Form1 : Form
{
    public string value { get; set; }

    public Form1()
    {
        Application.EnableVisualStyles();
        InitializeComponent();
        this.textBox1.Text = "Default";
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        value = this.textBox1.Text;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        switch (comboBox2.SelectedIndex)
        {
            case -1:
                MessageBox.Show("You didn't choose anything.");
                break;
            case 0:
                MessageBox.Show("Value of variable: " + value); // Here it's the new value, NOT "Default"
                Form2 form_Form2 = new Form2();
                form_Form2.ShowDialog();
                break;
        }
    }
}

Form2: 窗体2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form1 getdata = new Form1();
        string value = getdata.value;
        if (checkBox1.Checked)
        {
            MessageBox.Show("Value: " + value); // Here it's "Default" :(
        }
        else if (!checkBox1.Checked)
        {
            MessageBox.Show("Value: " + value); // TBD
        }
    }
}

The solution is to inject a reference to Form1 into Form2 . 解决方案是将对Form1的引用注入到Form2 You could do this like: 您可以这样做:

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

    public Form1 OtherForm { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        string value = OtherForm.value;
        ...

Then in the sction of code where you create the Form1 and Form2 instances, do something like: 然后在创建Form1Form2实例的代码部分中,执行以下操作:

Form1 form1 = new Form1();
Form2 form2 = new Form2();
form2.OtherForm = form1;

I would just create a variable on form 2 and a method that changes it, then when the button is pressed on form 1 do something like form_form2.someMethod(value) 我只是在表单2上创建一个变量,然后更改它的方法,然后在表单1上按下按钮时,执行诸如form_form2.someMethod(value)之类的操作。
but i'm not sure that's the recommended way to handle it. 但是我不确定这是处理它的推荐方法。

you would also have to change the scope of form_form2 i believe. 您还必须更改form_form2的范围,我相信。

Don't create a new Form1. 不要创建新的Form1。 Pass your Value to your Form2. 将您的价值传递给Form2。 I modified your code a little bit: 我对您的代码做了一些修改:

Form1 : 表格1

namespace GUITest
{
  public partial class Form1 : Form
  {
    private string _value = "Default"; // Create a private field with your Default Value

    public string Value
    {
      get
      {
        return _value;
      }
      set
      {
        _value = value
      }
    }

    public Form1()
    {
      Application.EnableVisualStyles();
      InitializeComponent();
      this.textBox1.Text = Value;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      Value = this.textBox1.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      switch (comboBox2.SelectedIndex)
      {
        case -1:
          MessageBox.Show("You didn't choose anything.");
          break;
        case 0:
          MessageBox.Show("Value of variable: " + value); // Here it's the new value, NOT "Default"
          Form2 form_Form2 = new Form2(Value); // Pass the Value to the Constructor of Form2
          form_Form2.ShowDialog();
          break;
      }
    }
  }
}

Form2 窗体2

namespace GUITest
{
  public partial class Form2 : Form
  {

    private string _value; // Again your field but this time without your default Value

    public Form2(string myVal)
    {
      InitializeComponent();
      _value = myVal;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      if (checkBox1.Checked)
      {
        MessageBox.Show("Value: " + _value); // Here it's now not anymore "Default" :)
      }
      else // Removed redundant if
      {
        MessageBox.Show("Value: " + _value); // TBD
      }
    }
  }
}

And something general: Even if it's only you who is working on this project... Give your controls self explaining names ;) It'll make your developer life much easier :) 和一般的事情:即使只有您来从事这个项目,也可以让控件自我解释名称;)这将使您的开发人员的生活更加轻松:)

Code should work. 代码应该起作用。 I couldn't test it at the moment. 我目前无法测试。 If errors occur then pls comment! 如果发生错误,请发表评论! :) :)

Source: My own know-how ^^ 资料来源:我自己的专业知识^^

Since you're using ShowDialog() , simply grab the value off your Form2 instance when it returns execution back to Form1. 由于您使用的是ShowDialog() ,因此当Form2实例将执行返回给Form1时,只需从Form2实例中获取值即可。 Form2 does not need any kind of reference to Form1 for this to work... 窗体2 不需要任何形式的参考到Form1这个工作...

So you need to do two things: 因此,您需要做两件事:

  1. Somehow pass the current value to Form2 (via the Constructor is a common way and has already been shown). 以某种方式将当前值传递给Form2(通过构造函数是一种常见的方式,并且已经显示出来)。
  2. Retrieve the new value off Form2 if "OK" was returned. 如果返回“ OK”,则从Form2检索新值。 A property in Form2 would be best. Form2中的属性是最好的。

Here are those two things put together: 这是这两件事的总和:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        this.textBox1.Text = "Default";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        switch (comboBox2.SelectedIndex)
        {
            case -1:
                MessageBox.Show("You didn't choose anything.");
                break;
            case 0:
                Form2 form_Form2 = new Form2(this.textBox1.Text);
                if (form_Form2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.textBox1.Text = form_Form2.Value;
                }
                break;
        }
    }

}

public partial class Form2 : Form
{

    private Form2() { }

    public string Value
    {
        get
        {
            return this.textBox1.Text;
        }
    }

    public Form2(string currentValue)
    {
        InitializeComponent();
        this.textBox1.Text = currentValue;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM