Tuesday, December 23, 2014

WPF XAML Horizontal Menu


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
     <Menu Height="26" Name="topMenu" VerticalAlignment="Top" Margin="0,0,0,-5" FontFamily="arial">
                <MenuItem Header="File" Name="FileMenu">
                    <MenuItem Header="Shop Setting"  Name="mnuShopSetting" Background="Transparent" Click="mnuShopSetting_Click"  />
                    <Separator />
                    <MenuItem Header="Exit" Name="mnuExit" Click="mnuExit_Click"/>
                </MenuItem>

                <MenuItem Header="Supplier" Name="SupplierMenu">
                    <MenuItem x:Name="mnuNewSupplier" Header="New Supplier" Click="mnuNewSupplier_Click"  />
                    <Separator />
                    <MenuItem Header="Supplier Status Report"   />
                </MenuItem>
                </MenuItem>
            </Menu>

WPF ListView StringFormat

In XAML follow string formating like this way:


1
<GridViewColumn DisplayMemberBinding="{Binding PurchaseDate, StringFormat='yyyy-MM-dd'}" Header="Date" Width="100"/>

C# WPF, Access Parent Window Method from Child Window

For access parent window method from child window, first make parent window method as PUBLIC, then follow below code for child window.


 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
//Declear ParentWindow as GlobalPreview
        SupplierDueListUI globalPrewindow;

public SupplierDueCollectionUI(SupplierDueListUI prewindow)
        {
            InitializeComponent();
            globalPrewindow = prewindow; // Initial prewindow
        }

private void btnUpdatePayment_Click(object sender, RoutedEventArgs e)
        {
            PurchaseInfo purchaseInfoObj = new PurchaseInfo();

            purchaseInfoObj.Id = this._purchaseInfoObj.Id;
            purchaseInfoObj.ProductId = this._purchaseInfoObj.ProductId;
            purchaseInfoObj.SupplierId = this._purchaseInfoObj.SupplierId;
            purchaseInfoObj.TotalDue = Convert.ToInt32(txtTotalDue.Text);
            purchaseInfoObj.TotalPay = Convert.ToInt32(txtTotalPay.Text);

            if (purchaseInfoObj.TotalDue <= 0)
            {
                purchaseInfoObj.IsFullPaid = true;
            }

            _purchaseManagerObj.UpdatePurchasePayment(purchaseInfoObj);

            globalPrewindow.LoadAllPurchaseDueBill(); //Load Parent window method

            MessageBox.Show("Successfully Updated", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);

            this.Close();
        }

Saturday, December 20, 2014

C# Generate an Unique code

This method use DateTime Ticks for generate an Unique 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
//Generate Unique code using DateTime
        static string uniqueCode()
        {
            string characters = "K13A56Z89I";
            string time = DateTime.UtcNow.Ticks.ToString();
            var code = "";
            for (var i = 0; i < characters.Length; i += 2)
            {
                if ((i + 2) <= time.Length)
                {
                    var number = int.Parse(time.Substring(i, 2));
                    if (number > characters.Length - 1)
                    {
                        var one = double.Parse(number.ToString().Substring(0, 1));
                        var two = double.Parse(number.ToString().Substring(1, 1));
                        code += characters[Convert.ToInt32(one)];
                        code += characters[Convert.ToInt32(two)];
                    }
                    else
                        code += characters[number];
                }
            }
            return code;
        }

C# Entity, Load Data from Child window to Parent window

In child window create an event and use code like this:

1
2
3
4
5
6
7
8
//Send data to Parent form
        private void CustomerInfoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (CustomerInfoListView.SelectedIndex > -1)
            {
                this.DialogResult = true;
            }
        }

