I am creating a program that simulates a flight reservation system. An admin will create each flight. The user will then fill in required search fields, and the flights matching those fields will be found and print out. With each match, there is a 'book' button. My question is, how can I make it so that when the user clicks the button, it runs the code based on that specific flight?

Ex: Clicking the 'book' button on a flight from L.A. to Chicago would differ from clicking the 'book' button on a flight from New York to Dallas.

The code for creating a flight is as follows:

public void addFlight()
        {
            Maintenance maint = new Maintenance();
            btn.Text = "Book";
            airline.Add(maint.airlineMaintComboBox.Text);
            price.Add(int.Parse(maint.minPriceMaintTextBox.Text));
            originCity.Add(maint.cityOriginMaintTextBox.Text);
            originState.Add(maint.stateOriginMaintComboBox.Text);
            destCity.Add(maint.cityDestMaintTextBox.Text);
            destState.Add(maint.stateDestMaintComboBox.Text);
            dateChosen = maint.dateMaintPicker.Value.ToString();
            date.Add(maint.dateMaintPicker.Value.ToString());
            seats.Add(int.Parse(maint.peopleMaintComboBox.Text));
            buttons.Add(btn);
            totalFlights++;
        }

Basically just adds the data to lists, then later the index list gets printed out.

I don't see where you declare btn, just set some of it's values. If you need to store something related to what you want to do in the button, use the Tag attribute.

I created the button outside of the method. I just pasted my code for the actually create flight button.

The click event has a sender variable, you can cast it into a button type and figure out which button is was from that.

This article has been dead for over six months. Start a new discussion instead.