简体   繁体   English

C#我不能以其他形式打印可变文本

[英]C# I can't print variable text in other Form

I trying to code program and I have two forms (Form1 is for loging and Form2 is empty for now, but I would like to print Logged: 'Name' + 'Surename' from DB). 我试图编写程序代码,并且有两种形式(Form1用于记录日志,Form2目前为空,但我想从数据库中打印Logged:'名称'+'Surename')。

I created class User which has Name and Surename (both public static strings) and method GetUserInfo() to get Name and Surename by user's Login. 我创建了具有名称和Surename(均为公共静态字符串)和方法GetUserInfo()的类User,该类通过用户的Login获取名称和Surename。 If I print User.Name + User.Surename in MsgBox after program checks login and password everything is ok, but after Form2 loads then 如果我在程序检查登录名和密码后在MsgBox中打印User.Name + User.Surename,则一切正常,但是在Form2加载后,

label1.Text = "User: " + User.name + " " + User.surename;

in method public Form2() shows just User: and nothing more. 在公共Form2()方法中仅显示User :,仅此而已。

Form1: Form1中:

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

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        string login = textBox1.Text;
        string pass = textBox2.Text;

        SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\xxx\Documents\Visual Studio 2015\Projects\*\*\Database1.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand query = new SqlCommand("Select * from Users where login='" + login + "' AND password ='" + pass + "'", connection);

        connection.Open();


        SqlDataReader Reader = query.ExecuteReader();

        int count = 0;

        Form1 form1 = new Form1();
        Form2 form2 = new Form2();

        while (Reader.Read())
        {
            count += 1;
        }

        if (count == 1)
        {
            this.Hide();
            form2.Show();

            User.GetUserInfo(login);

            MessageBox.Show(User.name + " " + User.surename);
        }
        else
            MessageBox.Show("Bad Login");
    }
}

Form2 窗体2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        label1.Text = "User: " + User.name + " " + User.surename;
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label1_Click_1(object sender, EventArgs e)
    {

    }
}

and User class: 和用户类别:

    public class User
{
    public static string name { get; set; }
    public static string surename { get; set; }

    public static void GetUserInfo(string login)
    {


        using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\xxx\Documents\Visual Studio 2015\Projects\*\*\Database1.mdf;Integrated Security=True;Connect Timeout=30"))
        using (SqlCommand query = new SqlCommand("Select FirstName, LastName from Users where login='" + login + "'", connection))
        {
            connection.Open();


            SqlDataReader Reader = query.ExecuteReader();

            while (Reader.Read())
            {
                User.name = Reader["FirstName"].ToString();
                User.surename = Reader["LastName"].ToString();
            }
        }
    }
}

Thank you! 谢谢!

Add public variables or properties to Form1 so that you can access them: 将公共变量或属性添加到Form1,以便您可以访问它们:

in Form1: 在Form1中:

public User user; 
....
user.name = Textbox1.Text; 
user.surname = Textbox2.Text; 

In your Form2: 在您的Form2中:

Form frm = new Form1();
frm.ShowDialog();
label1.Text = "User: " + frm.user.name + " " + frm.user.surename;
frm.Dispose();

You are setting the label text in the constructor of Form2 . 您正在Form2的构造函数中设置标签文本。 At that point they don't have a value as you haven't read the data from the database yet, so the label will simply read: 那时它们还没有值,因为您还没有从数据库中读取数据,因此标签将简单地读取为:

User: 用户:

It won't change until you update it's value again. 在您再次更新其值之前,它不会更改。 You need to call a method on Form2 to update the label text once the call to User.GetUserInfo has completed successfully. 一旦对User.GetUserInfo的调用成功完成,则需要在Form2上调用一个方法来更新标签文本。

    if (count == 1)
    {
        this.Hide();
        form2.Show();

        User.GetUserInfo(login);

        // Update the label text here

        MessageBox.Show(User.name + " " + User.surename);

or swap the order of getting the user information and constructing Form2 : 或交换获取用户信息和构造Form2的顺序:

    if (count == 1)
    {
        this.Hide();

        User.GetUserInfo(login);
        form2 = new Form2();
        form2.Show();

Either way will work, the one you choose depends on what else you need to do with your forms. 无论哪种方法都可以,选择哪种方法取决于您需要对表单执行的其他操作。

Use this code: 使用此代码:

User.GetUserInfo(login);    
this.Hide();
Form2 form2 = new Form2();
form2.Show();

because you need first set values for name and surename and then use it. 因为您需要先为namesurename设置值,然后再使用它。

In your case, you first show Form2 and then set values 对于您的情况,首先显示Form2,然后设置值

So the problem you have is that you are creating Form2 before the properties in the User class are filled. 因此,您遇到的问题是,要在填充User类中的属性之前创建Form2

This is why : 这就是为什么 :

label1.Text = "User: " + User.name + " " + User.surename;

This is called inside the constructor of Form2 . 这在Form2的构造函数中调用。 This means whenever you initialize this form with 这意味着每当您使用

Form2 form2 = new Form2(); 

all code in the constructor executes. 构造函数中的所有代码都将执行。 So the label text will be set when creating the form. 因此,在创建表单时将设置标签文本。

User.GetUserInfo(login); is called after this, the data will be empty for Form2 since the form is already created. 在此之后被调用时, Form2的数据将为空,因为已经创建了表单。

A possible solution would be 一个可能的解决方案是

To set the label in the form2 loading event and call User.GetUserInfo(login); 要在form2加载事件中设置标签并调用User.GetUserInfo(login); before form2.Show(); form2.Show();之前form2.Show();

or 要么

Calling Form2 form2 = new Form2(); 调用Form2 form2 = new Form2(); after User.GetUserInfo(login); User.GetUserInfo(login);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM