13 Replies - 3577 Views - Last Post: 18 December 2012 - 11:38 AM Rate Topic: -----

#1 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

WPF Cinema Booking

Posted 11 December 2012 - 01:52 PM

Hi Guys,
I`m trying to make a WPF application for a cinema, to book seats in a room. Here is what I did until now.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfControlLibrary1
{
public partial class SeatingArea : UserControl
{
    public ObservableCollection<int> Seats { get; private set; }

    public SeatingArea()
    {
        Seats = new ObservableCollection<int>();
        for (int i = 1; i <= 200; i++)
            Seats.Add(i);

        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        
        MessageBox.Show(((FrameworkElement)sender).DataContext.ToString());
        ((Button)sender).Background = Brushes.Blue;
        
        
        
    }

}
}




<UserControl x:Class="WpfControlLibrary1.SeatingArea"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Beige"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
    <StackPanel>
        <TextBlock HorizontalAlignment="Center" Text="SEATING CHART" FontSize="24" Margin="0,10"/>
        <ItemsControl  ItemsSource="{Binding Seats}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Name="LayoutRoot">
                        <Button Name="Seat"  Content="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="30" Height="30"  Margin="1,2" Click="Button_Click_1" />

                    </Grid>
                    

                    <DataTemplate.Triggers>
                        
                        
                    </DataTemplate.Triggers>


                    

                </DataTemplate>






            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Height="300" Width="550"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
</UserControl>





As you can see I am using an user control for creating a seating chart. I am using buttons as seats.
My questions: How can I change the color of the seats or how to make them unclickable by their name in the C# code?

Is This A Good Question/Topic? 0
  • +

Replies To: WPF Cinema Booking

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 11 December 2012 - 02:04 PM

Don't worry about the names. Just stop thinking that way.
Add the seats to a List<seat>. THen you can do what you like as part of that collection.


mySeatList[47].BackColor = green;
mySeatList[82].Click -= ClickHandler;// Unsubscribe to the click event


You simply can't try to micromanage all the seats by name. And you really shouldn't be doing these things from the outside anyway.

You should be telling the seat object it is either occupied or free and let it handle itself.

mySeatList[123].IsOccupied = true;

class Seat
{
   public bool IsOccupied
   {
       get { return backingvariable; }
       set
       {
           Backcolor = value ? "Blue" : "Yellow"
           backingvariable = value;
           if (value)
           {
              // Unsubscribe from click event
           }
           else
           {               
               // Subscribe to click event
           }
        }
   }
}


The Properties article and the Objects article linked in my signature block will help a lot.

This post has been edited by tlhIn`toq: 11 December 2012 - 02:04 PM

Was This Post Helpful? 1
  • +
  • -

#3 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 12 December 2012 - 10:19 AM

And I bind in xaml to the List<Seat> ?

This post has been edited by tlhIn`toq: 12 December 2012 - 10:28 AM
Reason for edit:: We all see the previous post: No need to quote it in its entirety

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 12 December 2012 - 10:30 AM

I suppose you could. Just depends on how you want to architect your program. But as you say you are laying about a bunch of buttons I'm not sure what control you want to bind the entire list to.

Quote

I am using buttons as seats.

Was This Post Helpful? 0
  • +
  • -

#5 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 12 December 2012 - 10:36 AM

I will need the program to autoselect the best suitable seats (by receiving the seats from a webservice) or also to select more than 1 seat and then to book those seats. Should I use something else than buttons? Thanks for helping me:D

This post has been edited by tlhIn`toq: 12 December 2012 - 10:59 AM
Reason for edit:: AGAIN: NO NEED TO QUOTE THE ENTIRE PREVIOUS POST. Just use the Fast Reply at the bottom of the page.

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 12 December 2012 - 11:06 AM

You can use anything you like. You are the architect of this program. You are engineering this software, hence the job title "Software Engineer".

But it sounds a lot like you are typing before you are engineering. You want the program to auto-select the best seat - but you are laying out a bunch of button on the window. What are the buttons for if the program is auto-selecting the seat? What is the .Click of the button supposed to do if the seat has been pre-selected?

I suspect you are doing what a lot of new coders do: Drag out some controls that they know and aren't afraid of, THEN try to write code that will make them do what they want. That's totally backwards. GUI is last, logic is first.

Please read the "I don't know where to start" article linked in my signature block. It is designed to help new coders plan before typing.
Was This Post Helpful? 0
  • +
  • -

#7 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 12 December 2012 - 12:09 PM

