zoukankan      html  css  js  c++  java
  • 在winForm 中实现两个窗体间的数据传递


    一共有4种实现方法
    1. Using constructor
    2. Using objects
    3. Using properties
    4. Using delegates

    这里主要介绍方法3、4
    方法3:

    Step 1: Add a property in form1 to retrieve value from textbox.

     

    public string _sendtxt
            {
                
    get{return this.txtBox1.Text.ToString().Trim();}
            }


    Step 2: Add a property in form2 to set the labels’ text

    public string _recevicetxt
            {
                
    set { this.label1.Text=value; }
            }


    Step 3:
    In form1’s button click event handler add the following code.

    private void btnsend_Click(object sender, EventArgs e)
            {
                Form2 frm2 
    = new Form2();
                frm2.Show();
                frm2._recevicetxt 
    = _sendtxt;
            }

    方法4:

    Step 1: Add a delegate signature to form1 as below

            public delegate void delPassData(TextBox text);


    Step 2: In form1’s button click event handler instantiate form2 class and delegate. Assign a function in form2 to the delegate and call the delegate as below

    private void btnsend_Click(object sender, System.EventArgs e)
            {
                Form2 frm 
    = new Form2();
                delPassData del 
    = new delPassData(frm.funData);
                del(
    this.textBox1);
                frm.Show();
            }

     

    Step 3: In form2, add a function to which the delegate should point to. This function will assign textbox’s text to the label.

     

    public void funData(TextBox txtForm1)
            {
                label1.Text 
    = txtForm1.Text;
            }


    详细介绍请参考:http://www.codeproject.com/useritems/pass_data_between_forms.asp

    FrmM:   txt1、txt2、txt3的Modify设为:Public
     private void btn1_Click(object sender, EventArgs e)
            {
                FrmS fs = new FrmS();
                fs.Owner = this;//Owner:拥有当前窗体的窗体
                fs.ShowDialog();
            }

    FrmS:
     private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                FrmM fm = (FrmM)this.Owner;
                fm.txt1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                fm.txt2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                fm.txt3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                this.Close();
            }

  • 相关阅读:
    ROM定制开发教程-Android adb命令用法与实例解析
    Android内存优化—dumpsys meminfo详解
    adb命令查看手机应用内存使用情况
    【特征检测】BRISK特征提取算法
    焦虑症
    基于Lua的游戏服务端框架简介
    流量
    学习React Native必看的几个开源项目
    那些离开私企进入国企的人,现在都过得怎么样了?
    分布式系统中有关一致性的理解
  • 原文地址:https://www.cnblogs.com/perfect/p/579063.html
Copyright © 2011-2022 走看看