0

I have a List in which I have stored name of few buttons. I have used this to create List.`

        List<String> seat = new List<String>(); //List name "seat". 
        for (int a = 0; a < dt.Rows.Count; a++)
            seat.Add(dt.Rows[a]["SeatNo"].ToString()); //Adding values to seat from a column of table.("SeatNo" column has button names).

What I want is that all the buttons in form whose Name is same as those Names containing in the List, to get disabled.

I thought of traversing the List and disabling the buttons whose name matches with name in the List but I cannot get the logic for that.

I am a beginner in C#, Please explain it in simple terms. Thank you in advance.

1 Answers1

1

Don't put in comments that repeat what the code is doing. Name collections with plural names. Create your List like so:

var seats = new List<String>();
foreach (var row in dt.Rows)
    seats.Add(row["SeatNo"].ToString()); // "SeatNo" column has button names

You could also use LINQ to create the List:

var seats = dt.AsEnumerable().Select(r => r["SeatNo"].ToString()).ToList();

Then you just go through the seats and disable the matching controls:

foreach (var seat in seats) {
    var controls = this.Controls.Find(seat, true);
    foreach (var control in controls)
        control.Enabled = false;
}

If you are worried the names might conflict with other names, you can verify they are buttons:

foreach (var seat in seats) {
    var controls = this.Controls.Find(seat, true);
    foreach (var control in controls)
        if (control is Button)
            control.Enabled = false;
}
NetMage
  • 26,163
  • 3
  • 34
  • 55