本文介绍了如何在用户控件中为标签添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!
我有一个注册表,一个登录表和一个主表。该程序从注册表开始。如果我注册数据(名称,电子邮件,密码)加载到本地数据库。当我正确登录显示主窗体。主窗体具有带标签的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();
}
}
}
推荐答案
这篇关于如何在用户控件中为标签添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!