0

I am building a C# movie theater program. On Form1, when clicking on "Reserve" button I want the 21 buttons on Form 2 to randomly change 6 seats to "Red". I would like for it to be different seats every time the "Reserve" button is pressed on Form 1.

Here is the code I am trying to make work.

Random random = new Random();

private Color GetRandomColor()
{
    return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}

private void reserve_button_Click(object sender, EventArgs e)
{
    button1.BackColor = GetRandomColor();
    button2.BackColor = GetRandomColor();
    button3.BackColor = GetRandomColor();
    button4.BackColor = GetRandomColor();
}

Movie Menu Picture Seating Selection

enter image description here

enter image description here

JohnG
  • 9,259
  • 2
  • 20
  • 29
Jfern32
  • 3
  • 2
  • The text and code appear to do two different things. The code appears to be randomizing the “Color” of the BUTTON. This is different than … _”I want the 21 buttons on Form 2 to randomly change 6 seats to "Red". I would like for it to be different seats every time the "Reserve" button is pressed on Form 1.”_ … ? … Are you trying to randomize the six (6) selected seats or the color of the buttons? – JohnG Dec 02 '21 at 16:25
  • You've not posted enough code. Why are you getting random colors when you just want red and the default? You need to put your seat buttons in a list and get 6 random seats each time, then set their color to red. – Steve W Dec 02 '21 at 16:25
  • As far as communication between two forms, you may find… [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) … helpful. – JohnG Dec 02 '21 at 16:25
  • Thanks for the response. I am trying to randomize buttons to the color red before selecting a seat that will turn green when clicked. If a button is already red and is clicked it will prompt to choose another seat since it has been reserved. – Jfern32 Dec 02 '21 at 16:42

1 Answers1

0

Well, you should take 6 random seats which are not Red and color them red. Assuming that you use Winforms:

Form2 which shows the seats:

using System.Linq;

  ... 

public bool MakeReservation(int count) {  
  // Buttons to exclude from being painted into red:
  HashSet<Button> exclude = new HashSet<Button>() {
    //TODO: put the right names here
    btnBook,
    btnClear,  
  };

  var buttons = Controls                             // All controls
    .OfType<Button>()                                // Buttons only
    .Where(button => button.BackColor != Color.Red)  // Non red ones
    .Where(button => !exclude.Contains(button))      // we don't want some buttons
    .OrderBy(_ => random.NextDouble())               // In random order
    .Take(count)                                     // count of them
    .ToArray();                                      // organized as an array

  //TODO: you can check here if we have 6 buttons, not less  
  if (buttons.Length < count) {
    // Too few seats are free 
    return false; 
  }

  // Having buttons selected, we paint them in red:
  foreach(var button in buttons)
    button.BackColor = Color.Red;

  return true;
}

When Form1 either opens a new Form2 or finds existing one:

private void reserve_button_Click(object sender, EventArgs e) {
  var form = Application
    .OpenForms
    .OfType<Form2>()
    .LastOrDefault();

  if (form == null) 
    form = new Form2();
  
   form.Show();
   //TODO: put the actual number instead of 6
   form.MakeReservation(6);
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • This is working perfectly, the only thing i forgot to mention was that the "Book" button is also changing to Red when the randomization is taking place. How can I exclude only that button from turning Red? – Jfern32 Dec 02 '21 at 17:18
  • @Jfern32: you can exclude some buttons with a help of one more `.Where`, please, see my edit – Dmitry Bychenko Dec 02 '21 at 18:35