【问题标题】:How to show a new form如何显示新表格
【发布时间】:2015-02-14 09:22:55
【问题描述】:

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

【问题讨论】:

    标签: c# show show-hide


    【解决方案1】:

    您需要在 Show() 上使用 ShowDialog() 代替

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

    【讨论】:

    • @rezaRahmait 感谢您的快速回复,但似乎我没有显示模式的选项。有什么我需要事先做的吗?
    • @MarthaMason 抱歉是ShowDialog()(在编程语言之间切换会给我带来很多困难)
    • @ResaRahmait 谢谢!得到它的工作!没问题,我已经够努力了!
    【解决方案2】:

    使用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");
    

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2021-06-04
      • 2014-04-12
      • 2018-08-18
      • 1970-01-01
      相关资源
      最近更新 更多