windows表單間傳遞資料和方法的調用。

來源:互聯網
上載者:User
a表單間傳遞資料和方法:
Code
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Linq;
 7using System.Text;
 8using System.Windows.Forms;
 9
10namespace 視窗間傳遞資料1
11{
12    public partial class Form1 : Form
13    {
14        public Form1()
15        {
16            InitializeComponent();
17        }
18
19        private void button2_Click(object sender, EventArgs e)
20        {
21            Form2 fm2 = new Form2();
22            fm2.form1 = this;
23            fm2.Show();
24        }
25        public void GetNameAddPwd(string name,string pwd)
26        {
27            textBox1.Text = name.Trim();
28            textBox2.Text = pwd.Trim();
29        }
30    }
31}
32

(二)這是一個註冊頁面:

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 視窗間傳遞資料1
{
    public partial class Form2 : Form
    {
        public Form1 form1;//在這裡首先聲明了一個Form1變數用來調用Form1類中的方法。並且在登陸介面中的函數中為他初始化。
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            form1.GetNameAddPwd(textBox1.Text ,textBox2 .Text );//調用Form1中的方法給登陸介面傳遞值
            form1.Show();
            this.Hide();
        }
    }
}

其實不僅僅可以用在傳資料上,因為可以調用方法。比如在gridview中重新開啟一個添加資料介面,添加資料後可以調用其主介面的方法重新整理資料,這樣就可以添加完成後在主介面立刻看到添加的資料。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.