一、Winform中有两个窗口Form1、Form2,运行过程中Form1操作完就关闭,只显示form2主窗口,以及Form2中使用Form1中变量的问题:
1.首先在Form1中,从工具箱中拖两个“label控件”,命名为:txtuser(用户名)和txtpwd(密码),两个TextBox、命名为:txtUser.Text、txtPwd.Text,两个Button,命名为btnLog、btnCancel方法如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string User= this.txtUser.Text;
public static string Pwd= this.txtPwd.Text;
private void btnLog_Click(object sender, EventArgs e)
{
User = this.txtUser.Text;
Pwd = this.txtPwd.Text;
.......
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
2. Form2中也做一下定义:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public static string User = Form1.User;
public static string Pwd = Form1.Pwd ;
}
3.应用程序的主入口点 Main()中,做如下更改:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//登录成功后,关闭登录窗口
Form1 fm1=new Form1 ();
if (fm1.ShowDialog() == DialogResult.OK)
{
Application.Run(new Form2());
}
}
}
以上方法即是对两个窗口的简单操作!