如何显示新表格

时间:2015-02-14 09:22:56

标签: c# show show-hide

我在使用.show()函数时遇到问题,它会隐藏上一页,但是当它显示新页面时,它会弹出一秒钟,然后停止调试立即编程。 这是第一页的代码。

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 System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form1()
        {

            InitializeComponent();
            connection.ConnectionString =   @"Provider=Microsoft.ACE.OLEDB.12.0;Data  Source=C:\Users\Martha\Documents\Database2.accdb;
        Persist Security Info=False;";  
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error  " + ex);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "SELECT * FROM StudentLogin WHERE      Username='" + TXT_NAME.Text + "'AND Password='" + TXT_PASSWORD.Text + "'";

            OleDbDataReader reader = command.ExecuteReader();
            int count = 0;
            while (reader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                MessageBox.Show("Username and Password are Correct");
                connection.Close();
                connection.Dispose();
                this.Close();
                PAGE_MAIN mainpage1 = new PAGE_MAIN();
                mainpage1.ShowDialog();
            }
            else
            { 
                MessageBox.Show("Username and Password are incorrect");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (HELP_LOGIN.Visible == true)
            {
                HELP_LOGIN.Hide();
            }
            else
            {
                HELP_LOGIN.Show();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在ShowDialog()

上使用Show()

之后您可以使用表单的DialogResult来了解登录是否成功

答案 1 :(得分:1)

使用ShowDialog()方法。它返回DialogResult,表示对话框的关闭方式

var dialogResult = HELP_LOGIN.ShowDialog();

if ( dialogResult == DialogResult.OK )
    MessageBox.Show ("User clicked OK button");
else if ( dialogResult == DialogResult.Cancel)
    MessageBox.Show ("User clicked Cancel button");