• 在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();
            }

  • 相关阅读:
    arm单板上移植gdb
    video on web
    chromium源码阅读
    CE-HTML简介
    multi-tap
    DPDK 网卡RSS(receive side scaling)简介
    c语言实现带LRU机制的哈希表
    Linux TCP协议使用的变量
    scp源码浅析
    Linux e1000e网卡驱动
  • 原文地址:https://www.cnblogs.com/perfect/p/579063.html
Copyright © 2020-2023  润新知