Mayank Jani

Mayank Jani

  • 1.5k
  • 477
  • 37.2k

EventHandler for programmetically created control.

Aug 2 2018 12:40 PM
Hii Members,
 
I am working on an application using C# WinApp. I have created a form and there is a button on the form named 'Show Bill List' in this button's click event, I have created some controls programmetically (dynamically) like, DataGridView (to show the records), Button (to close the DGV), DateTimePicker (to choose data between the dates) in a GroupBox. some of the codes...
 
private void btnBillList_Click(object sender, EventArgs e)
GroupBox gbBillList = new GroupBox(); //New group box.
Button btnListClose = new Button(); //New list close button to close the group box.
 
gbBillList.Location = new Point(70, 110); //Group box properties.
gbBillList.Text = "Bill List";
gbBillList.Font = new Font("Arial", 9);
gbBillList.Height = 450;
gbBillList.Width = 500;

btnListClose.Location = new Point(415, 415);//List close button properties.
btnListClose.Text = "Close List";

dgvBillList.Location = new Point(15, 55); //Bill list data grid view properties.
dgvBillList.Height = 350;
dgvBillList.Width = 470;
 
this.Controls.Add(gbBillList); //Adding group box to form.
gbBillList.Controls.Add(btnListClose); //Adding button to group box.
gbBillList.Controls.Add(dgvBillList); //Adding dgv to group box. 
 
 btnListClose.Click += new EventHandler(this.btnListClose_Click); //to close all list.
}
 
//Then created the event handler.
private void btnListClose_Click(Object sender, EventArgs e)
{
MessageBox.Show("Button List Close is Clicked.");
}
when clicked on the button, the message box appears. but I want to write code for do some act. e.g. choosing data between two dates and see the data in the DGV. but I can not see any of the controls that I have created, in event handler.
 
Please show me where I am mistaking... I have searched on net but everywhere is the same answer. I have also check, https://www.c-sharpcorner.com/uploadfile/mahesh/creating-a-button-at-run-time-in-C-Sharp/
but nothing.
please help.
 
Thank You.
 
Mayank Jani

Brought to you by:

Answers (8)