从一种形式到另一种形式获得价值,反之亦然 [英] get value form one form to another and vice versa

查看:99
本文介绍了从一种形式到另一种形式获得价值,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有两个表单,第一个表单是FORM1,第二个表单是FORM2。

FORM1包含3个文本框,1个按钮,FORM2还包含3个文本框,1个按钮。我想做....

当用户填写FORM1 textboex并单击button1时,所有条目都应显示在FORM2上。反过来plz帮助我。我使用这个代码,但它只有一个方向,plz帮助我

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间打孔
{
public partial class Form1:Form
{
public string RichText { get { return richTextBox1.Text; } set {richTextBox1.Text = value ; }
public string txtbox1 { get { return textBox1.Text; } set {textBox1.Text = value ; }
public string txtbox2 { get { return textBox2.Text; } set {textBox2.Text = value ; }
public string txtbox3 { get { return textBox3.Text; } set {textBox3.Text = value ; }
public Form1()
{
InitializeComponent();
}

private void richTextBox1_Click( object sender,EventArgs e)
{
var form2 = new Form2( this );
form2.Show();
}
}
}





使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间井号
{
公共部分类Form2:表格
{
private Form1 parentForm;

public Form2(Form1表格)
{
InitializeComponent();
this.parentForm = form;
}

private void Form2_Load(object sender,EventArgs e)
{

}

private void button1_Click(object sender,EventArgs e)
{
// if(this.parentForm == null)
// {
// return;
//}
parentForm.RichText = textBox1.Text + Environment.NewLine + textBox2.Text + Environment.NewLine + textBox3.Text;
parentForm.txtbox1 = textBox1.Text;
parentForm.txtbox2 = textBox2.Text;
parentForm.txtbox3 = textBox3.Text;
this.Close();
}
}
}

解决方案

不要这样做!

请参阅此处:在两个表单之间传输信息,第2部分:孩子到父母 [ ^ ]

虽然我怀疑你可能想把它当作在两种形式之间传递信息,第3部分:儿童到儿童 [ ^ ]


为要在表单之间传递的每个变量使用单独的公共属性,或创建一个结构来保存所有v ariables并使用 Public Property 来传递结构。



Form1

*声明属性(下面是日期属性的示例)

 public System.DateTime Stop_Time {
get {Stop_Time = m_stop_time; }
set {m_Stop_Time = value; }
}
System.DateTime m_Stop_Time;



*在实例化Form2之前设置属性

*实例化并显示Form2

* Form2关闭后,使用属性更改Form1中的表单字段



Form2

*在表单加载事件中,使用Form1中的公共属性来初始化Form2的控件

*在关闭事件中,使用Form2的控件来更改Form1的公共属性


I have two form in my project 1st form is FORM1 and 2nd form is FORM2.
FORM1 contain 3 textbox,1 button and FORM2 also contain 3 textbox, 1 button. I want to do....
when user fill FORM1 textboex and click button1 then all entries should be shown on FORM2. and vice versa plz help me.I use this code but its wrking only one direction, plz help me

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace punchout
{
    public partial class Form1 : Form
    {
        public string RichText { get { return richTextBox1.Text; } set { richTextBox1.Text = value; } }
        public string txtbox1 { get { return textBox1.Text; } set { textBox1.Text = value; } }
        public string txtbox2 { get { return textBox2.Text; } set { textBox2.Text = value; } }
        public string txtbox3 { get { return textBox3.Text; } set { textBox3.Text = value; } }
        public Form1()
        {
            InitializeComponent();
        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            var form2 = new Form2(this);
            form2.Show();
        }
    }
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace punchout
{
    public partial class Form2 : Form
    {
        private Form1 parentForm;
        
        public Form2(Form1 form)
        {
            InitializeComponent();
            this.parentForm = form;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (this.parentForm == null)
            //{
               // return;
            //}
            parentForm.RichText = textBox1.Text + Environment.NewLine + textBox2.Text + Environment.NewLine + textBox3.Text;
            parentForm.txtbox1 = textBox1.Text;
            parentForm.txtbox2 = textBox2.Text;
            parentForm.txtbox3 = textBox3.Text;
            this.Close();
        }
    }
}

解决方案

Don''t do it that way!
See here: Transfering information between two forms, Part 2: Child to Parent[^]
Though I suspect you might want to consider this as a form of Transfering information between two forms, Part 3: Child to Child[^]


Use a separate Public Property for each variable you want to pass between forms or create one Structure to hold all the variables and use a Public Property to pass the Structure.

Form1
* Declare the Properties (Below is an example of a date property)

public System.DateTime Stop_Time {
	get { Stop_Time = m_stop_time; }
	set { m_Stop_Time = value; }
}
System.DateTime m_Stop_Time;


* Set the Properties before instantiating Form2
* Instantiate and Show Form2
* After Form2 closes, use properties to change form fields in Form1

Form2
* In Form Load Event, use Public Properties from Form1 to initialize Form2''s controls
* In the Close Event, use Form2''s controls to change Form1''s Public Properties


这篇关于从一种形式到另一种形式获得价值,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