我正在尝试创建一个需要登录的应用程序,我已经成功地让应用程序隐藏了表单1(主应用程序)并通过初始化Application.Run直接加载表单2(登录)(新登录( ));
通过从(登录)表单中点击关闭应用程序,可以轻松绕过这个问题。然后将把你带到form1(MainApplication)我尝试在表单上定义接近退出应用程序,但遇到一个问题,成功登录也将关闭整个应用程序。这显然是不希望的,有人能指出我如何处理这样一个事件的正确方向吗?
Form1 (Main Application)
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 NewApplication1
{
public partial class MainApp : Form
{
public MainApp()
{
InitializeComponent();
Application.Run(new Login());
}
}
}
Form2(登录)
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;
using MySql.Data.MySqlClient;
namespace NewApplication1
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void Login_Click(object sender, EventArgs e)
{
try
{
string mySqlConnection = "datasource=xxxxx;port=3306;username='" + UserName_TXTBX.Text + "';password= '" + Password_TXTBX.Text + "'";
MySqlConnection mySqlConn = new MySqlConnection(mySqlConnection);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = mySqlConn;
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(cmd);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
mySqlConn.Open();
DataSet ds = new DataSet();
mySqlConn.Close();
Login.close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
private static void close()
{
ActiveForm.Close();
}
}
}
我遇到的问题是当表单被“red'x'”关闭时,它会关闭登录表单并在我希望它退出整个应用程序时继续使用MainApplication。