如何显示新表格

How to show a new form

我的 .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();
            }
        }
    }
}

您需要在 Show()

上使用 ShowDialog()

之后可以通过表单的DialogResult了解是否登录成功

使用ShowDialog()方法。它 returns 一个 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");