title: C# Winform Click Event for Programmatically created user control
body: <p>I have created a user control which contains profile pix, Name, employee No, Branch (similar to contact card). On a form i have placed a flowlayoutview and wanted to display the user control for every row on the table name users.</p>
  
  <pre><code>private void Users_Load(object sender, EventArgs e)
      {
          load_grid();
          Load_UserList();
  
      }
  
  void Load_UserList()
      {
  
          string conn = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=ifz001;";
          SqlConnection Connect = new SqlConnection(conn);
          Connect.Open();
  
          try
          {
  
              var cmdQuery = "SELECT * FROM Users";
              var cmdCount = "SELECT COUNT (*) FROM Users";
              SqlCommand cmdQ = new SqlCommand(cmdQuery, Connect);
              SqlCommand cmdC = new SqlCommand(cmdCount, Connect);
              int count = (int)cmdC.ExecuteScalar();
              var ad = new SqlDataAdapter(cmdQuery, Connect);
              DataSet ds = new DataSet();
              DataTable dt = new DataTable();
              ad.Fill(dt);
              for (int i = 0; i &lt; dt.Rows.Count ; i++)
              {
                      UserLst1 uList = new UserLst1();
                  uList.Name = "userlst11" + i;
                  uList.FullName.Text = dt.Rows[i]["fname"].ToString();
                  uList.Emp_Id.Text = dt.Rows[i]["emp_no"].ToString();
                  uList.Department.Text = dt.Rows[i]["dept"].ToString();
                  uList.Branch.Text = dt.Rows[i]["brn"].ToString();
                  uList.ProfilePix.Image = Image.FromFile(dt.Rows[i]["img_path"].ToString());
                      flowLayoutPanel1.Controls.Add(uList);
  
               }
  
  
  
              Connect.Close();
          }
          catch(Exception u)
          {
              MessageBox.Show(u.Message);
          }
  
                  }
  </code></pre>
  
  <p>I didn't drag and drop the user control in the flowLayoutPanel1 instead used the above code to display them in the flowLayoutPanel1. (UserLst1 uList = new UserLst1(); uList.Name = "userlst11" + i;)</p>
  
  <p>And it works, displays all the users details in the user control for each row.</p>
  
  <p>Now the problem is; i need to create a click event on the user control so that the related user control id is populated to textBox1.</p>
  
  <p>The User Control --> uList.Name = "userlst11" + i;
  count is row number in table users</p>
  
  <p>How do i create Click Event for each User Control (ulist.Name) .</p>
  
  <p>the issue I face is, the user control name on increment which is uList.Name = "userlst11" + i;. where do I place the click event. I want click event for user control (ulist).</p>
  
  <p>I feel this post is not a duplication, coz i need the click event on my user control, which is a incremental with new name.</p>