WinForm Multi-form label--hyappframe

Source: Internet
Author: User

This section focuses on how to implement multiple tabs in WinForm, which can be opened or activated by a menu, and the tab effect is displayed, click the tab to implement a form switch, and close the subform by closing the button. As follows:

Hyappframe Project Address: https://sourceforge.net/projects/hyappframe/

1 Implementation principle
    • Use the TabControl control to act as a tab bar
    • Using the TreeView control as a menu navigation
    • Form Form1 set up as a form container
    • Create a global array to store the list of open forms, and then create a global array to store the Open tab form name is consistent with the tab name for the corresponding
    • Draws a close icon in the upper-right corner of each label TabPage, closes the form and moves out the label when the mouse clicks the area of the icon
2 Implementation Method 2.1 defines two global arrays
The form collection has been opened private static ArrayList frmlist;public static ArrayList frmlsit{    get{return frmlist;}    set{frmlist = value;}} Tab tab is open private static ArrayList tabList; public static ArrayList tablist{    get{return TabList;}    set{tabList = value;}}
2.2 Open or activate a form

Opens the given form and joins the form to the global array frmlist

private void ShowForm (Form frm) {   frm. FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;   frm. MdiParent = this;   frm. Dock = DockStyle.Fill;   frm. Show ();   MYFUNCLIB.FRMLSIT.ADD (frm);   myfunclib.activedfrm = frm;}

Activates the given form and places the form first.

private void ActiveForm (Form frm) {   frm. Activate ();   frm. Focus ();   frm. BringToFront ();   myfunclib.activedfrm = frm;}

Open or activate the target form, and when given a form, first check that the form is open, activate it if it is on, and set the corresponding Label tab selected, and if it is not, open the form and include the form label in the tab bar. The form and Label tab names must be exactly the same, otherwise they will not correspond.

Private Voidopenoractiveform (string name, string text) {//Determine if the incoming is a form if (!isform) return;    Boolean IsOpen = false; The #region determines whether the target form is open and activates foreach (Form frm in myfunclib.frmlsit) {if (frm) if it is already open.            Name.equals (name)) {IsOpen = true;                    foreach (TabPage tabItem inmyfunclib.tablist) {if (TabItem.Name.Equals (Name))            This.tabForms.SelectedTab =tabitem;            }//Activate form activeForm (frm);        Break        }} #endregion #region open open form if (!isopen) {form frm = NULL if it has not been opened;        Assembly Assembly =assembly.getexecutingassembly (); frm =assembly.        CreateInstance (namespacestring + "." + name) as Form;            if (frm = = null) {myfunclib.msg ("The specified program form not found", "w");        Return } frm.        name = name;        ShowForm (frm);//Display form TabPage TabPage = new TabPage ();        Tabpage.name = Name; tabPage.text = Text;        THIS.TABFORMS.TABPAGES.ADD (TabPage);        This.tabForms.SelectedTab = TabPage;    MYFUNCLIB.TABLIST.ADD (TabPage); } #endregion}
2.3 Tab Click Effects

When you click the Tabs tab, the corresponding form is found and activated.

Private Voidtabforms_selectedindexchanged (object sender, EventArgs e) {    TabPage tab = ((TabControl) sender). Selectedtab;    if (tab = = null)        return;    foreach (Form frm in myfunclib.frmlsit)    {        if (frm. Name.equals (tab. Name)        {            activeForm (frm);            Break;}}}    
2.4 Draw Close Icon

To add the Draw tab close icon in the Tabforms control DrawItem event

private void Tabforms_drawitem (Object Sender,drawitemeventargs e) {drawcloseimage (Close16grey, e);} private void DRAWCL Oseimage (Bitmap Image,drawitemeventargs e) {try {//Get TabPage tab Rectangle area Rectangle rect = This.tabForms.Get       Tabrect (E.index); #region set the background color when TabPage is selected color Reccolor = E.state = = drawitemstate.selected?       Color.FromArgb (214,231,247): Color.White;       using (Brush B = new SolidBrush (Reccolor)) {E.graphics.fillrectangle (b, rect); } #endregion//Draw TabPage e.graphics.drawstring (This.tabforms.tabpages[e.index]. Text, this. Font,systembrushes.controltext, Rect. X + 2, rect.        Y + 2); #region draw a rectangular region with the close icon size using (Pen p = new Pen (color.white)) {rect. Offset (rect.           Width-(closeimagesize + 3), 2); Rect.           Width = closeimagesize; Rect.           Height = closeimagesize;       E.graphics.drawrectangle (P, rect); #endregion #region Use the icon to populate this area using (PEn objpen = new Pen (Color.Black)) {Bitmap BT = new Bitmap (image); Point P5 = new Point (rect.           X, 4);       E.graphics.drawimage (BT, p5);    } #endregion//Release resource e.graphics.dispose (); } catch (Exception) {}}
2.5 Close button function

In the Tabforms control MouseDown event, adding code to close the form, move out of the tab, the difficulty is getting the mouse click coordinates, whether the cursor is within the "Close icon" range, and then continue executing the code. It is worth noting that it is not possible to achieve this through the Tabpage_click event.

private void Tabforms_mousedown (Object Sender,mouseeventargs e) {if (E.button = = mousebuttons.left) {int x = e .       X, y = e.y;        Calculates the closed area Rectangle rect = This.tabForms.GetTabRect (This.tabForms.SelectedIndex); Rect. Offset (rect.       Width-(closeimagesize + 3), 2); Rect.       Width = closeimagesize; Rect.        Height = closeimagesize; If the mouse is within the area, close the tab BOOL Isclose = x > Rect. X && x < rect. Right && y> rect. Y && y < rect.       Bottom;           if (Isclose = = true) {#region Remove Form and tab tab TabPage tab = This.tabForms.SelectedTab; Select the Left Tab tab if (This.tabForms.SelectedIndex > 1) this.tabForms.SelectedIndex = this.tabforms .           SelectedIndex-1;           This.tabForms.TabPages.Remove (tab); foreach (Form frm in myfunclib.frmlsit) {if (frm. Name.equals (tab.            Name) {MyFuncLib.FrmLsit.Remove (frm);        frm.                    Close ();               Break           }} #endregion #region display the current form foreach (Form frm in myfunclib.frmlsit) {if (frm).                    Name.equals (This.tabForms.SelectedTab.Name)) {activeForm (frm);               Break }} #endregion}}}

3 Study Questions

How does the form name call form entity classes in this section?

How do I perform a method in a subform on a button in a parent form?

How do I open another subform in a subform?

WinForm Multi-form label--hyappframe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.