//Event-Handlers of Runtime GUI
        //----------------------------------------------------------------------
        private void runtimeGUIRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked)
            {
                FlowLayoutPanel modeFLP = rb.Parent as FlowLayoutPanel;
                GroupBox gb = modeFLP.Parent as GroupBox;
                //IF Goupbox contain cmdFlowLayoutPanel is general, if not Groupbox is a Controller Groupbox
                Control[] list = gb.Controls.Find("cmdFlowLayoutPanel", true);
                if (list.Length > 0)
                {
                    FlowLayoutPanel cmdFLP = list[0] as FlowLayoutPanel;
                    cmdFLP.Controls.Clear();
                    //Always one or more but it can zero --- MODEITEM
                    DataRow[] drAitems = DSDatabase.GUIGroupboxModeItem.Select("GUIGroupboxModeID=" + rb.Name, "GUIGroupboxModeItemSequence");
                    if (drAitems.Length > 0)
                    {
                        List<string> strLcombosource = new List<string>();
                        foreach (DataRow dritem in drAitems)
                        {
                            //ONE item - It can MORE ITEMCMD
                            DataRow[] drAitemcmds = dritem.GetChildRows("FK_GUIGroupboxModeItemCmd_1");
                            if (drAitemcmds.Length > 0)
                            {
                                foreach (DataRow dritemcmd in drAitemcmds)
                                {
                                    //One ItemCmd - One Cmd always --- CMD
                                    DataRow drcmd = dritemcmd.GetParentRow("FK_GUIGroupboxModeItemCmd_0");
                                    if (!bDRIsNull(drcmd))
                                    {
                                        //If this is a combobox, cmd add to comboboxsource
                                        if (Convert.ToInt32(dritem["GUIGroupboxModeItemType"]) == 255) strLcombosource.Add(drcmd["CmdName"].ToString());
                                        //Null or many parameters for a cmd ---PARAMETER (CmdParameter Table)
                                        DataRow[] drAparameters = DSDatabase.CmdParameter.Select("CmdID=" + drcmd["CmdID"].ToString(), "ParameterSequence");
                                        //If no parameter, no iteration
                                        if (drAparameters.Length > 0)
                                        {
                                            foreach (DataRow drparameter in drAparameters)
                                            {
                                                //Null or ONE datarow or a parameter and his modeitem --- PARAMETER (GUIGroupboxModeItemParameter Table)
                                                DataRow[] drAmodeitemparameter = DSDatabase.GUIGroupboxModeItemParameter.Select("GUIGroupboxModeItemCmdID=" + dritemcmd["GUIGroupboxModeItemCmdID"].ToString() + " AND ParameterID=" + drparameter["ParameterID"].ToString() + " AND ParameterValue IS NOT NULL");
                                                if (drAmodeitemparameter.Length == 0)
                                                {
                                                    //One parameter - One datarow --- PARAMETER (Parameter Table)
                                                    DataRow drparam = drparameter.GetParentRow("FK_CmdParameter_0", DataRowVersion.Current);
                                                    if (!bDRIsNull(drparam))
                                                    {
                                                        FlowLayoutPanel parameterFLP = gb.Controls.Find("parameterFlowLayoutPanel", true)[0] as FlowLayoutPanel;
                                                        //IF parameterFLP don't contain this parameter yet, it added to parameterFLP
                                                        if (!parameterFLP.Controls.ContainsKey(drparam["ParameterID"].ToString()))
                                                        {
                                                            //Textbox
                                                            if (Convert.ToInt32(drparam["ParameterType"]) == 0)
                                                            {
                                                                runtimeGUIRCPTextBox = new RCPTextBox();
                                                                runtimeGUIRCPTextBox.Name = drparam["ParameterID"].ToString();
                                                                runtimeGUIRCPTextBox.Text = drparam["ParameterName"].ToString();
                                                                if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTextBox.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                else runtimeGUIRCPTextBox.Min = int.MinValue;
                                                                if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTextBox.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                else runtimeGUIRCPTextBox.Max = int.MaxValue;
                                                                if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTextBox.Default = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                else runtimeGUIRCPTextBox.Default = 0;
                                                                runtimeGUIRCPTextBox.Value = runtimeGUIRCPTextBox.Default;
                                                                runtimeGuiToolTip = new ToolTip();
                                                                runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTextBox, drparam["ParameterComment"].ToString());
                                                                parameterFLP.Controls.Add(runtimeGUIRCPTextBox);
                                                            }
                                                            //NumericUpDown
                                                            else if (Convert.ToInt32(drparam["ParameterType"]) == 1)
                                                            {
                                                                runtimeGUIRCPNumericUpDown = new RCPNumericUpDown();
                                                                runtimeGUIRCPNumericUpDown.Name = drparam["ParameterID"].ToString();
                                                                runtimeGUIRCPNumericUpDown.Text = drparam["ParameterName"].ToString();
                                                                if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Min = Convert.ToDecimal(drparam["ParameterMin"]);
                                                                else runtimeGUIRCPNumericUpDown.Min = decimal.MinValue;
                                                                if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Max = Convert.ToDecimal(drparam["ParameterMax"]);
                                                                else runtimeGUIRCPNumericUpDown.Max = decimal.MaxValue;
                                                                if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                else runtimeGUIRCPNumericUpDown.Value = 0;
                                                                runtimeGuiToolTip = new ToolTip();
                                                                runtimeGuiToolTip.SetToolTip(runtimeGUIRCPNumericUpDown, drparam["ParameterComment"].ToString());
                                                                parameterFLP.Controls.Add(runtimeGUIRCPNumericUpDown);
                                                            }
                                                            //Trackbar
                                                            else if (Convert.ToInt32(drparam["ParameterType"]) == 2)
                                                            {
                                                                runtimeGUIRCPTrackbar = new RCPTrackbar();
                                                                runtimeGUIRCPTrackbar.Name = drparam["ParameterID"].ToString();
                                                                runtimeGUIRCPTrackbar.Text = drparam["ParameterName"].ToString();
                                                                if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTrackbar.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                else runtimeGUIRCPTrackbar.Min = int.MinValue;
                                                                if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTrackbar.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                else runtimeGUIRCPTrackbar.Max = int.MaxValue;
                                                                if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTrackbar.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                else runtimeGUIRCPTrackbar.Value = 0;
                                                                runtimeGuiToolTip = new ToolTip();
                                                                runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTrackbar, drparam["ParameterComment"].ToString());
                                                                parameterFLP.Controls.Add(runtimeGUIRCPTrackbar);
                                                            }
                                                            //Trackbar with NumericUpDown
                                                            else if (Convert.ToInt32(drparam["ParameterType"]) == 3)
                                                            {
                                                                runtimeGUIRCPTrackbarWithNumericUpDown = new RCPTrackbarWithNumericUpDown();
                                                                runtimeGUIRCPTrackbarWithNumericUpDown.Name = drparam["ParameterID"].ToString();
                                                                runtimeGUIRCPTrackbarWithNumericUpDown.Text = drparam["ParameterName"].ToString();
                                                                if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                else runtimeGUIRCPTrackbarWithNumericUpDown.Min = int.MinValue;
                                                                if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                else runtimeGUIRCPTrackbarWithNumericUpDown.Max = int.MaxValue;
                                                                if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                else runtimeGUIRCPTrackbarWithNumericUpDown.Value = 0;
                                                                runtimeGuiToolTip = new ToolTip();
                                                                runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTrackbarWithNumericUpDown, drparam["ParameterComment"].ToString());
                                                                parameterFLP.Controls.Add(runtimeGUIRCPTrackbarWithNumericUpDown);
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        throw new DBException("There aren't usable parameter for " + drcmd["CmdName"].ToString() + " in " + dritem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                                    }
                                                }
                                            }
                                        }

                                    }
                                    else
                                    {
                                        throw new DBException("There aren't any cmd for " + dritem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                    }
                                }
                            }
                            else
                            {
                                throw new DBException("There aren't any itemcmd for " + dritem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                            }
                            //IF Item isn't a combobox
                            if (Convert.ToInt32(dritem["GUIGroupboxModeItemType"]) != 255)
                            {
                                if ((Convert.ToInt32(dritem["GUIGroupboxModeItemType"]) != 6) && (Convert.ToInt32(dritem["GUIGroupboxModeItemType"]) != 7)) runtimeGUIButton = new Button();
                                else
                                {
                                    Color coloron = Color.Empty;
                                    Color coloroff = Color.Empty;
                                    switch (Convert.ToInt32(dritem.GetChildRows("FK_ToggleDefault_0")[0]["ToggleColorOn"]))
                                    {
                                        case 0:
                                            coloron = System.Drawing.Color.Empty;
                                            break;
                                        case 1:
                                            coloron = System.Drawing.Color.Yellow;
                                            break;
                                        case 2:
                                            coloron = System.Drawing.Color.Red;
                                            break;
                                        case 3:
                                            coloron = System.Drawing.Color.Green;
                                            break;
                                        case 4:
                                            coloron = System.Drawing.Color.Blue;
                                            break;
                                    }
                                    switch (Convert.ToInt32(dritem.GetChildRows("FK_ToggleDefault_0")[0]["ToggleColorOff"]))
                                    {
                                        case 0:
                                            coloroff = System.Drawing.Color.Empty;
                                            break;
                                        case 1:
                                            coloroff = System.Drawing.Color.Yellow;
                                            break;
                                        case 2:
                                            coloroff = System.Drawing.Color.Red;
                                            break;
                                        case 3:
                                            coloroff = System.Drawing.Color.Green;
                                            break;
                                        case 4:
                                            coloroff = System.Drawing.Color.Blue;
                                            break;
                                    }
                                    runtimeGUIButton = new OnOffButton(coloron, coloroff);
                                    ((OnOffButton)runtimeGUIButton).On = Convert.ToBoolean(dritem.GetChildRows("FK_ToggleDefault_0")[0]["ToggleDefault"]);

                                }
                                runtimeGUIButton.Name = dritem["GUIGroupboxModeItemID"].ToString();
                                runtimeGUIButton.Size = new System.Drawing.Size(100, 23);
                                runtimeGUIButton.Text = dritem["GUIGroupboxModeItemName"].ToString();
                                //runtimeGUIButton.UseVisualStyleBackColor = true;
                                runtimeGUIButton.AutoSize = true;
                                runtimeGUIButton.Click += new EventHandler(runtimeGUIButton_Click);
                                runtimeGuiToolTip = new ToolTip();
                                runtimeGuiToolTip.SetToolTip(runtimeGUIButton, dritem["GUIGroupboxModeItemComment"].ToString());
                                cmdFLP.Controls.Add(runtimeGUIButton);
                            }
                            //IF Item is a combobox
                            else if (Convert.ToInt32(dritem["GUIGroupboxModeItemType"]) == 255)
                            {
                                runtimeGUIRCPCombobox = new RCPComboBox();
                                runtimeGUIRCPCombobox.button.Name = dritem["GUIGroupboxModeItemID"].ToString();
                                runtimeGUIRCPCombobox.GUIGroupboxModeItemID = Convert.ToInt32(dritem["GUIGroupboxModeItemID"]);
                                runtimeGUIRCPCombobox.Text = dritem["GUIGroupboxModeItemName"].ToString();
                                runtimeGUIRCPCombobox.button.Click += new EventHandler(runtimeGUIButton_Click);
                                runtimeGuiToolTip = new ToolTip();
                                runtimeGuiToolTip.SetToolTip(runtimeGUIButton, dritem["GUIGroupboxModeItemComment"].ToString());
                                runtimeGUIRCPCombobox.DataSource = strLcombosource;
                                cmdFLP.Controls.Add(runtimeGUIRCPCombobox);
                            }
                        }
                    }

                    else
                    {
                        throw new DBException("There aren't any usable item for " + DSDatabase.GUIGroupboxMode.Select("GUIGroupboxModeID=" + gb.Name)[0]["GUIGroupboxModeName"].ToString() + " mode");
                    }
                }
                else
                {
                    list = gb.Controls.Find("controllerTableLayoutPanel", true);
                    if (list.Length > 0)
                    {
                        List<string> strLparametersid = new List<string>();
                        TableLayoutPanel controllerTLP = list[0] as TableLayoutPanel;
                        FlowLayoutPanel parameterFLP = gb.Controls.Find("parameterFlowLayoutPanel", true)[0] as FlowLayoutPanel;
                        //Iteration through buttons of controllerTLP and identify which cell contains it = which button is which function
                        foreach (Button btn in controllerTLP.Controls)
                        {
                            int type = 0;
                            switch (controllerTLP.GetCellPosition(btn).Column)
                            {
                                //btnLeft
                                case 0:
                                    type = 4;
                                    break;
                                case 1:
                                    switch (controllerTLP.GetCellPosition(btn).Row)
                                    {
                                        //btnUP
                                        case 0:
                                            type = 1;
                                            break;
                                        case 1:
                                            //btnStop
                                            type = 5;
                                            break;
                                        case 2:
                                            //btnDown
                                            type = 3;
                                            break;
                                    }
                                    break;
                                //btnRight
                                case 2:
                                    type = 2;
                                    break;
                            }
                            //One Row in One ControllerTLP, always --- MODEITEM
                            DataRow[] drAitem = DSDatabase.GUIGroupboxModeItem.Select("GUIGroupboxModeID=" + rb.Name + " AND GUIGroupboxModeItemType=" + type.ToString(), "GUIGroupboxModeItemSequence");
                            if (drAitem.Length > 0)
                            {
                                DataRow dritem = drAitem[0];
                                btn.Name = dritem["GUIGroupboxModeItemID"].ToString();
                                btn.Text = dritem["GUIGroupboxModeItemName"].ToString();
                                runtimeGuiToolTip = new ToolTip();
                                runtimeGuiToolTip.SetToolTip(btn, dritem["GUIGroupboxModeItemComment"].ToString());
                                btn.Enabled = true;
                                //One modeitem - One itemcmd, only in controllerTLP --- ITEMCMD
                                DataRow dritemcmd = dritem.GetChildRows("FK_GUIGroupboxModeItemCmd_1", DataRowVersion.Current)[0];
                                if (!bDRIsNull(dritemcmd))
                                {
                                    //One ItemCmd - One Cmd always --- CMD
                                    DataRow drcmd = dritemcmd.GetParentRows("FK_GUIGroupboxModeItemCmd_0", DataRowVersion.Current)[0];
                                    if (!bDRIsNull(drcmd))
                                    {
                                        //Null or many parameters for a cmd ---PARAMETER (CmdParameter Table)
                                        DataRow[] drAparameters = DSDatabase.CmdParameter.Select("CmdID=" + drcmd["CmdID"].ToString(), "ParameterSequence");
                                        //If Cmd hasn't got parameters, next step won't running
                                        if (drAparameters.Length > 0)
                                        {
                                            foreach (DataRow Parameter in drAparameters)
                                            {
                                                //Null or ONE datarow or a parameter and his modeitem --- PARAMETER (GUIGroupboxModeItemParameter Table)
                                                DataRow[] drAmodeitemparameter = DSDatabase.GUIGroupboxModeItemParameter.Select("GUIGroupboxModeItemCmdID=" + dritemcmd["GUIGroupboxModeItemCmdID"].ToString() + " AND ParameterID=" + Parameter["ParameterID"].ToString() + " AND ParameterValue IS NOT NULL");
                                                //If no presetted Parameter it will added to strLparameterid list, which contain what parameter must activating
                                                if (drAmodeitemparameter.Length == 0)
                                                {
                                                    //One parameter - One datarow --- PARAMETER (Parameter Table)
                                                    DataRow drparam = Parameter.GetParentRow("FK_CmdParameter_0", DataRowVersion.Current);
                                                    strLparametersid.Add(drparam["ParameterID"].ToString());
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        throw new DBException("There aren't any cmd for " + dritem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                    }
                                }
                                else
                                {
                                    throw new DBException("There aren't any itemcmd for " + dritem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                }

                            }
                            else
                            {
                                btn.Text = "";
                                btn.Name = "";
                                runtimeGuiToolTip.Dispose();
                                btn.Enabled = false;
                            }
                        }
                        //When was every button, parameters can activate
                        foreach (Control control in parameterFLP.Controls)
                        {
                            if (strLparametersid.Count > 0)
                            {
                                if (strLparametersid.Contains(control.Name)) control.Enabled = true;
                                else control.Enabled = false;
                            }
                            else control.Enabled = false;
                        }
                    }
                }
            }
        }
 private void LoadGUI()
 {
     #region GUI from DB
     //IF we have an opened DB, let's go crazy!
     if (bDataSetFilled)
     {
         try
         {
             maxSize = new Size(10000, 190);
             //controlFlowLayoutPanel
             controlFlowLayoutPanel = new FlowLayoutPanel();
             controlFlowLayoutPanel.AutoScroll = true;
             controlFlowLayoutPanel.Dock = DockStyle.Fill;
             controlFlowLayoutPanel.Name = "controlFlowLayoutPanel";
             controlFlowLayoutPanel.Resize += new EventHandler(controlFlowLayoutPanel_Resize);
             //Always one or more DataRow --- GROUPBOX
             DataRow[] draGroupbox = DSDatabase.GUIGroupbox.Select("RobotID=" + this.iRobotID.ToString(), "GUIGroupboxSequence");
             foreach (DataRow Groupbox in draGroupbox)
             {
                 //------------------------------------------------------------------
                 //Groupbox
                 runtimeGUIGroupBox = new GroupBox();
                 runtimeGUIGroupBox.Name = Groupbox["GUIGroupboxID"].ToString();
                 runtimeGUIGroupBox.Text = Groupbox["GUIGroupboxName"].ToString();
                 runtimeGUIGroupBox.MinimumSize = new Size(100, 190);
                 runtimeGUIGroupBox.AutoSize = true;
                 runtimeGUIGroupBox.AutoSizeMode = AutoSizeMode.GrowOnly;
                 runtimeGUIGroupBox.Dock = DockStyle.Left;
                 runtimeGuiToolTip = new ToolTip();
                 runtimeGuiToolTip.SetToolTip(runtimeGUIGroupBox, Groupbox["GUIGroupboxComment"].ToString());
                 //Create containers to the groupbox
                 //------------------------------------------------------------------
                 //modeFlowLayoutPanel
                 modeFlowLayoutPanel = new FlowLayoutPanel();
                 modeFlowLayoutPanel.Name = "modeFlowLayoutPanel";
                 modeFlowLayoutPanel.AutoSize = true;
                 modeFlowLayoutPanel.AutoSizeMode = AutoSizeMode.GrowOnly;
                 modeFlowLayoutPanel.FlowDirection = FlowDirection.TopDown;
                 modeFlowLayoutPanel.Dock = DockStyle.Left;
                 modeFlowLayoutPanel.MaximumSize = maxSize;
                 //------------------------------------------------------------------
                 //parameterFlowLayoutPanel
                 parameterFlowLayoutPanel = new FlowLayoutPanel();
                 parameterFlowLayoutPanel.Name = "parameterFlowLayoutPanel";
                 parameterFlowLayoutPanel.AutoSize = true;
                 parameterFlowLayoutPanel.AutoSizeMode = AutoSizeMode.GrowOnly;
                 parameterFlowLayoutPanel.MaximumSize = maxSize;
                 parameterFlowLayoutPanel.FlowDirection = FlowDirection.TopDown;
                 parameterFlowLayoutPanel.Dock = DockStyle.Left;
                 parameterFlowLayoutPanel.WrapContents = true;
                 //------------------------------------------------------------------
                 //General Groupbox
                 if (Convert.ToInt32(Groupbox["GUIGroupboxType"]) == 0)
                 {
                     //Always one or more DataRow normally, but it can zero --- MODE
                     DataRow[] drAmodes = DSDatabase.GUIGroupboxMode.Select("GUIGroupboxID=" + Groupbox["GUIGroupboxID"], "GUIGroupboxModeSequence");
                     if (drAmodes.Length > 0)
                     {
                         foreach (DataRow drmode in drAmodes)
                         {
                             runtimeGUIRadioButton = new RadioButton();
                             runtimeGUIRadioButton.Name = drmode["GUIGroupboxModeID"].ToString();
                             runtimeGUIRadioButton.Text = drmode["GUIGroupboxModeName"].ToString();
                             runtimeGuiToolTip = new ToolTip();
                             runtimeGuiToolTip.SetToolTip(runtimeGUIRadioButton, drmode["GUIGroupboxModeComment"].ToString());
                             runtimeGUIRadioButton.CheckedChanged += new EventHandler(runtimeGUIRadioButton_CheckedChanged);
                             modeFlowLayoutPanel.Controls.Add(runtimeGUIRadioButton);
                         }
                         if (modeFlowLayoutPanel.Controls.Count == 1) modeFlowLayoutPanel.Visible = false;
                         //cmdFlowLayoutPanel
                         cmdFlowLayoutPanel = new FlowLayoutPanel();
                         cmdFlowLayoutPanel.Name = "cmdFlowLayoutPanel";
                         cmdFlowLayoutPanel.AutoSize = true;
                         cmdFlowLayoutPanel.AutoSizeMode = AutoSizeMode.GrowOnly;
                         cmdFlowLayoutPanel.FlowDirection = FlowDirection.TopDown;
                         cmdFlowLayoutPanel.Dock = DockStyle.Left;
                         cmdFlowLayoutPanel.WrapContents = true;
                         cmdFlowLayoutPanel.MaximumSize = maxSize;
                     }
                     else
                     {
                         throw new DBException("There aren't any modes for " + Groupbox["GUIGroupboxName"].ToString() + " groupbox");
                     }
                 }
                 //------------------------------------------------------------------
                 //Controller Groupbox
                 else if (Convert.ToInt32(Groupbox["GUIGroupboxType"]) == 1)
                 {
                     //Always one or more DataRow normally, but it can zero --- MODE
                     DataRow[] drAmodes = DSDatabase.GUIGroupboxMode.Select("GUIGroupboxID=" + Groupbox["GUIGroupboxID"], "GUIGroupboxModeSequence");
                     if (drAmodes.Length > 0)
                     {
                         foreach (DataRow drmode in drAmodes)
                         {
                             runtimeGUIRadioButton = new RadioButton();
                             runtimeGUIRadioButton.Name = drmode["GUIGroupboxModeID"].ToString();
                             runtimeGUIRadioButton.Text = drmode["GUIGroupboxModeName"].ToString();
                             runtimeGuiToolTip = new ToolTip();
                             runtimeGuiToolTip.SetToolTip(runtimeGUIRadioButton, drmode["GUIGroupboxModeComment"].ToString());
                             runtimeGUIRadioButton.CheckedChanged += new EventHandler(runtimeGUIRadioButton_CheckedChanged);
                             modeFlowLayoutPanel.Controls.Add(runtimeGUIRadioButton);
                             //Always one or more but it can zero --- MODEITEM
                             DataRow[] drAmodeitems = drmode.GetChildRows("FK_GUIGroupboxModeItem_0");
                             if (drAmodeitems.Length > 0)
                             {
                                 foreach (DataRow drmodeitem in drAmodeitems)
                                 {
                                     //One Item - One ItemCmd ONLY IN CONTROLLERGROUPBOX --- ITEMCMD
                                     DataRow[] drAitemcmds = drmodeitem.GetChildRows("FK_GUIGroupboxModeItemCmd_1");
                                     if (drAitemcmds.Length > 0)
                                     {
                                         DataRow dritemcmd = drAitemcmds[0];
                                         DataRow drcmd;
                                         //One ItemCmd - One Cmd always --- CMD
                                         drcmd = dritemcmd.GetParentRow("FK_GUIGroupboxModeItemCmd_0");
                                         if (!bDRIsNull(drcmd))
                                         {
                                             //Null or many parameters for a cmd ---PARAMETER (CmdParameter Table)
                                             DataRow[] drAparameters = drcmd.GetChildRows("FK_CmdParameter_1");
                                             //IF No parameters, no iteration
                                             if (drAparameters.Length > 0)
                                             {
                                                 foreach (DataRow drparameter in drAparameters)
                                                 {
                                                     //Null or ONE datarow or a parameter and his modeitem --- PARAMETER (GUIGroupboxModeItemParameter Table)
                                                     DataRow[] drAmodeitemparameter = DSDatabase.GUIGroupboxModeItemParameter.Select("GUIGroupboxModeItemCmdID=" + dritemcmd["GUIGroupboxModeItemCmdID"].ToString() + " AND ParameterID=" + drparameter["ParameterID"].ToString() + " AND ParameterValue IS NOT NULL");
                                                     if (drAmodeitemparameter.Length == 0)
                                                     {
                                                         //One parameter - One datarow --- PARAMETER (Parameter Table)
                                                         DataRow drparam = drparameter.GetParentRow("FK_CmdParameter_0", DataRowVersion.Current);
                                                         if (!bDRIsNull(drparam))
                                                         {
                                                             //IF parameterFlowLayoutPanel isn't contains this parameter yet, it will added
                                                             if (!parameterFlowLayoutPanel.Controls.ContainsKey(drparam["ParameterID"].ToString()))
                                                             {
                                                                 //Textbox
                                                                 if (Convert.ToInt32(drparam["ParameterType"]) == 0)
                                                                 {
                                                                     runtimeGUIRCPTextBox = new RCPTextBox();
                                                                     runtimeGUIRCPTextBox.Name = drparam["ParameterID"].ToString();
                                                                     runtimeGUIRCPTextBox.Text = drparam["ParameterName"].ToString();
                                                                     if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTextBox.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                     else runtimeGUIRCPTextBox.Min = int.MinValue;
                                                                     if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTextBox.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                     else runtimeGUIRCPTextBox.Max = int.MaxValue;
                                                                     if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTextBox.Default = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                     else runtimeGUIRCPTextBox.Default = 0;
                                                                     runtimeGUIRCPTextBox.Value = runtimeGUIRCPTextBox.Default;
                                                                     runtimeGUIRCPTextBox.Enabled = false;
                                                                     runtimeGuiToolTip = new ToolTip();
                                                                     runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTextBox, drparam["ParameterComment"].ToString());
                                                                     parameterFlowLayoutPanel.Controls.Add(runtimeGUIRCPTextBox);
                                                                 }
                                                                 //NumericUpDown
                                                                 else if (Convert.ToInt32(drparam["ParameterType"]) == 1)
                                                                 {
                                                                     runtimeGUIRCPNumericUpDown = new RCPNumericUpDown();
                                                                     runtimeGUIRCPNumericUpDown.Name = drparam["ParameterID"].ToString();
                                                                     runtimeGUIRCPNumericUpDown.Text = drparam["ParameterName"].ToString();
                                                                     if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Min = Convert.ToDecimal(drparam["ParameterMin"]);
                                                                     else runtimeGUIRCPNumericUpDown.Min = decimal.MinValue;
                                                                     if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Max = Convert.ToDecimal(drparam["ParameterMax"]);
                                                                     else runtimeGUIRCPNumericUpDown.Max = decimal.MaxValue;
                                                                     if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPNumericUpDown.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                     else runtimeGUIRCPNumericUpDown.Value = 0;
                                                                     runtimeGUIRCPNumericUpDown.Enabled = false;
                                                                     runtimeGuiToolTip = new ToolTip();
                                                                     runtimeGuiToolTip.SetToolTip(runtimeGUIRCPNumericUpDown, drparam["ParameterComment"].ToString());
                                                                     parameterFlowLayoutPanel.Controls.Add(runtimeGUIRCPNumericUpDown);
                                                                 }
                                                                 //Trackbar
                                                                 else if (Convert.ToInt32(drparam["ParameterType"]) == 2)
                                                                 {
                                                                     runtimeGUIRCPTrackbar = new RCPTrackbar();
                                                                     runtimeGUIRCPTrackbar.Name = drparam["ParameterID"].ToString();
                                                                     runtimeGUIRCPTrackbar.Text = drparam["ParameterName"].ToString();
                                                                     if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTrackbar.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                     else runtimeGUIRCPTrackbar.Min = int.MinValue;
                                                                     if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTrackbar.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                     else runtimeGUIRCPTrackbar.Max = int.MaxValue;
                                                                     if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTrackbar.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                     else runtimeGUIRCPTrackbar.Value = 0;
                                                                     runtimeGUIRCPTrackbar.Enabled = false;
                                                                     runtimeGuiToolTip = new ToolTip();
                                                                     runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTrackbar, drparam["ParameterComment"].ToString());
                                                                     parameterFlowLayoutPanel.Controls.Add(runtimeGUIRCPTrackbar);
                                                                 }
                                                                 //Trackbar with NumericUpDown
                                                                 else if (Convert.ToInt32(drparam["ParameterType"]) == 3)
                                                                 {
                                                                     runtimeGUIRCPTrackbarWithNumericUpDown = new RCPTrackbarWithNumericUpDown();
                                                                     runtimeGUIRCPTrackbarWithNumericUpDown.Name = drparam["ParameterID"].ToString();
                                                                     runtimeGUIRCPTrackbarWithNumericUpDown.Text = drparam["ParameterName"].ToString();
                                                                     if (drparam["ParameterMin"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Min = Convert.ToInt32(drparam["ParameterMin"]);
                                                                     else runtimeGUIRCPTrackbarWithNumericUpDown.Min = int.MinValue;
                                                                     if (drparam["ParameterMax"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Max = Convert.ToInt32(drparam["ParameterMax"]);
                                                                     else runtimeGUIRCPTrackbarWithNumericUpDown.Max = int.MaxValue;
                                                                     if (drparam["ParameterDefault"] != DBNull.Value) runtimeGUIRCPTrackbarWithNumericUpDown.Value = Convert.ToInt32(drparam["ParameterDefault"]);
                                                                     else runtimeGUIRCPTrackbarWithNumericUpDown.Value = 0;
                                                                     runtimeGUIRCPTrackbarWithNumericUpDown.Enabled = false;
                                                                     runtimeGuiToolTip = new ToolTip();
                                                                     runtimeGuiToolTip.SetToolTip(runtimeGUIRCPTrackbarWithNumericUpDown, drparam["ParameterComment"].ToString());
                                                                     parameterFlowLayoutPanel.Controls.Add(runtimeGUIRCPTrackbarWithNumericUpDown);
                                                                 }
                                                             }
                                                         }
                                                         else
                                                         {
                                                             throw new DBException("There aren't usable parameter for " + drcmd["CmdName"].ToString() + " in " + drmodeitem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         else
                                         {
                                             throw new DBException("There aren't any cmd for " + drmodeitem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                         }
                                     }
                                     else
                                     {
                                         throw new DBException("There aren't any itemcmd for " + drmodeitem["GUIGroupboxModeItemName"].ToString() + " modeitem");
                                     }
                                 }
                             }
                             else
                             {
                                 throw new DBException("There aren't any usable item for " + drmode["GUIGroupboxModeName"].ToString() + " mode");
                             }
                         }
                     }
                     else
                     {
                         throw new DBException("There aren't any modes for " + Groupbox["GUIGroupboxName"].ToString() + " groupbox");
                     }
                     if (modeFlowLayoutPanel.Controls.Count == 1) modeFlowLayoutPanel.Visible = false;
                     //controllerTableLayoutPanel
                     controllerTableLayoutPanel = new TableLayoutPanel();
                     controllerTableLayoutPanel.ColumnCount = 3;
                     controllerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.RowCount = 3;
                     controllerTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
                     controllerTableLayoutPanel.Name = "controllerTableLayoutPanel";
                     controllerTableLayoutPanel.Size = new Size(160, 160);
                     controllerTableLayoutPanel.Dock = DockStyle.Left;
                     controllerTableLayoutPanel.MaximumSize = maxSize;
                     //controllerButtons
                     runtimeGUIButtonUp = new Button();
                     runtimeGUIButtonUp.Font = new Font("Arial", 24F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(238)));
                     runtimeGUIButtonUp.TextAlign = ContentAlignment.MiddleCenter;
                     runtimeGUIButtonUp.UseVisualStyleBackColor = true;
                     runtimeGUIButtonUp.Click += new EventHandler(runtimeGUIButton_Click);
                     runtimeGUIButtonUp.Enabled = false;
                     runtimeGUIButtonUp.Dock = DockStyle.Fill;
                     runtimeGUIButtonRight = new Button();
                     runtimeGUIButtonRight.Font = new Font("Arial", 24F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(238)));
                     runtimeGUIButtonRight.TextAlign = ContentAlignment.MiddleCenter;
                     runtimeGUIButtonRight.UseVisualStyleBackColor = true;
                     runtimeGUIButtonRight.Click += new EventHandler(runtimeGUIButton_Click);
                     runtimeGUIButtonRight.Enabled = false;
                     runtimeGUIButtonRight.Dock = DockStyle.Fill;
                     runtimeGUIButtonDown = new Button();
                     runtimeGUIButtonDown.Font = new Font("Arial", 24F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(238)));
                     runtimeGUIButtonDown.TextAlign = ContentAlignment.MiddleCenter;
                     runtimeGUIButtonDown.UseVisualStyleBackColor = true;
                     runtimeGUIButtonDown.Click += new EventHandler(runtimeGUIButton_Click);
                     runtimeGUIButtonDown.Enabled = false;
                     runtimeGUIButtonDown.Dock = DockStyle.Fill;
                     runtimeGUIButtonLeft = new Button();
                     runtimeGUIButtonLeft.Font = new Font("Arial", 24F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(238)));
                     runtimeGUIButtonLeft.TextAlign = ContentAlignment.MiddleCenter;
                     runtimeGUIButtonLeft.UseVisualStyleBackColor = true;
                     runtimeGUIButtonLeft.Click += new EventHandler(runtimeGUIButton_Click);
                     runtimeGUIButtonLeft.Enabled = false;
                     runtimeGUIButtonLeft.Dock = DockStyle.Fill;
                     runtimeGUIButtonStop = new Button();
                     runtimeGUIButtonStop.Font = new Font("Arial", 24F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(238)));
                     runtimeGUIButtonStop.TextAlign = ContentAlignment.TopCenter;
                     runtimeGUIButtonStop.UseVisualStyleBackColor = true;
                     runtimeGUIButtonStop.Click += new EventHandler(runtimeGUIButton_Click);
                     runtimeGUIButtonStop.Enabled = false;
                     runtimeGUIButtonStop.Dock = DockStyle.Fill;
                     //Add controllerButtons to controllerTableLayoutPanel
                     controllerTableLayoutPanel.Controls.Add(runtimeGUIButtonUp, 1, 0);
                     controllerTableLayoutPanel.Controls.Add(runtimeGUIButtonRight, 2, 1);
                     controllerTableLayoutPanel.Controls.Add(runtimeGUIButtonDown, 1, 2);
                     controllerTableLayoutPanel.Controls.Add(runtimeGUIButtonLeft, 0, 1);
                     controllerTableLayoutPanel.Controls.Add(runtimeGUIButtonStop, 1, 1);
                 }
                 //------------------------------------------------------------------
                 //Add Panels to GroupBox - Order is important cause of dockstyles
                 runtimeGUIGroupBox.Controls.Add(parameterFlowLayoutPanel);
                 if (Convert.ToInt32(Groupbox["GUIGroupboxType"]) == 0) runtimeGUIGroupBox.Controls.Add(cmdFlowLayoutPanel);
                 else if (Convert.ToInt32(Groupbox["GUIGroupboxType"]) == 1) runtimeGUIGroupBox.Controls.Add(controllerTableLayoutPanel);
                 runtimeGUIGroupBox.Controls.Add(modeFlowLayoutPanel);
                 controlFlowLayoutPanel.Controls.Add(runtimeGUIGroupBox);
                 ((RadioButton)modeFlowLayoutPanel.Controls[0]).Checked = true;
             }
             controlTabPage.Controls.Clear();
             controlTabPage.Controls.Add(controlFlowLayoutPanel);
         }
         catch (DBException ex)
         {
             MessageBox.Show(ex.Message + "\r\nDatabase now closing!", "Unexpected error while process database", MessageBoxButtons.OK, MessageBoxIcon.Error);
             CloseDB();
         }
     }
     #endregion
     #region No DB GUI
     //IF no opened DB, we won't sad
     else
     {
         //Hide the showed function tabpages
         CloseFunctions();
         //noDB GUI loading
         noDBTableLayoutPanel = new TableLayoutPanel();
         noDBSendButton = new Button();
         noDBTextBox = new TextBox();
         noDBLabel = new Label();
         noDBTextBoxToolTip = new ToolTip();
         //NoDbSendButton
         noDBSendButton.Anchor = AnchorStyles.Top;
         noDBSendButton.Location = new Point(253, 109);
         noDBSendButton.Name = "noDBSendButton";
         noDBSendButton.Size = new Size(75, 23);
         noDBSendButton.TabIndex = 0;
         noDBSendButton.Text = "Send";
         noDBSendButton.UseVisualStyleBackColor = true;
         noDBSendButton.Click += new EventHandler(noDBSendButton_Click);
         //NoDbTextBox
         noDBTextBox.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
         noDBTextBox.Location = new Point(144, 69);
         noDBTextBox.Name = "noDBTextbox";
         noDBTextBox.Size = new Size(294, 20);
         noDBTextBox.TabIndex = 1;
         noDBTextBox.KeyDown += new KeyEventHandler(noDBTextBox_KeyDown);
         noDBTextBoxToolTip.SetToolTip(noDBTextBox, "Here you can write your own numeric-commands separated by spaces, commas or semicolons");
         //noDBLabel
         noDBLabel.AutoSize = true;
         noDBLabel.Dock = DockStyle.Fill;
         noDBLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
         noDBLabel.Location = new Point(144, 0);
         noDBLabel.Name = "noDBLabel";
         noDBLabel.Size = new Size(294, 53);
         noDBLabel.TabIndex = 2;
         noDBLabel.Text = "No opened database!!!\nYou can choose a database, or communicate your device with following textbox:";
         noDBLabel.TextAlign = ContentAlignment.MiddleCenter;
         //noDBTableLayoutPanel
         noDBTableLayoutPanel.ColumnCount = 3;
         noDBTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
         noDBTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 300F));
         noDBTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
         noDBTableLayoutPanel.Controls.Add(noDBSendButton, 1, 2);
         noDBTableLayoutPanel.Controls.Add(noDBTextBox, 1, 1);
         noDBTableLayoutPanel.Controls.Add(noDBLabel, 1, 0);
         noDBTableLayoutPanel.Dock = DockStyle.Fill;
         noDBTableLayoutPanel.Location = new Point(3, 3);
         noDBTableLayoutPanel.Name = "noDBTableLayoutPanel";
         noDBTableLayoutPanel.RowCount = 3;
         noDBTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
         noDBTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
         noDBTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
         noDBTableLayoutPanel.Size = new Size(582, 162);
         noDBTableLayoutPanel.TabIndex = 6;
         //Add controls to controlTabPage
         controlTabPage.Controls.Clear();
         controlTabPage.Controls.Add(noDBTableLayoutPanel);
         SetStatusText("No DB found!");
     }
     #endregion
 }