Thursday, 1 March 2012

Creating cash register using WPF


Cash Register GUI, Implement Solution for this.


Add full functionality to the cash register GUI
·         Accept a valid decimal number
·         Enter : adds the value to subtotal
·         Total : calculate 13% HST and present Tax and Total
·         Delete : Deletes value field
·         Clear : empty register.



XAML Code:-

<Window x:Class="Vdesai_Assign1A.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Cash Register" Height="384" Width="465" KeyDown="PreviewkeyDown" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" SizeToContent="Manual" ResizeMode="CanResize">
    <Window.Resources>
        <Style x:Key="VStyle" TargetType="Button">
            <Setter Property="FontSize" Value="42" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="ButtonBorder"
                  CornerRadius="25,25,25,25"
                  BorderThickness="4,4,4,4"
                  Background="CadetBlue"  
                  BorderBrush="MediumAquamarine"
                  RenderTransformOrigin="0.5,0.5">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="1.7*"/>
                                </Grid.RowDefinitions>
                                <Border Grid.Row="0" CornerRadius="23,23,0,0">
                                    <Border.Background>
                                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                            <GradientStop Color="CadetBlue" Offset="0"/>
                                            <GradientStop Color="MediumAquamarine" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                </Border>
                                <ContentPresenter x:Name="ButtonContentPresenter"
                                VerticalAlignment="Center" 
                                Grid.RowSpan="2"
                                HorizontalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="RenderTransform" TargetName="ButtonBorder">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                        </TransformGroup>
                                  </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Margin" Value="2"/>
            </Style>
    </Window.Resources >
    <Window.Background>
        <LinearGradientBrush SpreadMethod="Reflect">
            <GradientStop Color="Beige"      Offset="0"/>
            <GradientStop Color="CadetBlue"    Offset="1"/>
        </LinearGradientBrush >
    </Window.Background >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="58*" />
            <RowDefinition Height="169.624*" />
            <RowDefinition Height="118.376*" />
        </Grid.RowDefinitions>
        <Label Height="28" HorizontalAlignment="Left" Margin="57,10,0,0" Name="label1" VerticalAlignment="Top" Width="39" Grid.RowSpan="2" Grid.Row="0">
            <Label.Effect>
                <DropShadowEffect ShadowDepth ="5" BlurRadius="5 "/>
            </Label.Effect > "$"
        </Label>
        <TextBox Margin="86,12,77,0" Name="textBox1" KeyDown="textBox1_KeyDown" PreviewTextInput="Preview_TextInput" Height="34" VerticalAlignment="Top" Grid.Row="0"/>
      <UniformGrid Rows ="4" Grid.Row="1" Margin="5,5,204,5">
            <Button Name="OneBtn" Content="1" Margin="5" Click="NumberBtn"  Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18" />
            <Button Name="TwoBtn" Content="2" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18" />
            <Button Name="ThreeBtn" Content="3" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="FourBtn" Content="4" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="FiveBtn" Content="5" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="SixBtn" Content="6" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="SeveBtn" Content="7" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="EightBtn" Content="8" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="NineBtn" Content="9" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="EmptyBtn" Content=" " Margin="5" Visibility="Hidden" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="ZeroBtn" Content="0" Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
            <Button Name="DotBtn" Content="." Margin="5" Click="NumberBtn" Style="{StaticResource VStyle}" Height="29.124" Width="65" FontSize="18"/>
        </UniformGrid>
       
       
        <StackPanel HorizontalAlignment="Right"  Name="stackPanel1"  Orientation="Vertical" Grid.Row="1" Width="144.167" Margin="5">
            <Button Name="EnterBtn" Content="Enter" Style="{StaticResource VStyle}" Click="EnterBtn_Click"  Margin="5"  FontSize="18" />
            <Button Content="Total" Style="{StaticResource VStyle}" Click ="TotalBtn_Click" Margin="5" FontSize="18" />
            <Button  Name="DeleteBtn" Content="Delete" Style="{StaticResource VStyle}" Click ="DeleteBtn_Click" Margin="5" FontSize="18" />
            <Button Content="Clear" Style="{StaticResource VStyle}" Click ="ClearBtn_Click" Margin="5" FontSize="18" />
        </StackPanel >
        <UniformGrid  Name="uniformGrid1" VerticalAlignment="Bottom" Grid.Row="2"  >
            <Label Name="SubtotLbl" Content="Sub Total" Grid.Row="0" Grid.Column="0" FontSize="12" />
            <Label Name="Lbl1" Grid.Row="0" Grid.Column="1" FlowDirection="RightToLeft">
                <Label.Effect>
                    <DropShadowEffect ShadowDepth ="5" BlurRadius="5 "/>
                </Label.Effect > "$"
            </Label>
            <TextBox Name="SubTxt" Grid.Row="0" Grid.Column="2" Width="140.666" />
            <Label Name="TaxLbl" Content="Tax(13%HST+PresentTax)" Grid.Row="1" Grid.Column="0" FontSize="12" Height="29.605" Width="176" />
            <Label Name="Lbl2"  FlowDirection="RightToLeft" Grid.Row="1" Grid.Column="1" >
                <Label.Effect>
                    <DropShadowEffect ShadowDepth ="5" BlurRadius="5 "/>
                </Label.Effect > "$"
            </Label>
            <TextBox Name="TaxTxt" Grid.Row="2" Grid.Column="3" Height="35.5" Width="140.666" />
            <Label Name="TotalLbl" Content="Total" Grid.Row="2" Grid.Column="0"/>          
            <Label Name="Lbl3" FlowDirection="RightToLeft" Grid.Row="2" Grid.Column="1">
                <Label.Effect>
                    <DropShadowEffect ShadowDepth ="5" BlurRadius="5 "/>
                </Label.Effect > "$"
            </Label>
            <TextBox Name="TotTxt" Grid.Row="2" Grid.Column="3" Height="35.5" Width="140.666" />
        </UniformGrid>
    </Grid>