Now in Parent window create a button event for show child window and get data. Use code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//Get data from child window and set it to parent window
        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            CustomerInfoUI customerUI = new CustomerInfoUI(); //Child window object

            if (customerUI.ShowDialog() == true) // Set condition for show child window and do some operation
            {
                CustomerInfo customerObj = new CustomerInfo(); //Child window entity object
                customerObj = customerUI.CustomerInfoListView.SelectedItem as CustomerInfo; //Load child window listview data to entity
                txtCustomerName.Text = customerObj.CustomerName; // set data to parent window text field
                txtCustomerId.Text = customerObj.Id.ToString();// set data to parent window text field
                this._customerInfoObj.Id = customerObj.Id; // set child window ID to parent window ID

                customerUI.Close(); //Close child window
            }
        }


C# with Entity, Send Data from Parent Form to Child Form

In Child Form Create an entity object as public and set all form field with that entity property. Like This code:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Declare Purchase Entity Object as Public
        public PurchaseInfo _purchaseInfoObj = new PurchaseInfo();


//Load Purchase Data from Parent Window to Child Form
        public void LoadDataFromDueList()
        {
            txtProductID.Text = this._purchaseInfoObj.ProductId.ToString();
            txtProductName.Text = this._purchaseInfoObj.ProductName;
            txtSupplierName.Text = this._purchaseInfoObj.SupplierName;
            txtTotalPurchaseAmount.Text = this._purchaseInfoObj.TotalPurchaseAmount.ToString();
            txtTotalDue.Text = this._purchaseInfoObj.TotalDue.ToString();
        }


Now in Parent Form, create an Event and write code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//Collect Data from Listview and Send it to child form
        private void MakePaymentContexMenu_Click(object sender, RoutedEventArgs e)
        {
            if (PurchaseDueListView.SelectedIndex > -1)
            {
                SupplierDueCollectionUI supplierDueCollectionUI = new SupplierDueCollectionUI();
                PurchaseInfo purchaseObj = new PurchaseInfo();
                purchaseObj = PurchaseDueListView.SelectedItem as PurchaseInfo; // select and load data from listview

                supplierDueCollectionUI._purchaseInfoObj = purchaseObj; // send it to public entity _purchaseInfoObj
                supplierDueCollectionUI.LoadDataFromDueList(); // Call data load method from child form
                supplierDueCollectionUI.ShowDialog(); // show child form
            }
        }

Tuesday, November 25, 2014

WPF C# ComboBox with DataBind

Step - 1 (XAML)

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
       <ComboBox HorizontalAlignment="Left" Margin="168,54,0,0" VerticalAlignment="Top" Width="197" x:Name="cmbProductCat">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding CategoryTitle}" Width="60"/>
                        <!--<TextBlock Text="|"/>
                        <TextBlock Text="{Binding DepartmentName}" Width="60"/>-->

                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

Step - 2 (UI Code)
1
2
3
4
5
6
7
8
9
private void LoadProductCatComboBox()
        {
            cmbProductCat.Items.Clear();
            _productCategoryInfoList = _productCategoryInfoManager.LoadAllProductCategoryInfo();
            foreach (ProductCategoryInfo pCatInf in _productCategoryInfoList)
            {
                cmbProductCat.ItemsSource = _productCategoryInfoList;
            }
        }

Step - 3 (BLL)
1
2
3
4
public List<ProductCategoryInfo> LoadAllProductCategoryInfo()
        {
            return productCatInfoGetway.LoadAllProductCategoryInfo();
        }

Step -4 (DAL)

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public List<ProductCategoryInfo> LoadAllProductCategoryInfo()
        {
            List<ProductCategoryInfo> productCategoryInfoList = new List<ProductCategoryInfo>();

            foreach (var p in (from c in dataContext.tbl_ProductCategories select c).Distinct())
            {
                ProductCategoryInfo productCatInfoObj = new ProductCategoryInfo();
                productCatInfoObj.Id = p.ID;
                productCatInfoObj.CategoryTitle = p.CategoriesTitle;

                productCategoryInfoList.Add(productCatInfoObj);
            }
            return productCategoryInfoList;
        }