如何在C#中的两种形式之间传递数据?我想在将form1数据传递给form2之后将form2数据传递给form1

时间:2019-05-15 17:15:49

标签: c# visual-studio

此代码有效,但是 我无法弄清楚setForm1()函数。为什么要编写此功能? 有其他解决方案吗?

//form1
namespace prguide1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void UpdateData(string subject, string grade)
        {
            lblGrade.Text = grade;
            lblSub.Text = subject;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.setForm1(this);
            form2.stno = txtStno.Text;
            form2.Show();
        }
    }
}

//form2
namespace prguide1
{
    public partial class Form2 : Form
    {
        public string stno;
        private Form3 frm1;

        public void setForm1(Form1 form1)
        {
            this.frm1 = form1;
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            lblTitle.Text = "Enter details of " + stno;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
           if(txtGrade.Text !="" && txtSub.Text !="")
            {
                frm1.UpdateData(txtSub.Text, txtGrade.Text);
            }
        }
    }
}

0 个答案:

没有答案