gpt4 book ai didi

c# - 在第一个 winform 页面中设置标签文本以显示在第二个表单页面上

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:02 25 4
gpt4 key购买 nike

我是 C# 的新手,所以如果我的术语不符合标准,请原谅我。在我的 winform 应用程序中,我想从 MySQL 数据库@登录中获取一个名称,并在第二页 (home.cs) 上显示该名称。我目前收到一个错误,即“非静态字段需要对象引用。在 login.cs 中,我试图添加类似 -> Home.bunifuCustomLabel2.Text = "test"; 的内容来设置home.cs 中的标签基本上... Login.cs 是我的第一个加载页面,Home.cs 是我的第二个页面,它在成功登录后加载。我想在连接打开时查询数据库并最终设置标签文本让 home.cs 显示类似 Welcome, Name

的内容

登录.CS:

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 Firemax
{
public partial class Login : Form
{
MySqlConnection con = new MySqlConnection(@"Data Source=xyz.com;port=3333;Initial Catalog = test_db;User Id = fml;password=imscrewed");
int i;
public Login()
{
InitializeComponent();
this.CenterToScreen();
}

private void BunifuMaterialTextbox1_OnValueChanged(object sender, EventArgs e)
{

}

private void BunifuMaterialTextbox2_OnValueChanged(object sender, EventArgs e)
{
bunifuMaterialTextbox2.isPassword = true;
}

private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from users where username='" + bunifuMaterialTextbox1.Text + "' and password='" + bunifuMaterialTextbox2.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());

if (i==0)
{
bunifuCustomLabel3.Text = "You have entered either a wrong Username and/or Password";

Home.bunifuCustomLabel2.Text = "test";
}
else
{
this.Hide();
Home fm = new Home();
fm.Show();
fm.Location = this.Location;
}
con.Close();

}

private void BunifuMaterialTextbox1_OnValueChanged_1(object sender, EventArgs e)
{

}

private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

主页.CS:

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 Firemax
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
this.CenterToScreen();
}

private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

最佳答案

欢迎来到 C# 世界!将 logic.cs 的代码更改为以下将消除您面临的问题。

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 Firemax
{
public partial class Login : Form
{
MySqlConnection con = new MySqlConnection(@"Data Source=xyz.com;port=3333;Initial Catalog = test_db;User Id = fml;password=imscrewed");
int i;
public Login()
{
InitializeComponent();
this.CenterToScreen();
}

private void BunifuMaterialTextbox1_OnValueChanged(object sender, EventArgs e)
{

}

private void BunifuMaterialTextbox2_OnValueChanged(object sender, EventArgs e)
{
bunifuMaterialTextbox2.isPassword = true;
}

private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from users where username='" + bunifuMaterialTextbox1.Text + "' and password='" + bunifuMaterialTextbox2.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());

Home fm = new Home();
if (i==0)
{
bunifuCustomLabel3.Text = "You have entered either a wrong Username and/or Password";

fm.bunifuCustomLabel2.Text = "test";
}
else
{
this.Hide();
fm.Show();
fm.Location = this.Location;
}
con.Close();

}

private void BunifuMaterialTextbox1_OnValueChanged_1(object sender, EventArgs e)
{

}

private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

您的代码需要改进。我强烈建议在 C# video series 上投入 70 分钟莫什。

关于c# - 在第一个 winform 页面中设置标签文本以显示在第二个表单页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57966375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com