.NET基础示例系列之七:登录窗口

WinForm程序常常需要制作登录窗口,如果登录过程较复杂,所需时间较久,有时还需要在登录过程中显示一些进度信息,以下是一个简单的示例:

新建两个窗体,分别作为登录窗体、主窗体,现在重点在登录窗体:

添加必要的标签、文本框,两个按钮,以及一个用于显示状态的标签:lbState

设置窗体的acceptbuttoncancelbutton分别为btOKbtCancel,使窗体可以响应Enter键和Esc键。设置btCancelDialogResultCancel

btOK的点击事件中:

private void btOK_Click(object sender, EventArgs e)

{

    this.lbState.Text = "用户验证......";

    Application.DoEvents();//处理当前在消息队列中的所有windows消息,避免出现界面假死现象

    if (!UserCheck(this.tbName.Text, this.tbPwd.Text))

    {

        this.lbState.Text = "";

        MessageBox.Show("用户名或密码错误!", "错误");

        this.tbName.Focus();

    }

    else

    {

        this.lbState.Text = "连接DCOM......";

        Application.DoEvents();//

        if (!ConnectDCOM())

        {

            this.lbState.Text = "";

            MessageBox.Show("连接DCOM出现错误,程序无法启动!", "错误");

        }

        else

        {

            this.DialogResult = DialogResult.OK;

        }

    }

}

 

private bool UserCheck(string userName,string userPwd)

{

//TO DO:USER CHECK

}

 

private bool ConnectDCOM()

{

// TO DO:CONNECT DCOM

}

 

程序入口处:

static void Main()

{

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

 

    LoginForm lf = new LoginForm();

    if (lf.ShowDialog() == DialogResult.OK)

    {

        Application.Run(new MainForm());

    }

}

posted @ 2006-09-10 10:51  后厂村思维导图馆  阅读(2690)  评论(0编辑  收藏  举报