</Window>


Code Behind:-

using System;
using System.Collections.Generic;
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 Vdesai_Assign1A
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        int count = 0;
        public Window1()
        {
            InitializeComponent();
        }
        private void NumberBtn(object sender, RoutedEventArgs e)
        {
            if (((Button)e.OriginalSource).Content.ToString() == ".")
            {
                if (!(textBox1.Text.Contains(".")))
                {
                    textBox1.Text += ((Button)e.OriginalSource).Content.ToString();
                }
                else
                {
                    MessageBox.Show("Input String Not in correct format");
                }
                Keyboard.Focus(textBox1);
            }
            else
                textBox1.Text += ((Button)e.OriginalSource).Content.ToString();
            Keyboard.Focus(textBox1);
        }

        private void EnterBtn_Click(object sender, RoutedEventArgs e)
        {

            if (SubTxt.Text == string.Empty)
            {
                SubTxt.Text = textBox1.Text;
                textBox1.Text = string.Empty;

            }
            else
            {
                decimal a;
                a = Convert.ToDecimal(textBox1.Text);
                decimal b;
                b = Convert.ToDecimal(SubTxt.Text);
                decimal c;
                c = a + b;
                textBox1.Text = string.Empty;
                SubTxt.Text = c.ToString("0.00");
                //                Keyboard .Focus ()
            }
            Keyboard.Focus(textBox1);
        }

        private void TotalBtn_Click(object sender, RoutedEventArgs e)
        {
            decimal tot = Convert.ToDecimal(SubTxt.Text);
            TaxTxt.Text = (((tot) * 13 / 100) + ((tot) * 5 / 100)).ToString();
            decimal tax = Convert.ToDecimal(TaxTxt.Text);
            TotTxt.Text = (tot + tax).ToString();
            Keyboard.Focus(textBox1);
        }

        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = string.Empty;
            Keyboard.Focus(textBox1);
        }

        private void ClearBtn_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = string.Empty;
            SubTxt.Text = string.Empty;
            TaxTxt.Text = string.Empty;
            TotTxt.Text = string.Empty;
            Keyboard.Focus(textBox1);
        }

        private void PreviewkeyDown(object sender, KeyEventArgs e)
        {
            bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal;
            bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod;

            if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
            {
                e.Handled = true;
                return;
            }

            bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
                || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
                || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
                || e.Key == Key.Tab
                || e.Key == Key.PageDown || e.Key == Key.PageUp
                || e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape
                || e.Key == Key.Home || e.Key == Key.End);

            e.Handled = !isControl && !isNumeric && !isNumPadNumeric;

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal;
            bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod;

            if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
            {
                if ((e.Key == Key.Decimal) && (e.Key == Key.Enter))
                {
                    if (textBox1.Text.Contains("."))
                    {
                        MessageBox.Show("Input String not in correct format");
                    }
                }

                e.Handled = true;
                return;
            }


            else if (e.Key == Key.Enter)
            {
                if (textBox1.Text.Contains("."))
                {

                }
                EnterBtn.Focus();
                if (SubTxt.Text == string.Empty)
                {
                    SubTxt.Text = textBox1.Text;
                    textBox1.Text = string.Empty;
                }
                else
                {
                    decimal a;
                    a = Convert.ToDecimal(textBox1.Text);
                    decimal b;
                    b = Convert.ToDecimal(SubTxt.Text);
                    decimal c;
                    c = a + b;
                    textBox1.Text = string.Empty;
                    SubTxt.Text = c.ToString("0.00");
                }
                Keyboard.Focus(textBox1);
            }
            bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
                || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
                || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
                || e.Key == Key.Tab
                || e.Key == Key.PageDown || e.Key == Key.PageUp
                || e.Key == Key.Return || e.Key == Key.Escape
                || e.Key == Key.Home || e.Key == Key.End);

            e.Handled = !isControl && !isNumeric && !isNumPadNumeric;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(textBox1);
        }

        private void Preview_TextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                if (e.Text.Contains('.')) count++;
                if (count <= 1)
                {
                    if (!(Char.IsDigit(ch) || ch.Equals('.')))
                        e.Handled = true;
                }
                else
                    if (!(Char.IsDigit(ch)))
                        e.Handled = true;
            }
        }
    }
}
 

No comments:

Post a Comment