0

Here is my question:
Im making a booking cinema system in c#, windows form
Let's say i have 5 columns of 5 rows of pictureboxes that on form load get their value, avaliable or not from the database.
The user then click on the seat he wants (and the image of the pictuebox change) and press a submit button.
How i can check the image of every picturebox (to determine if he want this seat or not) together?
I can do something like this

if (picturebox11.image=="seatchecked"){seats[]+=11;}
if (picturebox12...

But im wondering if there is another faster way to do it. (the position of the pictureboxes is fixed if that helps)

I have done this so far:

private void button1_Click(object sender, EventArgs e)
        {
            List<PictureBox> pb = new List<PictureBox>();
            pb.Add(seat11);
            pb.Add(seat12);
            pb.Add(seat13);
            pb.Add(seat14);
            pb.Add(seat15);
            pb.Add(seat21);
            pb.Add(seat22);
            pb.Add(seat23);
            pb.Add(seat24);
            pb.Add(seat25);
            pb.Add(seat31);
            pb.Add(seat32);
            pb.Add(seat33);
            pb.Add(seat34);
            pb.Add(seat35);
            for (int i = 0; i < 20; i++) {

                pb[i].Click += pictureBox_Click;
            }
        }
             void pictureBox_Click(object sender, EventArgs e)
{
   this.pictureBox.Image = ArgyroCinema.Properties.Resources.seatred;     
}
CDrosos
  • 2,418
  • 4
  • 26
  • 49

1 Answers1

0

Store each PictureBox in a list and iterate through them. Also, when the user selects/deselects a seat, change the Tag property of the PictureBox as, at the moment, you're trying to compare a string to an Image (picturebox11.Image returns an Image object).

List<PictureBox> pb = new List<PictureBox>();
pb.Add(pictureBox1);
pb.Add(pictureBox2);
//etc..

Alternatively, you can use the methods suggested here to get all PictureBox objects in your form to save you having to type it out above.

Then just iterate through them and read their Tag property. In this case I've used true to represent that they want the seat but Tag is an object type, so you can use whatever type you like.

foreach(PictureBox p in allPictureBoxes)
{        
    if((bool)p.Tag == true)
    {
       //seat wanted
    }
    else
    {
       //seat not wanted
    }
}

Update from comments

void pictureBox_Click(object sender, EventArgs e)
{
   PictureBox pb = sender as PictureBox;
   if(pb != null)
      pb.Image = ArgyroCinema.Properties.Resources.seatred;     
}
Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • i can add mouseenter and mouseleave events in all of them together with those solutions? im having hard time figuring out how – CDrosos Jul 28 '13 at 10:43
  • Yes you can but it depends on how you're creating the PictureBox. Are you creating them in the designer or in code? – keyboardP Jul 28 '13 at 11:03
  • i guess i can create and place them in the code if that will help me to avoid some code, if you can please give me an example of how to create ,set an image from the project database, place it in the form and add those events – CDrosos Jul 28 '13 at 11:22
  • See the [answer here](http://stackoverflow.com/questions/6606819/creating-numerous-pictureboxes-by-code-only-one-is-visible) to show you how to create them in code. After creating them, simply add them to a list (`myList.Add(mainSprite)`). You can attach the click event handler as [shown here](http://stackoverflow.com/a/17907655/187697). – keyboardP Jul 28 '13 at 11:27
  • i have edit my first post, i dont know how to set up the event for every picture box (this.pictureBox.Image doesnt work) Also some pictureboxes must be locked if there are already booked. i should use enabled.false? i have to put it last in the code or it doesnt matter? – CDrosos Jul 28 '13 at 12:03
  • That seems fine although you might want to use `i < pb.Count` instead in case the size of the list changes. Add an `if` check to see if that PB should be locked, in which case, you can set the `Enabled` property to false like you said. Putting it last in the `for` loop would work. – keyboardP Jul 28 '13 at 12:06
  • im getting this error:Error 1 'ArgyroCinema.FormRes' does not contain a definition for 'pictureBox' and no extension method 'pictureBox' accepting a first argument of type 'ArgyroCinema.FormRes' could be found (are you missing a using directive or an assembly reference?) C:\Users\Exoskeletor\documents\visual studio 2012\Projects\ArgyroCinema\ArgyroCinema\FormRes.cs 51 20 ArgyroCinema – CDrosos Jul 28 '13 at 12:08
  • @CDrosos - Ah sorry, I missed your `pictureBox_Click` event. Cast the `sender` to a PictureBox and use that. (See my updated answer). – keyboardP Jul 28 '13 at 12:11
  • Also, are you sure you want to create a new list every time the button is clicked? If you want to do it as a one-off then move it outside the button's click event. – keyboardP Jul 28 '13 at 12:16
  • so far so good. now im trying to select deselect the avaliable seat on click. im trying to do it this way: if (pb.Image.ToString == "seatblue2") { pb.Image = ArgyroCinema.Properties.Resources.seatblue; } else { pb.Image = ArgyroCinema.Properties.Resources.seatblue2; } what am i doing again wrong? – CDrosos Jul 28 '13 at 12:30
  • See [my answer here](http://stackoverflow.com/questions/17460419/change-checked-state-with-image). (Basically, use the `Tag` property to determine the seat colour instead of the `Image` property). – keyboardP Jul 28 '13 at 12:35
  • Thanx work amazing. one last question. is there a way to do something like this: int booked_set[]={11,12}; for (i=0;i<30;i++) seat[].enabled=false – CDrosos Jul 28 '13 at 12:45
  • What are you trying to do? Disable only 11 and 12? – keyboardP Jul 28 '13 at 12:48
  • yes but i think i will just do this if seats_booked=="11" then seat11.enabled=false. i just have to do it 20 times – CDrosos Jul 28 '13 at 12:49
  • This might be easier. Have an array (like you do) that stores the ones you want disabled. Then in your loop, do this `if (booked_set.Contains(i)) seat[i].Enabled=false`. That checks if `i` is in your array. If it is, then disable that particular seat. – keyboardP Jul 28 '13 at 12:50
  • thanks but i think i cant do that because the first number is the line number and the second number is the seat number – CDrosos Jul 28 '13 at 12:54
  • Ah okay, I misunderstood what that represented. – keyboardP Jul 28 '13 at 12:55