将值从一个表单传递到另一个表单

时间:2011-10-25 08:23:22

标签: c# .net winforms

我有两个名为form1form2的表单:

  • form1labelbutton组成。
  • form2textBoxbutton
  • 组成

当我点击form1按钮时,会显示form2。点击textBox中的按钮后,form1.label中的所有输入都应写回form2
我有下面的代码,但它不起作用。

// Code from Form 1
public partial class Form1 : Form
{
    public void PassValue(string strValue)
    {
        label1.Text = strValue;
    }
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2();
        objForm2.Show();
    }

}

// Code From Form 2

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 objForm1 = new Form1();
        objForm1.PassValue(textBox1.Text);
        this.Close();
    }
}

截图:

Application screenshot

我怎么能意识到这一点?

9 个答案:

答案 0 :(得分:16)

您无法访问从中创建form2的form1。在form2 button1_Click中,您创建Form1的新实例,这与初始不同。您可以将form1实例传递给form2构造函数,如:

   // Code from Form 1
 public partial class Form1 : Form
{
    public void PassValue(string strValue)
    {
        label1.Text = strValue;
    }
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2(this);
        objForm2.Show();
    }

}

// Code From Form 2

public partial class Form2 : Form
{
    Form1 ownerForm = null;

    public Form2(Form1 ownerForm)
    {
        InitializeComponent();
        this.ownerForm = ownerForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {       
        this.ownerForm.PassValue(textBox1.Text);
        this.Close();
    }
}

答案 1 :(得分:5)

如其他帖子中提到的那样,您将无法通过创建Form1的新实例来引用原始Form1。您可以将Form1传递给Form2的构造函数或将Form2的文本公开为公共属性,但我通常更喜欢使用委托来保持松耦合。

// Code from Form 1
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2();
        objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
        objForm2.Show();
    }
    public void objForm2_PassValue(string strValue)
    {
        label1.Text = strValue;
    }
}

// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
    public event PassValueHandler PassValue;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (PassValue != null)
        {
            PassValue(textBox1.Text);
        }
        this.Close();
    }
}

答案 2 :(得分:2)

当你这样做时:

Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);

...您正在创建新的Form1并在错误的PassValue对象上调用Form1方法。相反,你可以这样做:

public partial class Form1 : Form
{
    // This is the text that will be entered in form2
    public String form2text;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Show form2
        Form2 objForm2 = new Form2(this);
        objForm2.ShowDialog();
        // When form2 is closed, update the label text on form1
        label1.Text = form2text;
    }
}

public partial class Form2 : Form
{
    // This is the instance of Form1 that called form2
    private Form1 form1caller;

    public Form2(Form1 form1caller)
    {
        InitializeComponent();
        this.form1caller = form1caller;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Pass the textBox value to form1 before closing form2
        form1caller.form2text = textBox1.Text;
        this.Close();
    }
}

答案 3 :(得分:2)

我刚试过这段代码,但它确实可以帮到你。

以下第一种形式(Form1)类型:

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2(textBox1.Text);
        f.ShowDialog();
    }
}

以第二种形式(Form2)使用以下代码:

public partial class Form2 : Form
{
    public Form2( string st)
    {
        InitializeComponent();
        textBox1.Text = st;
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
}

答案 4 :(得分:1)

你可以这样做:

class Form2
{
    public string ReturnedText = "";

    private void button1_Click(object sender, EventArgs e)
    {
        ReturnedText = textbox1.Text;
        Close();
    }
}

并在form1中

Form2 objForm2 = new Form2();
objForm2.ShowDialog();
string ret = objForm2.ReturnedText;

答案 5 :(得分:0)

您应该将form1上的引用传递给form2,而不是在此代码中创建新实例:

private void button1_Click(object sender, EventArgs e)
{
    Form1 objForm1 = new Form1(); // ← this is another form1, not that you see
    objForm1.PassValue(textBox1.Text);
    this.Close();
}

答案 6 :(得分:0)

这就是你要做的事情:

// Code from Form 1
public partial class Form1 : Form
{
    public string MyValue { get; set; }

    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2();
        objForm2.textBox1.Text = MyValue;
        objForm2.MainForm = this;
        objForm2.Show();
    }
}

// Code From Form 2

public partial class Form2 : Form
{
    public Form1 MainForm { get; set; }
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MainForm.MyValue = textBox1.Text;
        MainForm.Show();
        this.Close();
    }
}

答案 7 :(得分:0)

我通常接近这个要求的方式如下:

我在Form2类上放置一个公共属性:

public string ValueFromForm1 { get; set; }

//In the constructor, or other relevant method, I use the value
public Form2()
{
  form2LabelToDisplayForm1Value.Text = ValueFromForm1;
}

为了向Form1返回一些东西,你需要向Form1类添加一个公共属性来接收值,然后将表单的引用发送到Form2,这样Form2就可以设置值:

//Add reference property to Form2 class
public Form1 CallingForm { get; set; }

//Form2 can access the value on Form1 as follows:
private someMethod()
{
  this.CallingForm.ValueFromForm2 = "Info coming from form 2";
}

然后

//Add public property to Form1 class
public string ValueFromForm2 { get; set; }

//When Form2 is created, set the reference property
Form2 objForm2 = new Form2();
objForm2.CallingForm = this;
objForm2.Show();

由于Form2现在引用了创建的Form1,因此无需在Form2中的任何位置调用新的Form1()。所有Form2必须做的是在引用上设置值,然后自行关闭。

答案 8 :(得分:0)

表格1代码......: -

namespace Passing_values_from_one_form_to_other    
{

    public partial class Form1 : Form
    {
        string str;
        private String value1;//taking values from form no _of_test_cases

        public string value
        {
            get { return value1; }
            set { value1 = value; }
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {


            textBox1.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
            str = f2.passvalue;
        }
    }
}

表格2代码......: -

namespace Passing_values_from_one_form_to_other
{

 public partial class Form2 : Form

    {
         private string str;
        public string passvalue
        {
            get { return  str; }
            set { str = value; }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Btn_Ok1_Click(object sender, EventArgs e)
        {

            passvalue = textBox1.Text;
            this.Close();

        }
    }
}

直接执行它你会得到清晰的图片....同样,你可以将值从一个表格传递给其他表格......

如果您遇到任何问题,请发表评论......

希望这会有所帮助......

或者你可以推荐这个视频......

http://www.youtube.com/watch?v=PI3ad-TebP0