Ifraz Imanudeen

Ifraz Imanudeen

  • NA
  • 60
  • 3.2k

C# Winform Click Event for Programmically created user contr

Sep 18 2018 9:07 PM

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.

  1. private void Users_Load(object sender, EventArgs e)  
  2.         {  
  3.             load_grid();  
  4.             Load_UserList();  
  5.   
  6.         }  
  7.   
  8.     void Load_UserList()  
  9.         {  
  10.               
  11.             string conn = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=ifz001;";  
  12.             SqlConnection Connect = new SqlConnection(conn);  
  13.             Connect.Open();  
  14.   
  15.             try  
  16.             {  
  17.                   
  18.                 var cmdQuery = "SELECT * FROM Users";  
  19.                 var cmdCount = "SELECT COUNT (*) FROM Users";  
  20.                 SqlCommand cmdQ = new SqlCommand(cmdQuery, Connect);  
  21.                 SqlCommand cmdC = new SqlCommand(cmdCount, Connect);  
  22.                 int count = (int)cmdC.ExecuteScalar();  
  23.                 var ad = new SqlDataAdapter(cmdQuery, Connect);  
  24.                 DataSet ds = new DataSet();  
  25.                 DataTable dt = new DataTable();  
  26.                 ad.Fill(dt);  
  27.                 for (int i = 0; i < dt.Rows.Count ; i++)  
  28.                 {  
  29.                         UserLst1 uList = new UserLst1();  
  30.                     uList.Name = "userlst11" + i;  
  31.                     uList.FullName.Text = dt.Rows[i]["fname"].ToString();  
  32.                     uList.Emp_Id.Text = dt.Rows[i]["emp_no"].ToString();  
  33.                     uList.Department.Text = dt.Rows[i]["dept"].ToString();  
  34.                     uList.Branch.Text = dt.Rows[i]["brn"].ToString();  
  35.                     uList.ProfilePix.Image = Image.FromFile(dt.Rows[i]["img_path"].ToString());  
  36.                         flowLayoutPanel1.Controls.Add(uList);  
  37.                       
  38.                  }  
  39.   
  40.   
  41.   
  42.                 Connect.Close();  
  43.             }  
  44.             catch(Exception u)  
  45.             {  
  46.                 MessageBox.Show(u.Message);  
  47.             }  
  48.               
  49.                     }  

I didn't drag and drop the user control in the flowlayoutview instead used the above code to display them in the flowlayoutview. (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).


Answers (1)