Tuesday, November 25, 2014

WPF C# Data Save, Update and Delete with 3 layer architecture - Part 4(UI) - Final Part

In this Final Part we will design our UI and complete our Save, Delete & Update operation.
Our Final UI will look like the image below.

Here is XAML code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<Grid>
  <Grid.RowDefinitions>
   <RowDefinition/>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
   <ColumnDefinition/>
  </Grid.ColumnDefinitions>

  <Button x:Name="btnNewCustomer" Content="New Customer" HorizontalAlignment="Left" Margin="572,646,0,0" Width="125" Height="30" Click="btnNewCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnSaveCustomer" Content="Save Customer" HorizontalAlignment="Left" Margin="727,646,0,0" Width="125" Height="30" Click="btnSaveCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnDeleteCustomer" Content="Delete Customer" HorizontalAlignment="Left" Margin="891,646,0,0" Width="125" Height="30" Click="btnDeleteCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="1057,646,0,0" Width="125" Height="30" Click="btnClose_Click" VerticalAlignment="Top"/>
  <Label Content="Customer Information Form" HorizontalAlignment="Left" Margin="447,9,0,0" VerticalAlignment="Top" Background="{x:Null}" Foreground="#FF41B1E1" FontFamily="Lucida Calligraphy" FontSize="34" FontWeight="Bold" FontStyle="Italic" Width="642" Height="55"/>
  <GroupBox Header="Customer List" HorizontalAlignment="Left" Margin="378,70,0,0" VerticalAlignment="Top" Height="557" Width="957"/>
  <ListView 
   HorizontalAlignment="Left" 
   Height="473" 
   Margin="399,139,0,0" 
   VerticalAlignment="Top" 
   Width="921"
   x:Name="CustomerInfoListView" MouseDoubleClick="CustomerInfoListView_MouseDoubleClick">
   <ListView.ContextMenu>
    <ContextMenu x:Name="lstCustomerInfo" StaysOpen="True" Background="WhiteSmoke">
     <ContextMenu.BitmapEffect>
      <BitmapEffectGroup/>
     </ContextMenu.BitmapEffect>
     <MenuItem Header="Edit" x:Name="EditProgramContexMenu" Click="EditProgramContexMenu_Click"/>
     <MenuItem Header="Rmove" x:Name="RemoveProgramContexMenu" Click="RemoveProgramContexMenu_Click"/>
    </ContextMenu>
   </ListView.ContextMenu>
   <ListView.View>
    <GridView AllowsColumnReorder="True">
     <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="" Width="0"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerName}" Header="Customer Name" Width="220"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerAddress}" Header="Customer Address" Width="280"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerMobile}" Header="Customer Mobile Number" Width="160"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerEmail}" Header="Customer Email Address" Width="220"/>
    </GridView>
   </ListView.View>
  </ListView>
  <Label Content="Search Customer :" HorizontalAlignment="Left" Margin="396,101,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtCustomerSearch" Controls:TextboxHelper.Watermark="Search by Name" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="514,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
  <GroupBox Header="Customer Information" HorizontalAlignment="Left" Height="557" Margin="10,70,0,0" VerticalAlignment="Top" Width="363">
   <Grid HorizontalAlignment="Left" Height="514" Margin="0,0,-2,0" VerticalAlignment="Top" Width="353">
    <Label Content="Customer Name :" HorizontalAlignment="Left" Margin="6,3,0,0" VerticalAlignment="Top" Height="26" Width="102" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS"/>
                <TextBox x:Name="txtCustomerName" Controls:TextboxHelper.Watermark="Kazi Obaydollah" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Margin="147,6,0,0" TextChanged="txtCustomerName_TextChanged"/>
    <Label Content="Customer Address :" HorizontalAlignment="Left" Margin="6,34,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="116"/>
                <TextBox x:Name="txtCustomerAddress" Controls:TextboxHelper.Watermark="Dhaka" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="75" Margin="147,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AcceptsReturn="True"/>
    <Label Content="Phone Number :" HorizontalAlignment="Left" Margin="6,116,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="97"/>
                <TextBox x:Name="txtCustomerPhone" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,119,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Mobile Number :" HorizontalAlignment="Left" Margin="6,147,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="98"/>
                <TextBox x:Name="txtCustomerMobile" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="FAX Number :" HorizontalAlignment="Left" Margin="6,178,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="86"/>
                <TextBox x:Name="txtCustomerFax" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,181,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Email Address :" HorizontalAlignment="Left" Margin="6,209,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="94"/>
                <TextBox x:Name="txtCustomerEmail" Controls:TextboxHelper.Watermark="info@4ditsolution.com" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,212,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Note :" HorizontalAlignment="Left" Margin="6,240,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="42"/>
                <TextBox x:Name="txtCustomerNote" Controls:TextboxHelper.Watermark="Type Your Note Here" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="77" Margin="147,244,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AcceptsReturn="True"/>
   </Grid>
  </GroupBox>

 </Grid>

And here is C# code:



1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using BLL;
using ENTITY;

namespace JSMS.UI
{
    /// <summary>
    /// Interaction logic for CustomerInfo.xaml
    /// </summary>
    public partial class CustomerInfoUI : MetroWindow
    {
        CustomerInfo _customerInfoObj = new CustomerInfo();
        CustomerInfoManager _customerInfManagerObj = new CustomerInfoManager();
        List<CustomerInfo> _customerInfoList = new List<CustomerInfo>();


