Accounting Project Part 8 Accounts Controls Form in C#

Hi, Dear's here we learn how to implement stock in Accounting Project  POS in Visual Studio using C#. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like.

In this tutorial we will cover all the below mention topics:
    i)      How to Create AccountControlForms inside AccontForms Folder in out project visual studio?
    ii)     How to design Account Head form?
    iii)    Create AccountHead Method inside ComboHelper Class to retrive all account heads and fill combobox.
    iv)    How to Insert, update, delete and retrieve records from HeadControlTable and show inside datagridview(dgv)?

So if you want all question answers watch the video, and for source code scroll down.

vedio upload soon


Now Open Helpers Folder and past this code in ComboHelper.cs class
Note : use to retrieve all accounts head and fill combobox.

 public static void AccountHeads(ComboBox cmb)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("HeadID");
            dt.Columns.Add("HeadTitle");
            dt.Rows.Add("0", "--Select--");
            try
            {
                DataTable categorydt = DatabaseLayer.Select("Select HeadID, HeadTitle From AccountHeadTable Order By HeadTitle asc");
                if (categorydt != null)
                {
                    if (categorydt.Rows.Count > 0)
                    {
                        foreach (DataRow item in categorydt.Rows)
                        {
                            dt.Rows.Add(item[0].ToString(), item[1].ToString());
                        }
                    }
                }
            }
            catch
            {

            }

            cmb.DataSource = dt;
            cmb.DisplayMember = "HeadTitle";
            cmb.ValueMember = "HeadID";
        }



Design Account Control Form in C# / C-Sharp :
So in this example, we have account control form, show below in figure:

