Search code examples
c#winformsuser-controls

C# Winform Click Event for Programmatically created user control


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.

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 < 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);
        }

                }

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;)

And it works, displays all the users details in the user control for each row.

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.

The User Control --> uList.Name = "userlst11" + i; count is row number in table users

How do i create Click Event for each User Control (ulist.Name) .

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).

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.


Solution

  • you can do it like:

        Button btn = new Button();
        btn.Click += Btn_Click;
    
        private static void Btn_Click(object sender, EventArgs e)
        {
            // do your magic here
        }
    

    or even simpler:

      btn.Click += (s,e) => { your magic here };