如何关闭LoginForm&在C#中显示主窗体而不隐藏LoginForm [英] How to Close LoginForm & Show Main Form in C# without Hiding LoginForm

查看:125
本文介绍了如何关闭LoginForm&在C#中显示主窗体而不隐藏LoginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想知道如何编写我的C#程序来关闭登录表单和显示主表单。我只能使用Hide方法登录表单。但问题是,即使我关闭主表格。程序不退出。为了解决这个问题,我找到了解决方案。我不知道这是不是很好的编程实践。因为我对C#和编程世界都很陌生。



我的解决方案是。



Hi All,

I was wondering how to write my C# program to Close Login Form and Show Main Form. Simply I could use Hide method for Login Form. But the Problem is, Even I close Main Form. Program doesn't Exit. to over come this issue I found a solution. I don't know whether It is good programing practise. because I'm totally new to C# and programming world.

My Solution is.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Close_Last_Form
{
    static class Program
    {  
       //Create a variabel to store result
       public static DialogResult rslt;

        //create a property to assign values to the rslt variable
        public static DialogResult rsltt
        {
            get
            {
                return rslt;
            }
            set
            {
                rslt = value;
            }
        }


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());

            //according to the rslt Open Form2 or Exit the application
            if (DialogResult.OK  == rslt)
            {
                Application.Run(new Form2());
            }
            else
            {
                Application.Exit();
            }

        }
    }
}





在我的Form1中。有一个按钮可以转到Form2。该表格上的代码是



In my Form1. there is a button to goto the Form2. code on that Form is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Close_Last_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //tells the Program class I'm successfull to show form2
            Program.rsltt = DialogResult.OK;
            
            Form2 Form2 = new Form2();
            Form2.Show();
            this.Close();     
        }
    }
}







我需要一块来自专家的建议,这个代码是否可以在程序中使用。

谢谢。




I need a piece of advice from an Expert, Is this code ok to use in a program.
Thanks.

推荐答案

Program.cs

Program.cs
static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
 }





Form1.cs






Form1.cs


           private void button1_Click(object sender, EventArgs e)
        {  this.Hide();
        Form2 form2 = new Form2();
            if (form2.ShowDialog() != DialogResult.OK)
                Application.Exit();
}




in Program.cs
static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new frmMain());
          
       }


主表格中的



in Main form

private void frmMain_Load(object sender, EventArgs e)
{
frmLogin frml=new frmLogin();
frml.ShowDialog();
}



登录表格:


in login form:

private void btnLogin_Click(object sender, EventArgs e)
{
if(txtUserName.Text=="admin" && txtPassword.Text=="pass")
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Login Failed");
Application.Exit();

}
}


这篇关于如何关闭LoginForm&在C#中显示主窗体而不隐藏LoginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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