Form1 private string _username; public string UserName { get { return this._username; } set { this._username = value; } }Form2,加入Form2中有Form1的实例(可以传进来啊),然后直接赋值 this.Label1.Text = frm1.UserName; 还可以把from2的Lable的modifers属性设置为Public,这样在Form1中可以通过Form2的实例直接赋值 frm2.Lable1.Text = "xxxxxx";
form1: 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 WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); //textBox1.Leave += new EventHandler(textBox1_Leave); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string name = textBox1.Text; Form2 frm2 = new Form2(name); frm2.Show(); } } } form2: 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 WindowsFormsApplication5 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public Form2(string name)//重写构造方法 { InitializeComponent(); label1.Text = name; } }
2.定义个静态类,登录后把username填这里
......
public string UserName
{
get { return this._username; }
set { this._username = value; }
}Form2,加入Form2中有Form1的实例(可以传进来啊),然后直接赋值 this.Label1.Text = frm1.UserName;
还可以把from2的Lable的modifers属性设置为Public,这样在Form1中可以通过Form2的实例直接赋值
frm2.Lable1.Text = "xxxxxx";
form1:
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 WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Leave += new EventHandler(textBox1_Leave);
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
Form2 frm2 = new Form2(name);
frm2.Show(); } }
}
form2:
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 WindowsFormsApplication5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} public Form2(string name)//重写构造方法
{
InitializeComponent();
label1.Text = name;
}
}
}O(∩_∩)O