Reputation: 625
I have a program that I am trying to write that
one row of the comboboxes and text boxes are created dynamically when you click the plus button that is on the form and then you can keep clicking the plus and it keeps adding a row but moves the new row down
I have the row organized like this:
there is a comboBox for buildings, then a comboBox for rooms. And that is one row
The building comboboxes have lists of buildings in them, and when I choose an item from the list, I need to populate the room combobox with the list of room numbers that correspond with the building.
I am having an extremely hard time doing this. This is what Ive tried.
my row class is what I use to create the new rows. When I press the button, a new row is created with new comboboxes etc.
Here is my event handler
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
row.roomComboBox.Items.Add(x);
}
}
Now my problem is I need to somehow add the corresponding room number data to the roomComboBox associated with the current row, and I dont know how and its driving me nuts. The sender comboBox is associated with the current row, so is there a way to use the sender to reference the roomComboBox that is a member of the same row?? any help would be great. Ive searched lots of threads on here and cant seem to find an answer.
edit:
Is there a way to reference the object that a variable belongs to. If I could somehow reference the row object of the sender comboBox then i might could use that to reference the room comboBox of the same row object...maybe? somehow? please?
Upvotes: 1
Views: 413
Reputation: 1585
Hope this little snippet will help
(sender as ComboBox).Parent.Controls.Find("cmbRooms",true)
This code will get the control with id "cmbRooms" with reference to the ComboBox sender
Upvotes: 1
Reputation: 355
When you create your comboboxes for each row, you can set the Building combobox's Tag property to the Room Numbers combobox. That will let you retrieve the associated combobox for that row. i.e...
// create combobox for row N...
ComboBox cmbBuilding = new ComboBox();
ComboBox cmbRooms = new ComboBox();
// store rooms combobox in building combobox
cmbBuilding.Tag = cmbRooms;
// ...
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
// get the room combobox
ComboBox cmbRooms = comboBox.Tag as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
cmbRooms.Items.Add(x);
}
}
Upvotes: 1
Reputation: 112512
Use a DataRepeater Control for Windows Forms. It is included in the Visual Basic Power Packs but can be used in C# projects as well.
Another option is to use a DataGridView
.
Upvotes: 0