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

标签 c#

我是 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/

相关文章:

c# - 从 C# (VS2013) 调用 C++ 函数时隐藏的调试信息

c# - 如何按对象中的属性对 List<T> 进行排序

c# - 我可以在 URL 引用地址检查中使用什么来了解请求来 self 的域中的何处?

c# - 如何在 C# 中显示测试性能数字的准确时间

c# - 自动增加文件名

c# - WPF 转换器 - 返回类类型的名称

c# - 如何在不完全阻塞线程的情况下将线程池中的线程暂停执行一定时间?

c# - 全新的 Xamarin.Forms 项目 : The file "obj\Debug\android\bin\packaged_resources" does not exist

c# - 如何在 C# 中检索磁盘信息?

C# WinForms DateTimePicker 自定义格式