I am having trouble figuring out how to handle events for components that are created when I click a button.

I have a class called rows which is used to create a row of components (combo boxes) . I have a button on my form that has an event to create a new rows object. I would like to have events for the newly created combo boxes but I dont know how or where to put them.

Here is my form code:

private void button1_Click(object sender, EventArgs e)
    {
        rowClass row = new rowClass(startingBuildingComboBoxX, startingRoomComboBoxX, startingProblemsTextBoxX, startingNotesTextBoxX, startingY + 30);
        startingY = startingY + 30;
        this.Controls.Add(row.buildingComboBox);
        this.Controls.Add(row.roomComboBox);
        this.Controls.Add(row.problemsTextBox);
        this.Controls.Add(row.notesTextBox);
        button1.Location = new Point(411, buttonY);
        buttonY = buttonY + 30;
        this.Height += 30;




    }

so whenever I push the button it creates a row of components and also moves the button down to the next row.

I would like to add events for those components that were created in that rows object. But how do I do that??

this is my code for that event:

     public void buildingComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

        this.roomComboBox.Items.Clear();
        if (this.buildingComboBox.SelectedItem.Equals("ATLC"))
        {
            foreach (int x in ATLC)
            {
                this.roomComboBox.Items.Add(x);
            }
        }


    }

I just dont know where it belongs, or how to tell the form to use it etc.

ok so I now under my button event I have this

 row.buildingComboBox.SelectedIndexChanged += new EventHandler(buildingComboBox_SelectedIndexChanged);

and my event code is this:

        private void buildingComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

      roomComboBox.Items.Clear();
        if (this.buildingComboBox.SelectedItem.Equals("ATLC"))
        {
            foreach (int x in ATLC)
            {
                this.roomComboBox.Items.Add(x);
            }
        }


    }

My problem now is it will not let me reference buildingComboBox or roomComboBox even though they exist on the form after the button is clicked

See here. Basically, you subscribe to an event like so:

comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;

and you can unsubscribe from an event like so:

comboBox.SelectedIndexChanged -= comboBox_SelectedIndexChanged;

The sender parameter gives you the reference to the particular ComboBox that fired the event. For example:

private void buildingComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox comboxBox = sender as ComboBox;
    if (comboBox != null)
    {
        ...
    }
}

but why cant I reference the variables of my new row object
I havent done much with programming so im sure its a scope issue but I cant figure out why
I need to refernce the variables of the row object from within my event code for

i mean i create the new row object when i press the button, and creating the row object is what puts all the new combo boxes on the form, so i should be able to reference those comboBoxes using row.whateverComboBox right???

do i need to make it a global variable of sorts in the row class or something??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.