I have a windows form with a set of groupboxes each containing a number of buttons. Each button represents a time slot in a day (8:00 - 9:00, 13:30 - 14:30) which has either been booked or not booked (by another user). When pressing on a button I need it to display information about who booked the slot(ie name, postcode) which is already stored in a database. I can get the information from the database and display it as long as have the ID of the slot booked. To get this I need a date and a time. Each groupbox has corresponding date. When a user clicks a button I need it to tell me which groupbox it is in so I can use the associated date to find the information of the customer that booked it. How can I code this in c# to find what groupbox the clicked button is in.
CodePudding user response:
You can get the parent object of the sender from the ButtonClickEvent.
private void button1_Click (object sender, EventArgs e) {
Button btn = (Button)sender;
GroupBox grp = (GroupBox)btn.Parent;
MessageBox.Show (grp.Text);
}