        public CustomerInfoUI()
        {
            InitializeComponent();
            LoadAllCustomerInfo();
            btnSaveCustomer.IsEnabled = false;
            btnDeleteCustomer.IsEnabled = false;
        }

        //Close Button Event
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            test testWindow = new test();
            testWindow.Show();
            this.Close();
        }

        //New Button Event
        private void btnNewCustomer_Click(object sender, RoutedEventArgs e)
        {
            txtCustomerName.Text = "";
            txtCustomerAddress.Text = "";
            txtCustomerPhone.Text = "";
            txtCustomerMobile.Text = "";
            txtCustomerFax.Text = "";
            txtCustomerEmail.Text = "";
            txtCustomerNote.Text = "";
        }

        //Save Customer
        private void btnSaveCustomer_Click(object sender, RoutedEventArgs e)
        {
            if (btnSaveCustomer.Content.ToString() == "Save Customer")
            {
                _customerInfoObj.CustomerName = txtCustomerName.Text;
                _customerInfoObj.CustomerAddress = txtCustomerAddress.Text;
                _customerInfoObj.CustomerPhone = txtCustomerPhone.Text;
                _customerInfoObj.CustomerMobile = txtCustomerMobile.Text;
                _customerInfoObj.CustomerFax = txtCustomerFax.Text;
                _customerInfoObj.CustomerEmail = txtCustomerEmail.Text;
                _customerInfoObj.CustomerNote = txtCustomerNote.Text;

                _customerInfManagerObj.SaveCustomerInfo(_customerInfoObj);

                txtCustomerName.Text = "";
                txtCustomerAddress.Text = "";
                txtCustomerPhone.Text = "";
                txtCustomerMobile.Text = "";
                txtCustomerFax.Text = "";
                txtCustomerEmail.Text = "";
                txtCustomerNote.Text = "";

                this.ShowMessageAsync("Confirmation", "Customer data is successfully saved");
                //MessageBox.Show("Customer data is successfully saved", "Confirmation");
                LoadAllCustomerInfo();
            }

            if (btnSaveCustomer.Content.ToString() == "Update Customer") // Update Customer
            {
                _customerInfoObj.CustomerName = txtCustomerName.Text;
                _customerInfoObj.CustomerAddress = txtCustomerAddress.Text;
                _customerInfoObj.CustomerPhone = txtCustomerPhone.Text;
                _customerInfoObj.CustomerMobile = txtCustomerMobile.Text;
                _customerInfoObj.CustomerFax = txtCustomerFax.Text;
                _customerInfoObj.CustomerEmail = txtCustomerEmail.Text;
                _customerInfoObj.CustomerNote = txtCustomerNote.Text;

                _customerInfManagerObj.UpdateCustomerInfo(_customerInfoObj);

                txtCustomerName.Text = "";
                txtCustomerAddress.Text = "";
                txtCustomerPhone.Text = "";
                txtCustomerMobile.Text = "";
                txtCustomerFax.Text = "";
                txtCustomerEmail.Text = "";
                txtCustomerNote.Text = "";

                this.ShowMessageAsync("Confirmation", "Customer data is successfully Update");
                //MessageBox.Show("Customer data is successfully Update", "Confirmation");
                btnSaveCustomer.Content = "Save Customer";
                btnSaveCustomer.IsEnabled = false;
                
            }
            
        }

        //Load data to ListView
        private void LoadAllCustomerInfo()
        {
            _customerInfoList = new List<CustomerInfo>();

            _customerInfoList = _customerInfManagerObj.LoadAllCustomerInfo();
            CustomerInfoListView.Items.Clear();

            if (_customerInfoList.Count > 0)
            {
                foreach (var item in _customerInfoList)
                {
                    CustomerInfoListView.Items.Add(item);
                }
            }
        }

        //Edit Context Menu code
        private void EditProgramContexMenu_Click(object sender, RoutedEventArgs e)
        {
            btnSaveCustomer.IsEnabled = true;

            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            txtCustomerName.Text = _customerInfoObj.CustomerName;
            txtCustomerAddress.Text = _customerInfoObj.CustomerAddress;
            txtCustomerPhone.Text = _customerInfoObj.CustomerPhone;
            txtCustomerMobile.Text = _customerInfoObj.CustomerMobile;
            txtCustomerFax.Text = _customerInfoObj.CustomerFax;
            txtCustomerEmail.Text = _customerInfoObj.CustomerEmail;
            txtCustomerNote.Text = _customerInfoObj.CustomerNote;
            btnSaveCustomer.Content = "Update Customer";
        }

        //Remove data from database
        private void RemoveProgramContexMenu_Click(object sender, RoutedEventArgs e)
        {
            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            _customerInfManagerObj.DeleteCustomerInfo(_customerInfoObj);
            LoadAllCustomerInfo();

        }

        //Remove data from database
        private void btnDeleteCustomer_Click(object sender, RoutedEventArgs e)
        {
            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            _customerInfManagerObj.DeleteCustomerInfo(_customerInfoObj);
            LoadAllCustomerInfo();
        }


        private void txtCustomerName_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnSaveCustomer.IsEnabled = true;
        }

        //Send data to another form
        private void CustomerInfoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (CustomerInfoListView.SelectedIndex > -1)
            {
                this.DialogResult = true;
            }
        }
    }
}

No comments:

Post a Comment