1

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.

7
  • Many duplicates. I prefer the method in the answer below.. Commented Sep 18, 2018 at 18:44
  • I don't see this as a duplication Commented Sep 19, 2018 at 3:44
  • If you want the actualy code in the UC you can put it into the constructor or into an event there which you trigger from the code you hook to. Same technique, duplicate. Note that in the lambda version (the 'simpler' code below) all variables in the surrounding code will magically stay in scope! Very handy!! - Btw: your code would look like this: UserLst1 uList = new UserLst1(); uList .Click += (s,e) => { your magic here }; Commented Sep 19, 2018 at 7:13
  • Thank you very much for the response, will UserLst1 uList = new UserLst1(); uList .Click += (s,e) => { your magic here }; solve, because uList.Name = "userlst11" + i; will it automatically identify which uList is clicked and that uList id is populated to textBox1.Text which is my worry. Commented Sep 19, 2018 at 7:27
  • 1
    Works like a charm. Wow. Thank you Commented Sep 19, 2018 at 10:26

1 Answer 1

3

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 };
Sign up to request clarification or add additional context in comments.

2 Comments

hi, thanks for your response, I have edited my question to explain in more detail. Where around should I place this click event. coz, my user control is under void Load_UserList() { ...}, I want the click event for my UserControl (uList). Kindly Help.
Thank you again for your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.