Login窗体代码:
namespace WindowsForms
{
    public partial class Login : Form
    {
        //记录用户登录次数
        private int nLoginCount = 0;
        //记录用户登录基本信息
        private XT_YHXX yhxx = null;
        private DataTable dt_yhxx = null;
        //用户信息操作类
        private XT_YHXX_DAO yhxxDAO = null;        public Login(XT_YHXX yhxx)
        {
            InitializeComponent();
            this.yhxx = yhxx;
            yhxxDAO = new XT_YHXX_DAO();
        }        #region 登陆系统事件
        private void But_Login_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                this.textBox_yhgh.Focus();
                return;
            }
            dt_yhxx = yhxxDAO.SelectByYHGHYHMM(Convert.ToInt32(this.textBox_yhgh.Text), this.textBox_yhmm.Text);
            if (dt_yhxx.Rows.Count > 0)
            {
                yhxx.Yhxh = Convert.ToInt32(dt_yhxx.Rows[0]["yhxh"]);
                yhxx.Yhgh = Convert.ToInt32(this.textBox_yhgh.Text);
                yhxx.Yhmc = dt_yhxx.Rows[0]["yhmc"].ToString();
                yhxx.Ksdm = Convert.ToInt32(dt_yhxx.Rows[0]["ksdm"]);                
                yhxx.Pydm = dt_yhxx.Rows[0]["pydm"].ToString();
                yhxx.Wbdm = dt_yhxx.Rows[0]["wbdm"].ToString();
                yhxx.Yhmm = this.textBox_yhmm.Text;
                yhxx.Yhqx = Convert.ToInt32(dt_yhxx.Rows[0]["yhqx"]);    
                yhxx.Yksb = 0;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                nLoginCount++;
                if (nLoginCount == 3)
                {
                    this.DialogResult = DialogResult.Cancel;
                    MessageBox.Show("登录次数超出上限,请重新打开系统!");
                }
                else
                {
                    MessageBox.Show("用户名密码输入错误!");
                    return;
                }
            }
        }
        #endregion        #region 取消登陆系统事件
        private void But_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        #endregion        #region 用户关闭窗口时触发事件
        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != DialogResult.Cancel && this.DialogResult != DialogResult.OK)
                e.Cancel = true;
        }
        #endregion        #region 对用户输入登录信息进行验证
        private bool check()
        {
            if (string.IsNullOrEmpty(this.textBox_yhgh.Text))
            {
                MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.textBox_yhmm.Text))
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        #endregion
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~程序入口:
namespace WindowsForms
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            XT_YHXX yhxx = new XT_YHXX();
            Login login = new Login(yhxx);
            if (login.ShowDialog()==DialogResult.OK)
            {
                login.Close();
                Application.Run(new Main(yhxx));
            }
        }
    }
}

解决方案 »

  1.   

    也就是说,当用户输错密码或者什么的时候,窗口就关掉了!查了下DialogResult的值是Cancel的!
      

  2.   

    查下你OK按钮的DialogResult设成什么了?
      

  3.   


                 if (nLoginCount == 3)
                {
                    MessageBox.Show("登录次数超出上限,请重新打开系统!");
                    this.DialogResult = DialogResult.Cancel;
                }
                else
                {
                    MessageBox.Show("用户名密码输入错误!");
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                    return;
                }
      

  4.   

    谢谢五楼,知道原因了,原来没有强行赋值的话,DialogResult的值是cancel的,之前调试的时候也是这样,就是没想到,谢谢阿