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中重新開啟一個添加資料介面,添加資料後可以調用其主介面的方法重新整理資料,這樣就可以添加完成後在主介面立刻看到添加的資料。