private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gridPlayingBoardTitle.SelectedRows.Count == 0)
            {
                DataValidationMesaage.NoSelectedItemFromList(gridPlayingBoardTitle.Text);

                return;
            }

            DataBaseDataContext myConnection = Setting.DataBase;

            #region PlayingBoardTitle Casting

            int playingBoardTitleId = int.Parse(gridPlayingBoardTitle.SelectedRows[0].Cells[1].Value.ToString());

            if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
            {
                DataValidationMesaage.NoDataInBank();

                return;
            }

            PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

            #endregion

            txtTitle.Text = playingBoardTitle.Title;

            FormManagement.EnableYesNo(this.Controls);

            txtTitle.Focus();

            //myConnection.Dispose();
        }
        private void FrmSetRaspberryRelays_Load(object sender, EventArgs e)
        {
            DataBaseDataContext myConnection = Setting.DataBase;

            RaspBerryPlayingBoard.ShowList(lstRaspberryRelays, myConnection);

            RaspberryPin.LoadComboBoxAvailables(cmbRaspberryRelay, myConnection);

            PlayingBoardGroupTitle.LoadComboBox(cmbPlayingBoardGroupTitle, myConnection);

            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                #region PlayingBoardGroupTitle Cast

                int playingBoardGroupTitleId = ((PlayingBoardGroupTitle)cmbPlayingBoardGroupTitle.SelectedItem).ID;

                if (!PlayingBoardGroupTitle.Validation(playingBoardGroupTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardGroupTitle playingBoardGroupTitle = PlayingBoardGroupTitle.Get(playingBoardGroupTitleId,
                                                                                           myConnection);

                #endregion

                PlayingBoardTitle.LoadComboBox_By_PlayingBoardGroupTitle(cmbPlayingBoardTitle, playingBoardGroupTitle,
                                                                         myConnection);

                if (myConnection.PlayingBoardTitles.Any(a => a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    #region PlayingBoardTitle Cast

                    int playingBoardTitleId = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

                    if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
                    {
                        DataValidationMesaage.NoDataInBank();

                        return;
                    }

                    PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

                    #endregion

                    PlayingBoard.LoadComboBoxByFilter(cmbPlayingBoard, playingBoardTitle, myConnection);
                }
            }
        }
        private void btnYes_Click(object sender, EventArgs e)
        {
            DataBaseDataContext myConnection = Setting.DataBase;

            if (string.IsNullOrEmpty(txtTitle.Text) || string.IsNullOrWhiteSpace(txtTitle.Text))
            {
                DataValidationMesaage.BlankTextBox("عنوان");

                return;
            }

            #region PlayingBoardTitle Casting

            int playingBoardTitleID = int.Parse(gridPlayingBoardTitle.SelectedRows[0].Cells[1].Value.ToString());

            if (!PlayingBoardTitle.Validation(playingBoardTitleID, myConnection))
            {
                DataValidationMesaage.NoDataInBank();

                return;
            }

            PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleID, myConnection);

            #endregion

            if (myConnection.PlayingBoardTitles.Any(a => a.Title == txtTitle.Text.Trim() && a.ID != playingBoardTitleID))
            {
                DataValidationMesaage.DuplicateData(txtTitle.Text);

                return;
            }

            PlayingBoardTitle.Edit(playingBoardTitle, txtTitle.Text.Trim(),
                                   myConnection);

            DataValidationMesaage.EditMessage();

            ClearTextBox();

            txtTitle.Focus();

            PlayingBoardTitle.LoadGrid_By_PlayingBoardGroupTitle(gridPlayingBoardTitle,
                                                                 FrmPlayingBoard.SelectedPlayingBoardGroupTitle, myConnection);

            FormManagement.DisableYesNo(this.Controls);

            //myConnection.Dispose();
        }
        private void FrmPlayingBoard_Load(object sender, EventArgs e)
        {
            DataBaseDataContext myConnection = Setting.DataBase;

            PlayingBoardGroupTitle.LoadComboBox(cmbPlayingBoardGroupTitle, myConnection);

            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                #region PlayingBoardGroupTitle Cast

                int playingBoardGroupTitleId = ((PlayingBoardGroupTitle)cmbPlayingBoardGroupTitle.SelectedItem).ID;

                if (!PlayingBoardGroupTitle.Validation(playingBoardGroupTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardGroupTitle playingBoardGroupTitle = PlayingBoardGroupTitle.Get(playingBoardGroupTitleId,
                                                                                           myConnection);

                #endregion

                if (myConnection.PlayingBoardTitles.Any(a => a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    #region PlayingBoardTitle Cast

                    int playingBoardTitleId = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

                    if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
                    {
                        DataValidationMesaage.NoDataInBank();

                        return;
                    }

                    PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

                    #endregion

                    PlayingBoard.LoadGridColorful_By_PlayingBoardTitle(gridPlayingBoard, playingBoardTitle, myConnection);
                }
            }

            myConnection.Dispose();
        }
        private void cmbPlayingBoardGroupTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBaseDataContext myConnection = Setting.DataBase;

            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                #region PlayingBoardGroupTitle Cast

                int playingBoardGroupTitleId = ((PlayingBoardGroupTitle)cmbPlayingBoardGroupTitle.SelectedItem).ID;

                if (!PlayingBoardGroupTitle.Validation(playingBoardGroupTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardGroupTitle playingBoardGroupTitle = PlayingBoardGroupTitle.Get(playingBoardGroupTitleId,
                                                                                           myConnection);

                #endregion

                PlayingBoardTitle.LoadComboBox_By_PlayingBoardGroupTitle(cmbPlayingBoardTitle, playingBoardGroupTitle,
                                                                         myConnection);

                if (myConnection.PlayingBoardTitles.Any(a => a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    #region PlayingBoardTitle Cast

                    int playingBoardTitleId = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

                    if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
                    {
                        DataValidationMesaage.NoDataInBank();

                        return;
                    }

                    PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

                    #endregion

                    PlayingBoard.LoadComboBoxByFilter(cmbPlayingBoard, playingBoardTitle, myConnection);
                }
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridPlayingBoardTitle.SelectedRows.Count == 0)
            {
                DataValidationMesaage.NoSelectedItemFromList(gridPlayingBoardTitle.Text);

                return;
            }

            DataBaseDataContext myConnection = Setting.DataBase;

            #region PlayingBoardTitle Casting

            int playingBoardTitleId = int.Parse(gridPlayingBoardTitle.SelectedRows[0].Cells[1].Value.ToString());

            if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
            {
                DataValidationMesaage.NoDataInBank();

                return;
            }

            PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

            #endregion

            if (playingBoardTitle.PlayingBoards.Any())
            {
                DataValidationMesaage.DataInUse(playingBoardTitle.Title, gridPlayingBoardTitle.Text);

                return;
            }
            DialogResult message = DataValidationMesaage.ConfirmDeleteData(playingBoardTitle.Title);

            if (message == DialogResult.Yes)
            {
                PlayingBoardTitle.Delete(playingBoardTitle, myConnection);

                DataValidationMesaage.DeleteMessage();

                PlayingBoardTitle.LoadGrid_By_PlayingBoardGroupTitle(gridPlayingBoardTitle,
                                                                     FrmPlayingBoard.SelectedPlayingBoardGroupTitle, myConnection);
            }
            //myConnection.Dispose();
        }
        private void cmbPlayingBoardGroupTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridPlayingBoard.Rows.Clear();

            DataBaseDataContext myConnection = Setting.DataBase;

            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                btnAddPlayingboardTitle.Visible = true;

                #region PlayingBoardGroupTitle Cast

                int playingBoardGroupTitleId = ((PlayingBoardGroupTitle)cmbPlayingBoardGroupTitle.SelectedItem).ID;

                if (!PlayingBoardGroupTitle.Validation(playingBoardGroupTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardGroupTitle playingBoardGroupTitle = PlayingBoardGroupTitle.Get(playingBoardGroupTitleId,
                                                                                           myConnection);

                #endregion

                PlayingBoardTitle.LoadComboBox_By_PlayingBoardGroupTitle(cmbPlayingBoardTitle, playingBoardGroupTitle,
                                                                         myConnection);

                if (myConnection.PlayingBoardTitles.Any(a => a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    #region PlayingBoardTitle Cast

                    int playingBoardTitleId = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

                    if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
                    {
                        DataValidationMesaage.NoDataInBank();

                        return;
                    }

                    PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId, myConnection);

                    #endregion

                    PlayingBoard.LoadGridColorful_By_PlayingBoardTitle(gridPlayingBoard, playingBoardTitle, myConnection);
                }
            }

            else
            {
                btnAddPlayingboardTitle.Visible = false;

                cmbPlayingBoardTitle.Items.Add("گروه بازی کامل شود");

                cmbPlayingBoardTitle.SelectedIndex = 0;

                return;
            }
        }
        private void btnYes_Click(object sender, EventArgs e)
        {
            #region Check Blank txtNumber

            if (string.IsNullOrEmpty(txtNumber.Text.Trim()) && string.IsNullOrWhiteSpace(txtNumber.Text.Trim()))
            {
                DataValidationMesaage.BlankTextBox("شماره دستگاه");

                return;
            }

            #endregion

            DataBaseDataContext myConnection = Setting.DataBase;

            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                #region PlayingBoardTitle Cast

                int playingBoardTitleId = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

                if (!PlayingBoardTitle.Validation(playingBoardTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(playingBoardTitleId,
                                                                            myConnection);

                #endregion

                #region Check Duplicate cmbPlayingBoardTitle and txtNumber
                if (
                    myConnection.PlayingBoards.Any(
                        a =>
                        a.PlayingBoardTitle.Title.Equals(txtNumber.Text.Trim())))
                {
                    MessageBox.Show("کاربر گرامی مقادیر شماره میز و عنوان بازی نمیتوانند یکسان باشند.",
                                    "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }


                #endregion

                #region Check Duplicate Data

                if (
                    myConnection.PlayingBoards.Any(
                        a =>
                        a.Number.Equals(txtNumber.Text.Trim()) &&
                        a.PlayingBoardTitle.Equals(playingBoardTitle)))
                {
                    DataValidationMesaage.DuplicateData("شماره میز");

                    return;
                }

                #endregion

                #region PlayingBoard Cast

                int PlayingBoardId = (int)gridPlayingBoard.SelectedRows[0].Cells[1].Value;

                if (!PlayingBoard.Validation(PlayingBoardId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoard playingBoard = PlayingBoard.Get(PlayingBoardId, myConnection);

                #endregion

                PlayingBoard.Edit(playingBoard, playingBoardTitle, txtNumber.Text.Trim(), true, myConnection);

                DataValidationMesaage.EditMessage();

                clearTextBox();

                FormManagement.DisableYesNoIncludingComboBox(this.Controls);

                PlayingBoard.LoadGridColorful_By_PlayingBoardTitle(gridPlayingBoard, playingBoardTitle, myConnection);
            }

            myConnection.Dispose();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            DataBaseDataContext myConnection = Setting.DataBase;

            if (string.IsNullOrEmpty(txtNumber.Text) || string.IsNullOrWhiteSpace(txtNumber.Text))
            {
                DataValidationMesaage.BlankTextBox("شماره دستگاه");

                return;
            }
            if (myConnection.PlayingBoardGroupTitles.Any())
            {
                #region PlayingBoardGroupTitle Cast

                int playingBoardGroupTitleId = ((PlayingBoardGroupTitle)cmbPlayingBoardGroupTitle.SelectedItem).ID;

                if (!PlayingBoardGroupTitle.Validation(playingBoardGroupTitleId, myConnection))
                {
                    DataValidationMesaage.NoDataInBank();

                    return;
                }

                PlayingBoardGroupTitle playingBoardGroupTitle = PlayingBoardGroupTitle.Get(playingBoardGroupTitleId,
                                                                                           myConnection);

                #endregion

                if (!myConnection.PlayingBoardTitles.Any(a => a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    DataValidationMesaage.BlankComboBox("عنوان دستگاه");

                    return;
                }

                if (
                    myConnection.PlayingBoardTitles.Any(
                        a =>
                        a.Title.Equals(txtNumber.Text.Trim()) &&
                        a.PlayingBoardGroupTitle.Equals(playingBoardGroupTitle)))
                {
                    MessageBox.Show("کاربر گرامی مقادیر شماره میز و عنوان بازی نمیتوانند یکسان باشند.",
                                    "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
            }

            #region PlayingBoardTitle Cast

            int id = ((PlayingBoardTitle)cmbPlayingBoardTitle.SelectedItem).ID;

            if (!PlayingBoardTitle.Validation(id, myConnection))
            {
                DataValidationMesaage.NoDataInBank();

                return;
            }

            PlayingBoardTitle playingBoardTitle = PlayingBoardTitle.Get(id, myConnection);

            #endregion

            if (myConnection.PlayingBoards.Any(a => a.PlayingBoardTitle == playingBoardTitle && a.Number.Equals(txtNumber.Text.Trim())))
            {
                DataValidationMesaage.DuplicateData("شماره میز");

                return;
            }

            PlayingBoard playingBoard = PlayingBoard.Insert(playingBoardTitle, txtNumber.Text, true, myConnection);

            DataValidationMesaage.AcceptMessage();

            DialogResult result1 = MessageBox.Show("آیا مایلید برق میز بازی ثبت شده را به صورت سیستمی کنترل نمایید؟", "کاربر گرامی", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result1 == DialogResult.Yes)
            {
                btnRaspberryRelay.PerformClick();
            }


            DialogResult result2 = MessageBox.Show("آیا مایلید برای میز بازی ثبت شده بهاء تعیین نمایید؟", "کاربر گرامی", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result2 == DialogResult.Yes)
            {
                btnPrice.PerformClick();
            }

            clearTextBox();

            PlayingBoard.LoadGridColorful_By_PlayingBoardTitle(gridPlayingBoard, playingBoardTitle, myConnection);
        }