Source Code Account Controls Form : 
using POSAccounts.Helpers;
using System;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace POSAccounts.Forms.AccountForms
{
    public partial class FrmAccountControl : Form
    {
        public FrmAccountControl()
        {
            InitializeComponent();
            PanelBar.MouseDown += PanelBar_MouseDown;
        }

        private const int HT_CAPTION = 0x2; // what does this means
        private const int WM_NCLBUTTONDOWN = 0x00A1; // what does this means
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern bool ReleaseCapture();
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern int SendMessage(
        IntPtr hwnd,
        int wMsg,
        int wParam,
        int lParam);
        private void PanelBar_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                Rectangle rct = DisplayRectangle;
                if (rct.Contains(e.Location))
                {
                    ReleaseCapture();
                    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            }
        }

        // Retrive Data From Account Head Table using Select Method of Database layer
        private void FillGrid(string searchvalue)
        {
            try
            {
                string query = "Select HeadControlID [ID],  HeadID [ID], HeadTitle [Account Head] , ControlTitle [Account Control] FROM v_AccountControls";
                if (!string.IsNullOrEmpty(searchvalue))
                {
                    query = "Select HeadControlID [ID],  HeadID [ID], HeadTitle [Account Head] , ControlTitle [Account Control] FROM v_AccountControls Where (HeadTitle + ' ' + ControlTitle) like '%" + searchvalue.Trim() + "%'";
                }

                DataTable dt = DatabaseLayer.Select(query);
                dgv.DataSource = dt;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        //dgv.Columns[0].Width = 80; // Head ID
                        dgv.Columns[0].Visible = false; // HeadControlID
                        dgv.Columns[1].Visible = false; // Head ID
                        dgv.Columns[2].Width = 120; // HeadTitle
                        dgv.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Account Head Title
                    }
                }
            }
            catch
            {
                dgv.DataSource = null;
            }
        }

        // Validation Method for More Details Watch Vedio
        private void EnableComponents()
        {
            btnCancel.Visible = true;
            btnUpdate.Visible = true;
            btnSave.Visible = false;
            btnClear.Visible = false;
            dgv.Enabled = false;
            txtSearch.Enabled = false;
        }

        // Valition Method for More Details Watch Vedio
        private void ResetComponents()
        {
            btnCancel.Visible = false;
            btnUpdate.Visible = false;
            btnSave.Visible = true;
            btnClear.Visible = true;
            dgv.Enabled = true;
            txtSearch.Enabled = true;
            cmbAccountHead.SelectedIndex = 0;
            txtAccountControl.Clear();
            FillGrid(string.Empty);
        }

        private void ClearForm()
        {
            cmbAccountHead.SelectedIndex = 0;
            txtAccountControl.Clear();
        }

        private void btnAddAccountHead_Click(object sender, EventArgs e)
        {
            // Open Account Head Form
            FrmAccountHead frm = new FrmAccountHead();
            frm.ShowDialog();
            ComboHelper.AccountHeads(cmbAccountHead);// Reload Account Heads on close of Account Head Form

        }

        private void FrmAccountControl_Load(object sender, EventArgs e)
        {
            // retrive all accounts heads on form load and Fill cmbAccountHead Combobox
            ComboHelper.AccountHeads(cmbAccountHead);

            // Retrive all account controls list on form load and show datagridview
            FillGrid(string.Empty); 
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // using to close current form
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            FillGrid(txtSearch.Text.Trim()); // Filter Accounts Controls
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm(); // Clear Form Controls
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ResetComponents(); // Reset Form Controls / Cancel Updation
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();

                if (cmbAccountHead.SelectedIndex == 0)
                {
                    ep.SetError(cmbAccountHead, "Please Select Account Head!");
                    cmbAccountHead.Focus();
                    return;
                }

                // First Check Account Control Textbox, if it's is empty then show message to user, plese enter Account Controls Title
                if (txtAccountControl.Text.Trim().Length == 0)
                {
                    ep.SetError(txtAccountControl, "Please Enter Account Control Title!");
                    txtAccountControl.Focus();
                    return;
                }

                // Check Account Contorl is already registered or not
                DataTable existdt = DatabaseLayer.Select("Select * From v_AccountControls Where HeadID = '"+cmbAccountHead.SelectedValue+ "' and ControlTitle = '" + txtAccountControl.Text.Trim() + "'");
                if (existdt != null)
                {
                    if (existdt.Rows.Count > 0)
                    {
                        ep.SetError(txtAccountControl, "Already Exist!"); // if already Registered show this message
                        txtAccountControl.Focus();
                        txtAccountControl.SelectAll();
                        return;
                    }
                }

                // Now use database layer insert method to insert data in database in AccountControlTable.
                string insertquery = string.Format("Insert into HeadControlTable(HeadID,ControlTitle) values('{0}','{1}')", cmbAccountHead.SelectedValue, txtAccountControl.Text.Trim());
                bool result = DatabaseLayer.Insert(insertquery); // use insert method for insert Account Control in AccountControlTable
                if (result == true)
                {
                    MessageBox.Show("Registered Successfully!"); // if Insert show success message
                    FillGrid(string.Empty);
                    txtAccountControl.Clear();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // if Not Insert show success message
                }
            }
            catch
            {
                // incase of any issue is occure show this message
                MessageBox.Show("Some Unexptected issue is occure plz try agian!");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();

                if (cmbAccountHead.SelectedIndex == 0)
                {
                    ep.SetError(cmbAccountHead, "Please Select Account Head!");
                    cmbAccountHead.Focus();
                    return;
                }

                // First Check Account Control Textbox, if it's is empty then show message to user, plese enter Account Controls Title
                if (txtAccountControl.Text.Trim().Length == 0)
                {
                    ep.SetError(txtAccountControl, "Please Enter Account Control Title!");
                    txtAccountControl.Focus();
                    return;
                }

                // Check Account Contorl is already registered or not
                DataTable existdt = DatabaseLayer.Select("Select * From v_AccountControls Where HeadID = '" + cmbAccountHead.SelectedValue + "' and ControlTitle = '" + txtAccountControl.Text.Trim() + "' and HeadControlID = '"+ dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value + "'");
                if (existdt != null)
                {
                    if (existdt.Rows.Count > 0)
                    {
                        ep.SetError(txtAccountControl, "Already Exist!"); // if already Registered show this message
                        txtAccountControl.Focus();
                        txtAccountControl.SelectAll();
                        return;
                    }
                }

                // Now use database layer insert method to update data in database in AccountControlTable.
                string udpatequery = string.Format("update HeadControlTable set HeadID = '{0}',ControlTitle = '{1}' where HeadControlID = '{2}'", cmbAccountHead.SelectedValue, txtAccountControl.Text.Trim(), dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value);
                bool result = DatabaseLayer.Insert(udpatequery); // use update method for update Account Control in AccountControlTable
                if (result == true)
                {
                    MessageBox.Show("Updated Successfully!"); // if updated show success message
                    FillGrid(string.Empty);
                    txtAccountControl.Clear();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // if Not update show this message
                }
            }
            catch
            {
                // incase of any issue is occure show this message
                MessageBox.Show("Some Unexptected issue is occure plz try agian!");
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv != null)
            {
                if (dgv.Rows.Count > 0)
                {
                    if (dgv.SelectedRows.Count == 1)
                    {
                        if (MessageBox.Show("Are you sure you want to update selected Account Controls?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            cmbAccountHead.SelectedValue = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString(); // Head ID
                            txtAccountControl.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[2].Value.ToString(); // Account Controls
                            EnableComponents(); // Enable Update Controls
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Select One Record!");
                    }
                }
            }
        }
    }
}

Comments