[英]C# after login, redirect to another form with existing parameters
我正在尝试将经过身份验证的用户重定向到另一个C#表单。
使用用户名和密码对用户进行身份验证后,他将被发送到另一种表单。 可悲的是,我无法从旧的form元素访问旧的参数。
我的代码如下所示:
表格1:
using MaterialSkin;
using MaterialSkin.Controls;
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 Helpful
{
public partial class Form1 : MaterialForm
{
public Form1()
{
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
private void materialRaisedButton1_Click(object sender, EventArgs e)
{
try
{
string username = materialSingleLineTextField1.Text;
string password = materialSingleLineTextField2.Text;
database_connector dbConnect = new database_connector();
bool db_response = dbConnect.user_check(username, password);
if (db_response == true)
{
MessageBox.Show("User authentificated.");
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("Please try again, wrong user credentials.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
表格2:
using MaterialSkin;
using MaterialSkin.Controls;
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 Helpful
{
public partial class Form2 : MaterialForm
{
public Form2()
{
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
}
}
database_connector类:
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 Helpful
{
class database_connector
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
// Constructor
public database_connector()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "xxx";
database = "xxx";
uid = "xxx";
password = "xxx";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator.");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
// Select statement
public bool user_check(string username, string password)
{
string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";
bool hasRecords = false;
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.HasRows)
{
while (dataReader.Read())
{
hasRecords = true;
}
}
dataReader.Close();
this.CloseConnection();
}
return hasRecords;
}
}
}
我的问题是,如何在不再次输入用户名的情况下立即使用表单2中的变量用户名?
我将不胜感激。
为了实现所需的功能,必须按照POO和Layers编程进行操作,然后创建一个类来保存数据。
在下面的示例中,您可以在第一个屏幕中设置参数,并且可以从任何其他可以看到此类的表单中访问它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class User
{
static private string cdUser;
static private int cdAcess;
static private string nmUser;
public static string CdUser
{
get
{
return cdUser;
}
set
{
cdUser = value;
}
}
public static int CdAcess
{
get
{
return cdAcess;
}
set
{
cdAcess = value;
}
}
public static string NmUser
{
get
{
return nmUser;
}
set
{
nmUser = value;
}
}
}
}
要保存数据,您可以执行以下操作:
User.CdUser = _login;
User.CdAcess = Convert.ToInt32(rdr["acess"].ToString());
User.NmUser = rdr["name"].ToString();
为可以很好地获得名称的Form2创建一个构造函数,或者在Form2中创建一个很好地包含它并对其进行设置的字段。
//first approach
new Form2(username).show();
//second approach
var f2 = Form2();
f2.username = username;
f2.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.