public void ProductController(ProductsInfos PI, bool Update = false, bool Delete = false)
        {
            if (Delete)
            {
                var elv = ProductsList.Items.Find(PI.PID.ToString(), false);
                if (elv.Length > 0)
                {
                    this.ProductsList.Items.Remove(elv[0]);
                }
                ProductsInfos.AllProducts.Remove(PI.PID.ToString());
                ProductsInfos.TotalProducts--;
                ItemCountL.Text = ProductsInfos.TotalProducts.ToString();
                return;
            }
            string[] Items = new string[9];
            Items[0] = PI.PID.ToString();
            Items[1] = PI.PName;
            Items[2] = PI.PType;
            Items[3] = PI.Price.ToString();
            if (Program.isAdmin)
            {
                Items[4] = PI.PPrice.ToString();
            }
            Items[5] = PI.Count.ToString();
            Items[6] = PI.NoOfSales.ToString();
            Items[7] = PI.NeededCount.ToString();
            Items[8] = PI.Details;
            //name, type, price, purchasingprice, count, noofsales, needed, details, picurl) "+
            ListViewItem LV = new ListViewItem(Items);

            LV.Name        = LV.Text = PI.PID.ToString();
            LV.ToolTipText = "Name : " + PI.PName + Environment.NewLine +
                             "Type : " + PI.PType + Environment.NewLine +
                             "Price : " + PI.Price + Environment.NewLine +
                             "Purchasing Price : " + PI.PPrice + Environment.NewLine +
                             "Count : " + PI.Count + Environment.NewLine +
                             "Number of sales : " + PI.NoOfSales + Environment.NewLine +
                             "Needed count : " + PI.NeededCount + Environment.NewLine +
                             "Details : " + PI.Details;

            if (string.IsNullOrEmpty(PI.ImageURL))
            {
                LV.ImageKey = "LoginMonitor";
            }
            else
            {
                try
                {
                    if (!ProductsList.LargeImageList.Images.ContainsKey(PI.ImageURL))
                    {
                        if (this.ProductsList.InvokeRequired)
                        {
                            this.ProductsList.BeginInvoke(new MethodInvoker(
                                                              () => this.ProductsList.LargeImageList.Images.Add(PI.ImageURL, Image.FromFile(ProductsInfos.ImagesURL + PI.ImageURL))));
                        }
                        else
                        {
                            this.ProductsList.LargeImageList.Images.Add(PI.ImageURL, Image.FromFile(ProductsInfos.ImagesURL + PI.ImageURL));
                        }
                    }
                    if (!ProductsList.SmallImageList.Images.ContainsKey(PI.ImageURL))
                    {
                        if (this.ProductsList.InvokeRequired)
                        {
                            this.ProductsList.BeginInvoke(new MethodInvoker(
                                                              () => this.ProductsList.SmallImageList.Images.Add(PI.ImageURL, Image.FromFile(ProductsInfos.ImagesURL + PI.ImageURL))));
                        }
                        else
                        {
                            this.ProductsList.SmallImageList.Images.Add(PI.ImageURL, Image.FromFile(ProductsInfos.ImagesURL + PI.ImageURL));
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ProductsInfos.ImagesURL + PI.ImageURL + Environment.NewLine + ex.ToString());
                }
                LV.ImageKey = PI.ImageURL;
            }

            if (Update)
            {
                var elv = ProductsList.Items.Find(PI.PID.ToString(), false);
                if (elv.Length > 0)
                {
                    this.ProductsList.Items.Remove(elv[0]);
                }
                ProductsInfos.AllProducts.Remove(PI.PID.ToString());
                ProductsInfos.AllProducts.Add(PI.PID.ToString(), PI);
            }
            else
            {
                if (!ProductsInfos.AllProducts.ContainsKey(PI.PID.ToString()))
                {
                    ProductsInfos.AllProducts.Add(PI.PID.ToString(), PI);
                    ProductsInfos.TotalProducts++;
                    ItemCountL.Text = ProductsInfos.TotalProducts.ToString();
                }
            }
            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.Items.Add(LV)));
            }
            else
            {
                this.ProductsList.Items.Add(LV);
            }

            /*
             * string[] Items = new string[10];
             * Items[0] = MDR.GetString(0);
             * Items[1] = MDR.GetString(1);
             * Items[2] = MDR.GetString(2);
             * Items[3] = MDR.GetString(3);
             * Items[4] = MDR.GetString(4);
             * Items[5] = MDR.GetString(5);
             * Items[6] = MDR.GetString(6);
             * Items[7] = MDR.GetString(7);
             * Items[8] = MDR.GetString(8);
             * Items[9] = MDR.GetString(9);
             * //name, type, price, purchasingprice, count, noofsales, needed, details, picurl) "+
             * ListViewItem LV = new ListViewItem(Items);
             * LV.Name = LV.Text = MDR.GetString(0);
             * LV.ToolTipText = "Name : " + MDR.GetString(1) + Environment.NewLine +
             * "Type : " + MDR.GetString(2) + Environment.NewLine +
             * "Price : " + MDR.GetFloat(3) + Environment.NewLine +
             * "Purchasing Price : " + MDR.GetFloat(4) + Environment.NewLine +
             * "Count : " + MDR.GetInt32(5) + Environment.NewLine +
             * "Number of sales : " + MDR.GetInt32(6) + Environment.NewLine +
             * "Needed count : " + MDR.GetInt32(7) + Environment.NewLine +
             * "Details : " + MDR.GetString(8);
             * LV.ImageKey = "OnlineDisplay";
             * if (this.ProductsList.InvokeRequired)
             * {
             *  this.ProductsList.BeginInvoke(new MethodInvoker(
             *      () => this.ProductsList.Items.Add(LV)));
             * }
             * else
             * {
             *  this.ProductsList.Items.Add(LV);
             * }*/
            Application.DoEvents();
        }
        //  protected override void OnLoad(EventArgs e)
        public bool LoadProducts()
        {
            #region Images

            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.LargeImageList = new ImageList()));
            }
            else
            {
                this.ProductsList.LargeImageList = new ImageList();
            }

            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.LargeImageList.ImageSize = new Size(100, 100)));
            }
            else
            {
                this.ProductsList.LargeImageList.ImageSize = new Size(100, 100);
            }


            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.SmallImageList = new ImageList()));
            }
            else
            {
                this.ProductsList.SmallImageList = new ImageList();
            }

            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.SmallImageList.ImageSize = new Size(22, 31)));
            }
            else
            {
                this.ProductsList.SmallImageList.ImageSize = new Size(22, 31);
            }


            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.LargeImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor)));
            }
            else
            {
                this.ProductsList.LargeImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor);
            }

            if (this.ProductsList.InvokeRequired)
            {
                this.ProductsList.BeginInvoke(new MethodInvoker(
                                                  () => this.ProductsList.SmallImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor)));
            }
            else
            {
                this.ProductsList.SmallImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor);
            }

            //*********
            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.LargeImageList = new ImageList()));
            }
            else
            {
                this.SalesLogView.LargeImageList = new ImageList();
            }

            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.LargeImageList.ImageSize = new Size(100, 100)));
            }
            else
            {
                this.SalesLogView.LargeImageList.ImageSize = new Size(100, 100);
            }


            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.SmallImageList = new ImageList()));
            }
            else
            {
                this.SalesLogView.SmallImageList = new ImageList();
            }

            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.SmallImageList.ImageSize = new Size(22, 31)));
            }
            else
            {
                this.SalesLogView.SmallImageList.ImageSize = new Size(22, 31);
            }


            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.LargeImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor)));
            }
            else
            {
                this.SalesLogView.LargeImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor);
            }

            if (this.SalesLogView.InvokeRequired)
            {
                this.SalesLogView.BeginInvoke(new MethodInvoker(
                                                  () => this.SalesLogView.SmallImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor)));
            }
            else
            {
                this.SalesLogView.SmallImageList.Images.Add("LoginMonitor", Properties.Resources.LoginMonitor);
            }
            #endregion

            if (!Directory.Exists(ProductsInfos.ImagesURL))
            {
                Directory.CreateDirectory(ProductsInfos.ImagesURL);
            }
            MySqlConnection conn = new MySqlConnection(Program.connStr);

            conn = new MySqlConnection(Program.connStr);
            conn.Open();
            string       s0  = "use `aleman_cafe_server`; SELECT * FROM `products`";
            MySqlCommand cmd = new MySqlCommand(s0, conn);
            try
            {
                var rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    ProductsInfos PI = new ProductsInfos(rdr);
                    ProductController(PI);
                    if (!ProductsInfos.PTypes.Contains(rdr.GetString(2)))
                    {
                        ProductsInfos.PTypes.Add(rdr.GetString(2));
                    }
                    int MID = rdr.GetInt32(0);
                    if (MID >= ProductsInfos.NextID)
                    {
                        ProductsInfos.NextID = (MID + 1);
                    }
                }
                rdr.Close();
                conn.Close();
            }
            catch (Exception ee)
            {
                conn.Close();
                //  MessageBox.Show("There are an error and database cannot be loaded." + Environment.NewLine + ee.ToString(), "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (MessageBox.Show("There are an error and database cannot be loaded, do you want to create new database?" + Environment.NewLine + ee.ToString(), "Database Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {
                    MyDatabase.CreateDB();
                }
                this.ReloadList();
            }

            s0 = "use `aleman_cafe_server`; SELECT * FROM `productslog`";
            conn.Open();
            var TodayLogs = new List <ProductsLog>();
            cmd = new MySqlCommand(s0, conn);
            try
            {
                var rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    ProductsLog PL = new ProductsLog(rdr);
                    if (PL.At.Date == DateTime.Now.Date && !TodayLogs.Contains(PL))
                    {
                        TodayLogs.Add(PL);
                    }
                    ProductLogController(PL);
                }
                rdr.Close();
                conn.Close();
            }
            catch (Exception ee)
            {
                conn.Close();
                //  MessageBox.Show("There are an error and database cannot be loaded." + Environment.NewLine + ee.ToString(), "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (MessageBox.Show("There are an error and database 'productslog' cannot be loaded, do you want to create new database?" + Environment.NewLine + ee.ToString(), "Database Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {
                    MyDatabase.CreateDB();
                }
                this.ReloadList();
            }
            var WU = new List <int>();
            foreach (var N in TodayLogs)
            {
                var PI = ProductsInfos.AllProducts[N.ID.ToString()];
                if (N.LogType == ProductsLog.LogTypes.Sold)
                {
                    PI.SoldToday += N.Quantity;
                }
                else
                {
                    PI.SoldToday -= N.Quantity;
                }
                if (!WU.Contains(PI.PID))
                {
                    WU.Add(PI.PID);
                }
            }

            foreach (int i in WU)
            {
                var PI = ProductsInfos.AllProducts[i.ToString()];
                ProductController(PI, true);
            }

            /*
             * this.thumbnailsToolStripMenuItem.Click += new System.EventHandler(this.thumbnailsToolStripMenuItem_Click);
             * this.tilesToolStripMenuItem.Click += new System.EventHandler(this.tilesToolStripMenuItem_Click);
             * this.iconsToolStripMenuItem.Click += new System.EventHandler(this.iconsToolStripMenuItem_Click);
             * this.listToolStripMenuItem.Click += new System.EventHandler(this.listToolStripMenuItem_Click);
             * this.detailsToolStripMenuItem.Click += new System.EventHandler(this.detailsToolStripMenuItem_Click);
             * this.AddProductButton.Click += new System.EventHandler(this.AddProductButton_Click);
             */
            ProductsList.MouseClick           += new MouseEventHandler(ProductsList_MouseClick);
            ProductsList.MouseDoubleClick     += new MouseEventHandler(ProductsList_MouseClick);
            ProductsList.KeyDown              += new KeyEventHandler(ProductsList_KeyPress);
            ProductsList.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(ProductsList_ItemSelectionChanged);
            ProductsList.ColumnClick          += ProductsList_ColumnClick;

            SearchText.TextChanged += SearchText_TextChanged;

            FromdatePicker.ValueChanged += FromdatePicker_ValueChanged;
            TodatePicker.ValueChanged   += FromdatePicker_ValueChanged;
            return(true);
        }