如何在用户控件中为标签添加值? [英] How add value to a label in user control?

查看:130
本文介绍了如何在用户控件中为标签添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我有一个注册表,一个登录表和一个主表。该程序从注册表开始。如果我注册数据(名称,电子邮件,密码)加载到本地数据库。当我正确登录显示主窗体。主窗体具有带标签的usercontrol。我想用她/他的名字在标签上写一个欢迎文本。示例:欢迎Josh!。所以我应该识别用户,所以我使用登录表单中的textboxEmail.Text。它没有显示价值。



我尝试过:



Hi!

I have a registration form, a login form and a Main form.The program start with registration form. If i sign up the datas(name, email, password) load up to the local database. When i log in correctly show the main form. The main form has a usercontrol with a label. I would like to write a welcome text to the label with her/his name. Example: "Welcome Josh!". So I should to identify the user, so i use the textboxEmail.Text from the login form. It don't show the value.

What I have tried:

namespace personalFinance
{
   public partial class Login : Form
      {
        public Login()
         {

            InitializeComponent();
            var MainForm = new MainForm();
            MainForm.Show();
            HomepageUC hp = new 
            HomepageUC(textboxEmail.Text);
            hp.Show();
         } 
      }

}





public partial class HomepageUC : UserControl
{
   string login = "";

   public HomepageUC()
   {
       InitializeComponent();
   }

   public HomepageUC(string email): this() 
      {
       login = email;
        var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
        AttachDbFileName=|DataDirectory|database.mdf;");
        conn.Open();
         try
            {
              conn.Open();
              var cmd = new SqlCommand($"SELECT name FROM registration_data WHERE email = '{login}'", conn);
               var reader = cmd.ExecuteReader();
               while (reader.Read()) labelWelcome.Text = reader[0].ToString();
            }
            
            finally
            {
                conn.Close(); 
            }
      }
 }

推荐答案

SELECT name FROM registration_data WHERE email ='{login }',conn);
var reader = cmd.ExecuteReader();
while(reader.Read())labelWelcome.Text = reader [0] .ToString();
}

最后
{
conn.Close();
}
}
}
"SELECT name FROM registration_data WHERE email = '{login}'", conn); var reader = cmd.ExecuteReader(); while (reader.Read()) labelWelcome.Text = reader[0].ToString(); } finally { conn.Close(); } } }


不要 - 将用户控件的内部工作暴露给外界。

相反,请看这里:传输信息两种表格之间,第1部分:父母与子女 [ ^ ] - 它专注于表单,但控件的原则(甚至代码)完全相同(因为Form派生自Control)
Don't - that exposes the internal workings of the user control to the outside world.
Instead, see here: Transferring information between two forms, Part 1: Parent to Child[^] - it focusses on forms, but the principles (and even the code) are exactly the same for controls (because Form is derived from Control)


这篇关于如何在用户控件中为标签添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