我如何关闭当前表格并打开新表格? [英] How Can I Close The Current Form And Open A New Form?

查看:131
本文介绍了我如何关闭当前表格并打开新表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有两种表格



LoGEnq表格

查询表格



和LoGEnq有一个消息框,我想当我点击确定按钮关闭当前表格并打开一个新表格。



  private   void  Btn1_Click( object  sender,EventArgs e)
{

SqlConnection cn = new SqlConnection( global :: EnQApp.Properties.Settings.Default.Database1ConnectionString);

尝试 {
cn.Open();
使用(SqlCommand command = new SqlCommand( SELECT * FROM Login,cn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString( 0 ); // 名称字符串
string pass = reader.GetString( 1 ); // 密码字符串
}
MessageBox.Show( 密码接受);
this .Close();
}
}
查询enq = 查询(UName.Text);
enq.Show();
}





但这将关闭申请表中的所有表格



i只想在我点击消息框时关闭LoGEnq应用程序确定

并打开查询表格?

解决方案

这是很有意思的当你调用this.Close()它将关闭Form1并执行将返回Program.cs Main()。可以有一个解决方案。更改您的Program.cs

 命名空间 TestWindowApp 
{
static class 计划
{
public 静态 bool SetForm2 = false ;
/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1());

if (SetForm2)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}
}





无论你在哪里设置SetForm2 = true想要关闭当前表格并想打开Form2(查询)。



 Program.SetForm2 = ; 
this .Close();


试试这个



  if (DialogResult.OK == MessageBox.Show( 密码已接受 密码,MessageBoxButtons.OKCancel,MessageBoxIcon.Information))
{

Form2 frm = new Form2 ();
frm.Show();
this .Close(); // 如果您尝试在主窗体中执行此操作。请使用此。隐藏();
}


查询enq = new查询(UName.Text); 
enq.Show();
this.Close();







打开新表格后关闭表格


Here i have two forms

LoGEnq form
Enquiry form

and LoGEnq has a message box and i want when i hit ok button close the current form and open a new form.

private void Btn1_Click(object sender, EventArgs e)
        {
            
            SqlConnection cn = new SqlConnection(global::EnQApp.Properties.Settings.Default.Database1ConnectionString);

            try {
                cn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Login", cn))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string name = reader.GetString(0);  // Name string
                        string pass = reader.GetString(1); // Password string
                    }
                    MessageBox.Show("Password Accepted");
                    this.Close();
                }
            }
            Enquiry enq = new Enquiry(UName.Text);
            enq.Show();
        }



but this will close all forms in the application

i just want to close the LoGEnq application when i click message box OK
and open Enquiry Form?

解决方案

This is obious that when you will call this.Close() it will close the Form1 and execution will return to Program.cs Main(). There can be a solution. Change your Program.cs

namespace TestWindowApp
{
    static class Program
    {
        public static bool SetForm2 = false;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            if (SetForm2)
            {
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
        }
    }
}



And set SetForm2=true wherever you want to close the current form and want to open Form2(Enquiry).

Program.SetForm2 = true;
this.Close();


Try this one

if (DialogResult.OK == MessageBox.Show("Password Accepted", "Password", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{

 Form2 frm = new Form2();
 frm.Show();
 this.Close(); // if you are trying to do this in your main form.Use this.Hide();
}


Enquiry enq = new Enquiry(UName.Text);
    enq.Show();
    this.Close();




Close the form after you open the new form


这篇关于我如何关闭当前表格并打开新表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