After all I changed it a little bit... but I still have some questions.. instead of buttons I`ll use check boxes.. but can I select the maximum number of check boxes I can check at a time?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 12 December 2012 - 12:21 PM

You can do anything you like: Its your program.

Let me ask you this... You changed buttons for checkboxes. Why? What is the purpose of the checkbox? How will the user interact with it?

Do you have a vision in your head of what the finished program will look like and how it will operate? Can you see it in your mind (or on you whiteboard) as a completed program? How would you expect it to work if you bought it from someone else? Do these checkboxes make sense?

The original question remains: What is the purpose of these interactive controls on the GUI if the best seat has been pre-selected by the logic of the application?
Was This Post Helpful? 0
  • +
  • -

#9 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 12 December 2012 - 02:11 PM

There are 2 situations: 1)the best seats are preselected...
2) if the user doesn't like those, he should be able to choose other seats...
So I think that CheckBoxes would be the best.. because he can check those boxes...
So, in what Panel should I put the boxes and how to limit the number of checkboxes that can be checked?
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 12 December 2012 - 02:24 PM

View Postlordcoco, on 12 December 2012 - 03:11 PM, said:

There are 2 situations: 1)the best seats are preselected...
2) if the user doesn't like those, he should be able to choose other seats...

Seems reasonable.

View Postlordcoco, on 12 December 2012 - 03:11 PM, said:

So I think that CheckBoxes would be the best.. because he can check those boxes...
So, in what Panel should I put the boxes and how to limit the number of checkboxes that can be checked?

Put them in whatever panel you like. You're in front of the Visual Studio designer for your application, not me. What panel do you think they'd look good in? I'm not co-authoring your homework. You need to at least give it a try and if you run into trouble someone here can help. Be we aren't providing code for homework when you, the student, haven't written any in an effort to solve this specific feature/need/error.

As for counting... You need a maxChanged variable and a counter that increases each time a box is checked (that's an event you can add a handler method for) and is unchecked (that's an event as well).

Now ask yourself "Do I really need to limit the number of checked seats?" If the buyer originally wanted 3 seats, then realized they forgot someone, shouldn't they be able to buy another seat here by simply checking a 4th checkbox? The method that calculates the total should work based on the final number of checked seats to give the buyer the maximum flexibility - and thus increase potential sales.

Just slow down and think each part through. Don't make it more complicated than you need to. How would you do any of it on paper with real tickets and a paper map of the theater? Don't over think it.
Was This Post Helpful? 1
  • +
  • -

#11 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 12 December 2012 - 04:23 PM

sorry for making it look like I don't try... but I really tried for lots of hours....but I still didn't figure it out..
So this is the xaml.cs code I have until now... I made a list of seats which are numbered by rows and nr in the row.


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfControlLibrary1
{
public partial class SeatingArea : UserControl
{
    private ObservableCollection<Seat> seats;

    public ObservableCollection<Seat> Seats
    {
        get { return seats; }
        set { seats = value; }
    }
    

    public SeatingArea()
    {
        Seats = new ObservableCollection<Seat>();
        for (int i = 1; i <= 10; i++)
        for (int j =1; j<=10;j++)
        {
            Seat seat = new Seat(j,i);
            seat.IsOccupied = false;
            Seats.Add(seat);
        }

        InitializeComponent();
    }


    

   

}

   public class Seat
	{
       bool isOccupied;
       int nr;
       int row;

       public int Row
       {
           get { return row*100; }
           set { row = value; }
       }

       public int Nr
       {
           get { return nr*100; }
           set { nr = value; }
       }

       public Seat(int nr, int row)
       {
           Nr = nr;
           Row = row;
       }

	   public bool IsOccupied

	   {
	       get { return isOccupied; }
	       set
	       {
	           
	           isOccupied = value;
	          
	        }
	   }
	}
}




When it comes to the .xaml code this is what I have until now..

<UserControl x:Class="WpfControlLibrary1.SeatingArea"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Beige"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
    <StackPanel>
        <TextBlock HorizontalAlignment="Center" Text="SEATING CHART" FontSize="24" Margin="0,10"/>
        <ItemsControl  ItemsSource="{Binding Seats}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid >

                        <CheckBox   Content="{Binding Nr}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="30" Height="30"  Margin="5,5" Click="Button_Click_1" Tag="{Binding}" Canvas.Left="{Binding Nr}" Canvas.Top="{Binding Row}"   />

                    </Grid>
                  

                    


                    

                </DataTemplate>






            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Height="300" Width="550"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
</UserControl>




I though that using the canvas was best for me because I wanted to show the seats on row... and by using Canvas.Left and Canvas.Top it should have worked... but it doesn't... I am using the WrapPanel it will make really odd... so that's why asked which panel would you advice me...
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 13 December 2012 - 07:23 AM

Oh.... Not which panel... Which *type* of panel... "Which panel" makes it sound (to me at least) like "Which of the panels already on my Window"

I'm not going to hold your hand through selecting every last control. You don't learn anything that way.

There are lots of things you could do... Like
<StackPanel orientation="Verticle">
   <StackPanel orientation="Horizontal>
     <!-- Row of seats -->


Or how about a Grid and no panel at all. Each control in its own cell?

There is no right or wrong answer here. This is about your stylistic choices that get you the layout and look you like. Get in there and work the living daylights out of all the options to find what you like and what clicks with you.
Was This Post Helpful? 0
  • +
  • -

#13 lordcoco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-November 12

Re: WPF Cinema Booking

Posted 18 December 2012 - 08:29 AM

So.. I`ve listened to you.. and I used to StackPanels to represent the seats...
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfControlLibrary1
{
public partial class SeatingArea : UserControl
{
    private ObservableCollection<Seat> seats;
    private int i = 0;
    private ObservableCollection<int> rows;
    private ObservableCollection<int> columns;


    public ObservableCollection<Seat> Seats
    {
        get { return seats; }
        set { seats = value; }
    }
    public ObservableCollection<int> Rows
    {
        get { return rows; }
        set { rows = value; }
    }
    

    public SeatingArea()
    {
        Seats = new ObservableCollection<Seat>();
        for (int i = 1; i <= 15; i++)
        for (int j =1; j<=5;j++)
        {
            Seat seat = new Seat(j,i);
            seat.IsOccupied = false;
            Seats.Add(seat);
        }
        rows = new ObservableCollection<int>();
        columns = new ObservableCollection<int>();
        foreach (Seat seat in Seats)
        {
            if (!rows.Contains(seat.Row))
            {
                rows.Add(seat.Row);
            }

            if (!columns.Contains(seat.Nr))
            {
                columns.Add(seat.Nr);
                Debug.WriteLine(seat.Nr);
            }
        }

        

        InitializeComponent();
    }


    
    

    private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
    {
        i++;
        MessageBox.Show(i.ToString());
    }
    
}

   public class Seat
	{
       bool isOccupied;
       int nr;
       int row;

       public int Row
       {
           get { return row; }
           set { row = value; }
       }

       public int Nr
       {
           get { return nr; }
           set { nr = value; }
       }

       public Seat(int nr, int row)
       {
           Nr = nr;
           Row = row;
       }

	   public bool IsOccupied

	   {
	       get { return isOccupied; }
	       set
	       {
	           
	           isOccupied = value;
	          
	        }
	   }
	}
}






