最近要做一個WinForm程序,需要想QQ那樣登錄成功後打開一個新的窗體,同時關閉登錄窗體。剛開始我是直接Form one=new Form();one.Show();this.Close();這樣兩個窗體都關閉了,因爲是在主線程上面操作。(注意:如果是在其他線程上面使用該方法是可以實現的,但是在主線程無法實現).後來在網上差了資料,根據http://blog.csdn.net/knight94/article/details/652394這裏的文章終於實現了我需要的功能。
項目截圖:
UserInfo.cs代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VideoWatch.AppCode
{
/// <summary>
/// 用戶登錄信息
/// </summary>
public class UserInfo
{
private string strUserName;
private string strPassword;
/// <summary>
/// 用戶名稱
/// </summary>
public string UserName
{
get { return strUserName; }
set { strUserName = value; }
}
/// <summary>
/// 用戶密碼
/// </summary>
public string Password
{
get { return strPassword; }
set { strPassword = value; }
}
public UserInfo()
{
strUserName = "";
strPassword = "";
}
}
}
LoginFrom.CS代碼:
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;
using VideoWatch.AppCode;
namespace VideoWatch
{
public partial class LoginForm : Form
{
LoginForm loginForm = null;
private int nLoginCount = 0;//登錄次數
private const int MAX_LOGIN_COUNT = 3;//限制只能登錄三次
private UserInfo uiLogin;//用戶登錄信息
public LoginForm(ref UserInfo ui)
{
InitializeComponent();
uiLogin = ui;
}
/// <summary>
/// 登錄程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "Admin" && txtPassword.Text == "123456")
{
// Save login user info
uiLogin.UserName = txtUserName.Text.Trim();
uiLogin.Password = txtPassword.Text.Trim();
// Set dialog result with OK
this.DialogResult = DialogResult.OK;
}
else
{
// Wrong username or password
nLoginCount++;
if (nLoginCount == MAX_LOGIN_COUNT)
// Over 3 times
this.DialogResult = DialogResult.Cancel;
else
{
MessageBox.Show("Invalid user name and password!");
txtUserName.Focus();
}
}
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
/// <summary>
/// 窗體關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Check whether form is closed with dialog result
if (this.DialogResult != DialogResult.Cancel &&
this.DialogResult != DialogResult.OK)
e.Cancel = true;
}
}
}
Program.cs代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using VideoWatch.AppCode;
namespace VideoWatch
{
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
UserInfo ui = new UserInfo();//用戶登錄信息
LoginForm myLogin = new LoginForm(ref ui);//加載登錄窗體
if (myLogin.ShowDialog() == DialogResult.OK)
{
//Open your main form here
//MessageBox.Show("Logged in successfully!");
Application.Run(new MainForm());//如果登錄成功則打開主窗體
}
else
{
MessageBox.Show("Failed to logged in!");
}
}
}
}