不要用这种跳转的思路,还要隐藏窗口,控制起来很繁琐 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;namespace WindowsFormsApplication3 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FrmLogin Login = new FrmLogin(); Login.Text = "Login"; Login.ShowDialog(); if (Login.IsDone) { MessageBox.Show("已登录"); Application.Run(new Form1()); } else MessageBox.Show("登录失败"); } class FrmLogin : Form { public bool IsDone = false; public FrmLogin() { this.Text = "登录"; this.StartPosition = FormStartPosition.CenterScreen; Button B = new Button(); B.Parent = this; B.Text = "Login"; B.Click += new EventHandler(B_Click); } void B_Click(object sender, EventArgs e) { IsDone = true; Close(); } } } }
传值 Application.Run(new Form1("A"));
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 WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Form1(String S) { InitializeComponent(); MessageBox.Show(S); } } }
重写主窗体的构造函数,传入登陆者的用户名: 登陆窗体://登陆按钮单击事件 private void Login_Click(object sender, EventArgs e) { //输入验证通过后。 MainForm mf = new MainForm(this.txtName.text);//txtNme就是登陆输入的用户名的文本框。 mf.Show(); his.Hide(); } 主窗体using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace demo { public partial class MainForm : Form { public MainForm(name) { InitializeComponent(); this.LoginName = name; } string LoginName;//这时在主窗体直接使用这个就可以啦 } }
哎,失误啊,俺在WB呢。 主窗体using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace demo { public partial class MainForm : Form { public MainForm(string name) { InitializeComponent(); this.LoginName = name; } string LoginName;//这时在主窗体直接使用这个就可以啦 } }
public welForm()
{
InitializeComponent();
}
private void welForm_Load(object sender, EventArgs e)
{
System.Threading.Timer t = new System.Threading.Timer(myHide);//myHide,就是我们要委托的方法
t.Change(3000, 0);//3000毫秒之后关闭欢迎窗口,调用主窗口,第二个0表示只执行一次;
}
private void SetHide()
{
if (this.InvokeRequired)//之所以要这样,是因为Windows的跨进程访问控件的安全性;
{
SetHideCallback c = new SetHideCallback(SetHide);
Invoke(c, new object[] { });
}
else
{
this.Hide();//执行隐藏
form2.Show();
}
}
void myHide(object t)
{
SetHide();
((System.Threading.Timer)t).Dispose();//关闭委托,释放资源
}
}
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MainForm frm = new MainForm();
frm.userName = "ABC";
frm.userPwd = "123";
frm.Show();
this.Close();
}
}
}
主窗口
namespace testA
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public string userName;
public string userPwd;
}
}
WinForm中没有跳转这一说,只有打开新窗体!
static void Main()
{
Mutex mt=new Mutex(true,"MutexInstance");
if(mt.WaitOne(0,false))
{
Login Login_FRm1=new Login();
Login_FRm1.ShowDialog();
if (Login_FRm1.Bresult==true)
{
Main_Frm mainfrm =new Main_Frm();
mainfrm.User_Name=Login_FRm1.User_Name;
mainfrm.User_ID=Login_FRm1.User_ID;
Login_FRm1=null;
Application.Run(mainfrm);
}
}
}
登录窗口
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); FrmLogin Login = new FrmLogin();
Login.Text = "Login";
Login.ShowDialog(); if (Login.IsDone)
{
MessageBox.Show("已登录");
Application.Run(new Form1());
}
else
MessageBox.Show("登录失败");
} class FrmLogin : Form
{
public bool IsDone = false; public FrmLogin()
{
this.Text = "登录";
this.StartPosition = FormStartPosition.CenterScreen; Button B = new Button();
B.Parent = this;
B.Text = "Login";
B.Click += new EventHandler(B_Click);
} void B_Click(object sender, EventArgs e)
{
IsDone = true;
Close();
}
}
}
}
Application.Run(new Form1("A"));
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public Form1(String S)
{
InitializeComponent(); MessageBox.Show(S);
}
}
}
登陆窗体://登陆按钮单击事件
private void Login_Click(object sender, EventArgs e)
{
//输入验证通过后。
MainForm mf = new MainForm(this.txtName.text);//txtNme就是登陆输入的用户名的文本框。
mf.Show();
his.Hide();
}
主窗体using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace demo
{
public partial class MainForm : Form
{
public MainForm(name)
{
InitializeComponent();
this.LoginName = name;
}
string LoginName;//这时在主窗体直接使用这个就可以啦
}
}
最简单的方法就是
实例主窗体的时候,将登录者的信息封装到一个实体类中,然后将其实体类传入主窗体的构造函数
当然,前提是主窗体的构造函数得有该参数
还有很多其它的方法,比如建一XML,将信息存入XML中,然后在主窗口读取……
主窗体using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace demo
{
public partial class MainForm : Form
{
public MainForm(string name)
{
InitializeComponent();
this.LoginName = name;
}
string LoginName;//这时在主窗体直接使用这个就可以啦
}
}
而登录完成后那些资源已经失去存在的必要,
但是却不能得到释放。以下是我使用的方式,虽然稍微复杂了一点点,但是登录完成后,登录窗体已经进入垃圾回收的视野:不妥之处还望指出,谢谢!User.csusing system;namespace LoginFormTest
{
/// <summary>
/// 用户信息实体类
/// </summary>
public class User
{
private string m_userName; public string UserName
{
get { return m_userName; }
set { m_userName = value; }
} private string m_password; public string Password
{
get { return m_password; }
set { m_password = value; }
}
}
}
FrmLogin.cs 登录窗体using System;
using System.Windows.Forms;namespace LoginFormTest
{
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
} private void btnOk_Click(object sender, EventArgs e)
{
//验证
if ("name" == txtUserName.Text &&
"password" == txtPassword.Text)
{
FrmMain.User = new UserInfo();
FrmMain.User.UserName = txtUserName.Text;
FrmMain.User.Password = txtPassword.Text;
this.DialogResult = DialogResult.OK;
}
else
{
//提示信息
}
}
}
}FrmMain.cs 主窗体using System.Windows.Forms;namespace LoginFormTest
{
public partial class FrmMain : Form
{
/// <summary>
/// 静态成员,方便别的窗体使用
/// </summary>
public static UserInfo User = null; public FrmMain()
{
//构造中验证用户登录信息
DialogResult _dlgResult;
FrmLogin _frm = new FrmLogin();
_dlgResult = _frm.ShowDialog();
if (_dlgResult == DialogResult.OK)
{//此处为了提高新能而加,可不加判断
InitializeComponent();
}
}
}
}Program.cs 启动程序的类,关键在这里using System;
using System.Windows.Forms;namespace LoginFormTest
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FrmMain _frm = new FrmMain();
if (FrmMain.User != null)//主要在这里加入必要的验证是否登入成功
{
Application.Run(_frm);
}
}
}
}谢谢!
mainfrm.User_Name=Login_FRm1.User_Name;
汗,这样最简单!但是,FormLogin窗体对象还是一直存在的吧!
if (ActiveMdiChild != null) this.ActiveMdiChild.Close();
m_FormDeviceType = new FormDeviceType(this);
m_FormDeviceType.MdiParent = this;
m_FormDeviceType.Parent = panelChild;
m_FormDeviceType.WindowState = FormWindowState.Maximized;
m_FormDeviceType.Show();
ActivateMdiChild(m_FormDeviceType);