<UserControl x:Class="WpfControlLibrary1.SeatingArea"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfControlLibrary1"
Background="Beige"

DataContext="{Binding RelativeSource={RelativeSource Self}}"
>

        

    <StackPanel>
        <TextBlock HorizontalAlignment="Center" Text="SEATING CHART" FontSize="24" Margin="0,10"/>
        <ItemsControl  ItemsSource="{Binding Rows}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>



                    <StackPanel Background="blue" HorizontalAlignment="Center" >
                        <ItemsControl  ItemsSource="{Binding Columns}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox HorizontalAlignment="Center"   VerticalAlignment="Top" Width="30" Height="30" Background="Red" />
                                        
                                    
                                </DataTemplate>
                                
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel HorizontalAlignment="Center" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </StackPanel>









                </DataTemplate>






            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"  />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
     </StackPanel>
</UserControl>






The problem is that I successfully bind the first itemssource to Rows ... but I don't think that the second itemssource is bound correctly to Columns... because when I start the application it shows me only 7 checkboxes per each row.. what do you think it's the problem? and what did you mean by a Grid of Controls.. because I can't find anything about it
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: WPF Cinema Booking

Posted 18 December 2012 - 11:38 AM

I'm showing this example the way I keep saying to do it: With a Seat class object. If you want to do a checkbox then go for it.

<grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition width="*" />
    <ColumnDefinition width="*" />
    <ColumnDefinition width="*" />
    <ColumnDefinition width="*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
   <RowDefinition Height="*" />
   <RowDefinition Height="*" />
  </Grid.RowDfinitions>

  <Seat Name="Seat1"  Grid.Row="0" Grid.Column="0" />
  <!-- More Seat objects -->
</Grid>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1