简体   繁体   English

如何从winform中将值传递给用户控件?

[英]How pass a value to user control from a winform?

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. 主窗体具有带标签的usercontrol。 I would like to write a welcome text to the label with her/his name. 我想用她/他的名字在标签上写一个欢迎文本。 Example: "Welcome Josh!". 示例:“欢迎Josh!”。 So I should to identify the user, so i use the textboxEmail.Text from the login form. 所以我应该识别用户,所以我使用登录表单中的textboxEmail.Text。 My solution is not working. 我的解决方案不起作用。 There is my code: 有我的代码:

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

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

}
namespace personalFinance
{
    public partial class HomepageUC : UserControl
    {
       string login = "";
       public HomepageUC(string email)
          {

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

 }

I got that error: There is no argument given that corresponds to the required formal parameter 'email' of 'HomepageUC.HomepageUC(string)' personalFinance C:\\Users\\nickname18\\source\\repos\\personalFinance\\personalFinance\\MainForm.Designer.cs 我得到了这个错误:没有给出的参数对应于'HomepageUC.HomepageUC(string)'personalFinance C:\\ Users \\ nickname18 \\ source \\ repos \\ personalFinance \\ personalFinance \\ MainForm.Designer.cs所需的形式参数'email'

when i click this error retrieve to MainForm.Designer.cs this.HompageUC1 = new personalFinance.Homepage1(); 当我点击此错误检索到MainForm.Designer.cs this.HompageUC1 = new personalFinance.Homepage1(); it is underline with red. 它是红色的下划线。

The WinForms designer creates user controls by calling their default constructor. WinForms设计器通过调用其默认构造函数来创建用户控件。 Therefore, you can't define a custom constructor like that. 因此,您无法定义这样的自定义构造函数。

Instead, you should create a custom property. 相反,您应该创建一个自定义属性。

Is the email field unique? 电子邮件字段是唯一的吗? Did you already debug? 你有调试吗? Are occurs any error? 发生任何错误? Perhaps the query brought more than one record or no record, and perhaps the field was empty too. 也许查询带来了多条记录或没有记录,也许该字段也是空的。 Try this: 尝试这个:

namespace personalFinance
{
    public partial class HomepageUC : UserControl
    {
       string login = "";
       public HomepageUC(string email)
          {

            InitializeComponent();
            login = email;
            var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
            AttachDbFileName=|DataDirectory|database.mdf;");
            conn.Open();
            var cmd = new SqlCommand($"SELECT email FROM registration_data 
            WHERE email = '{login}'", conn);
            var reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                if(!string.IsNullOrEmpty(reader[0].ToString()))
                {
                    labelWelcome.Text = reader[0].ToString();
                    break;
                }
            }           
        }
    }
}

You should use parametrized queries and do proper usage of unmanaged resources. 您应该使用参数化查询并正确使用非托管资源。 An easy and quick trick to IDisposable is the using keyword IDisposable的一个简单快捷的技巧是using关键字

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

   public HomepageUC() // Default constructor for the designer/properties inicialization
   {
       InitializeComponent();
   }

   public HomepageUC(string email): this() // Your business logic constructor calls the default one
      {

        login = email;
        var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
        AttachDbFileName=|DataDirectory|database.mdf;");
        conn.Open();
        var cmd = new SqlCommand($"SELECT email FROM registration_data WHERE email = @email", conn);
        cmd.Parameters.AddWithValue("@email", email); // Use parameters to avoid SQLi 
        var reader = cmd.ExecuteReader();
        while (reader.Read()) labelWelcome.Text = reader[0].ToString();
        conn.Close(); // Added unmanaged resources liberation
        conn.Dispose();
      }
 }

} }

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

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