private void ResetToDefaults() { //Reset main form to defaults activeChildForm = null; //Set current child form field to null lblCaption.Text = "Home"; biFormIcon.IconChar = IconChar.Home; pnlMarker.Visible = false; //Hide child form marker pnlDesktopHeader.BackColor = UIAppearance.BackgroundColor; btnPrint.Visible = false; //Hide option buttons of child form btnScreenshot.Visible = false; btnMoveNewWindow.Visible = false; btnHelp.Visible = false; }
private void UpdateFormLayout() {//This method is responsible for updating the parent form with the proper parameters, //either resetting the default values or displaying an older child form if that's the case. var childForm = listChildForms.LastOrDefault(); //Check and get the last (previous) child form in the form list if (childForm == null) //If there is no result, there is no instance in the list of child forms { ResetToDefaults(); //Reset main form to defaults } else//if there is an instantiated child form in the list, add it back to the desktop panel, set it as an active child form and display it. { activeChildForm = childForm; //Set form as current child form (previous form) pnlDesktop.Controls.Add(childForm); //Add to desktop panel pnlDesktop.Tag = childForm; //Store the form childForm.Show(); //Show child form childForm.BringToFront(); SetChildFormItems();//Set the Caption, Icon and Options of the child form in the layout of the main form } }
protected void OpenChildForm <childForm>(Func <childForm> _delegate, object senderMenuItem, CTMenuButton ownerMenuButton) where childForm : ChildForm { ///Generic method with a Generic Delegate (Func <TResult>), Menu Button and Menu Item Parameters where the data type is RJChildForm. if (ownerMenuButton == null || senderMenuItem == null || senderMenuItem.GetType() != typeof(ToolStripMenuItem)) //validate parameters { RJMessageBox.Show("Please send valid object, null values are not allowed"); return; } ChildForm form = Application.OpenForms.OfType <childForm>().FirstOrDefault();//Checks if the child form is already open #region - Show Child Form as a New Instance if (form == null) //If there is no result, the form is not open, create the instance and display it on the desktop panel { deactivateFormEvent = false; //Don't deactivate form when child form opens form = _delegate(); // Execute the delegate form.IsChildForm = true; form.TopLevel = false; //Indicates that the form is not TopLevel form.Dock = DockStyle.Fill; form.MarkerPosition = ownerMenuButton.Location.Y; //Set marker position CleanDesktop(); //Remove current child form from desktop panel if (UIAppearance.MultiChildForms == false) { CloseActiveChildForm(); // Close current child form if multiple child forms option is disabled- This does not affect if the secondary form is open in a new window. } listChildForms.Add(form); //add FORM to form list pnlDesktop.Controls.Add(form); //add FORM to desktop panel pnlDesktop.Tag = form; form.Show(); //show on desktop panel form.BringToFront(); form.Focus(); activeChildForm = form; //set FORM as active child form SetChildFormItems(); //-> Set the Caption, Icon and Options of the child form in the layout of the main form ownerMenuButton.Activate(form, (ToolStripMenuItem)senderMenuItem); //-> Activate the menu button and associate // a form for the button to remain highlighted until all forms are closed. // You can have many associated forms since the menu button shows a drop-down menu. // Also activate the menu item and associate it to the form so that the menu item remains // highlighted when the form is instantiated. deactivateFormEvent = true;//Reset value } #endregion #region - Redisplay Existing Form or Child Form else //If the form is already open, just show the form again { if (form.IsChildForm && form != activeChildForm) { //if the form is a child form and the form is different from the active child form add back to the panel and set as active child form CleanDesktop(); //Remove current child form from desktop panel pnlDesktop.Controls.Add(form); //add FORM to desktop panel pnlDesktop.Tag = form; activeChildForm = form; //set FORM as active child form SetChildFormItems(); //-> Set the Caption, Icon and Options of the child form in the layout of the main form } if (form.WindowState == FormWindowState.Minimized) { form.WindowState = FormWindowState.Normal; } form.Show();//show the form on the desktop panel or outside in a new window if the form IS NOT A CHILD FORM form.BringToFront(); form.Focus(); } #endregion }
///Open Child Form protected void OpenChildForm <childForm>(Func <childForm> _delegate) where childForm : ChildForm { ///Generic method with a generic delegate parameter (Func <TResult>) where the data type is RJChildForm. ///This method ALLOWS to open forms WITH or WITHOUT parameters within the desktop panel. On many occasions, ///the youtube tutorials used methods similar to this. However, it simply allowed forms to be opened ///WITHOUT parameters ( e.g <see cref="new MyForm ()"/> ) ///and it was impossible to open a form WITH parameters( e.g. <see cref="new MyForm (user:"******", mail:"*****@*****.**"/> ) ///so this method solves this defect thanks to the generic delegate (Func <TResult>) ChildForm form = Application.OpenForms.OfType <childForm>().FirstOrDefault();//Checks if the child form is already open #region - Show Child Form as a New Instance if (form == null) //If there is no result, the form is not open, create the instance and display it on the desktop panel { deactivateFormEvent = false; //Don't deactivate form when child form opens form = _delegate(); // Execute the delegate form.IsChildForm = true; //Set form as child form form.TopLevel = false; //Indicates that the form is not TopLevel form.Dock = DockStyle.Fill; //Sets fill dock style (Fill desktop panel) form.MarkerPosition = 0; //Set the Marker Position to zero, because the form is not opening from a menu button. CleanDesktop(); //Remove current child form from desktop panel // If the multiple child forms option is disabled, only allow to open a single form within the desktop panel, then definitively close the previous one. if (UIAppearance.MultiChildForms == false) { CloseActiveChildForm(); //Close current child form - This does not affect if the secondary form is open in a new window. } listChildForms.Add(form); //Add child form to form list pnlDesktop.Controls.Add(form); //Add child form to desktop panel pnlDesktop.Tag = form; //Store the form form.Show(); //Show form form.BringToFront(); form.Focus(); //Focus form activeChildForm = form; //set FORM as active child form SetChildFormItems(); //-> Set the Caption, Icon and Options of the child form in the layout of the main form deactivateFormEvent = true; //Reset value } #endregion #region - Redisplay Existing Form or Child Form else //If the form is already open, just show the form again { if (form.IsChildForm && form != activeChildForm) { //if the form is a child form and the form is different from the active child form add back to the panel and set as active child form CleanDesktop(); //Remove current child form from desktop panel pnlDesktop.Controls.Add(form); //add child form to desktop panel pnlDesktop.Tag = form; //Store the form activeChildForm = form; //set FORM as active child form SetChildFormItems(); //-> Set the Caption, Icon and Options of the child form in the layout of the main form } if (form.WindowState == FormWindowState.Minimized) { form.WindowState = FormWindowState.Normal; } form.Show(); //show the form on the desktop panel or outside in a new window if the form IS NOT A CHILD FORM form.BringToFront(); //Bring the form to the front form.Focus(); //Focus form } #endregion ///<Note> /// You can use the Func<TResult> delegate with anonymous methods or lambda expression, /// for example, we can call this method as follows: Suppose we are in the button click event method. /// With anonymous method: /// <see cref="OpenChildForm( delegate () { return new MyForm('MyParameter'); });"/> /// With lambda expression /// <see cref="OpenChildForm( () => new MyForm('id', 'username'));"/> /// </Note> }//Here main comments