[C#]MagicLibrary.dll控件的使用(二)(TabControl控件)

MagicLibrary.dll控件功能中昨天写了个Menu的实现,全部以代码方式实现的一个例子。
在MagicLibrary.dll中,还一TabControl控件。
今天以拖拉方式(工具方法拖出)实现TabControl,其中加有Menu例子。

例子源码下载:/Files/HD/MagicLibrary_Demo/TabControlDemo.rar

截图如下:
例图一:


例图二:加入TabPage


例图三:关才已选择的TabPage


例图四:在已选择的TabControl中的RichTextBox控件右键菜单


例图五:TabControl控件的PositionTop属性设为flase



操作示例:
(界面设计)
项目中加入MagicLibrary.dll(包括解决方案资源管理器、工具箱中)。
项目中包括两个Form,分别为:Form1、Form2,Form1为起始Form。
打开Form1,在窗口设计器中从工具箱中拖入刚加入的控件:MenuControl,把MenuControl的Dock属性设为:Top,Name属性为:_topMenu,PositionTop属性设为:true,ShowArrows属性设为:true,ShowClose属性设为:true,ShrinkPagesToFit属性设为:false。接着再从工具箱中拖入:TabControl,把TabControl控件的Dock属性设为:Fill,Name属性为:_tabControl。
打开Form2,在窗口设计器中从工具箱中拖入控件:RichTextBox,把RichTextBox的Dock属性设为:Fill,Name属性设为:rtxContent

一:Form1
(代码实现)
打开Form1的代码设计器,在Form1的构造函数中加入:this.SetupMenu();
见源码:

        public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            
            
//加入菜单
            this.SetupMenu();
        }

接着复制这段代码到设计器中:

        
/// <summary>
        
/// 加入下拉菜单
        
/// </summary>

        protected void SetupMenu()
        
{
            Crownwood.Magic.Menus.MenuCommand top1 
= new Crownwood.Magic.Menus.MenuCommand( "&PageControls" );
            Crownwood.Magic.Menus.MenuCommand top1_1 
= new Crownwood.Magic.Menus.MenuCommand( "&AddPage",new EventHandler( OnAddPageSelected ) );
            Crownwood.Magic.Menus.MenuCommand top1_2 
= new Crownwood.Magic.Menus.MenuCommand( "&RemovePage",new EventHandler( OnColseSelected ) );
            Crownwood.Magic.Menus.MenuCommand top1_3 
= new Crownwood.Magic.Menus.MenuCommand( "&Exit",new EventHandler( OnExitSelected ) );
            top1.MenuCommands.AddRange( 
new Crownwood.Magic.Menus.MenuCommand[]{ top1_1,top1_2,top1_3 } );

            Crownwood.Magic.Menus.MenuCommand top2 
= new Crownwood.Magic.Menus.MenuCommand( "&TabControl" );
            Crownwood.Magic.Menus.MenuCommand top2_1 
= new Crownwood.Magic.Menus.MenuCommand( "&Top",new EventHandler( OnTopSelected ) );
            Crownwood.Magic.Menus.MenuCommand top2_2 
= new Crownwood.Magic.Menus.MenuCommand( "&Buttom",new EventHandler( OnButtomSelected ) );
            top2.MenuCommands.AddRange( 
new Crownwood.Magic.Menus.MenuCommand[]{ top2_1,top2_2 } );

            
this._topMenu.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1,top2 } );

            
this._tabControl.MouseUp += new MouseEventHandler( OnMouseUpTabPage );
        }


        
/// <summary>
        
/// 加入页面
        
/// </summary>

        protected void AddPage()
        
{
            Form2 f2 
= new Form2();
            System.Windows.Forms.Control ctlForm 
= f2;
            Crownwood.Magic.Controls.TabPage tp 
= new Crownwood.Magic.Controls.TabPage( "Page"+this._tabControl.TabPages.Count.ToString(),ctlForm );
            
this._tabControl.TabPages.Add( tp ).Selected = true;
        }




        
protected void OnExitSelected( object sender , EventArgs e )
        
{
            DialogResult dr 
= MessageBox.Show( "Are you sure exit application?","Warning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning );
            
if ( dr == DialogResult.Yes )
            
{
                Application.Exit();
            }

        }


        
protected void OnTopSelected( object sender,EventArgs e )
        
{
            
this._tabControl.PositionTop = true;
        }


        
protected void OnButtomSelected( object sender , EventArgs e )
        
{
            
this._tabControl.PositionTop = false;
        }


        
protected void OnAddPageSelected( object sender , EventArgs e )
        
{
            
this.AddPage();
        }


        
protected void OnMouseUpTabPage( object sender , System.Windows.Forms.MouseEventArgs e )
        
{
            
if ( this._tabControl.TabPages.Count>0 && e.Button == MouseButtons.Right && this._tabControl.SelectedTab.Selected )
            
{
                Crownwood.Magic.Menus.MenuControl muMenu 
= new Crownwood.Magic.Menus.MenuControl();
                Crownwood.Magic.Menus.MenuCommand menu1 
= new Crownwood.Magic.Menus.MenuCommand( "&SavePage" );
                Crownwood.Magic.Menus.MenuCommand menu2 
= new Crownwood.Magic.Menus.MenuCommand( "&Close",new EventHandler( OnColseSelected )  );

                Crownwood.Magic.Menus.PopupMenu pm 
= new Crownwood.Magic.Menus.PopupMenu();
                pm.MenuCommands.AddRange( 
new Crownwood.Magic.Menus.MenuCommand[]{ menu1,menu2 } );
                pm.TrackPopup( 
this._tabControl.PointToScreen( new Point( e.X,e.Y ) ) );
            }

        }


        
protected void OnColseSelected( object sender , EventArgs e )
        
{
            
if ( this._tabControl.TabPages.Count > 0 )
            
{
                
this._tabControl.TabPages.Remove( this._tabControl.SelectedTab );
            }

        }

(添加事件)
打开Form1窗口设计器,选中TabControl控件,向控件加入MouseUp事件:
        /// <summary>
        
/// 单击TabControl关闭按钮时关闭已选择的TabPage
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void _tabControl_ClosePressed(object sender, System.EventArgs e)
        
{
            
if ( this._tabControl.TabPages.Count > 0 )
            
{
                
this._tabControl.TabPages.Remove( this._tabControl.SelectedTab );
            }

        }


二:Form2
打开Form2窗口设计器,选中RichTextBox,为RichTextBox控件添加MouseUp事件:

        
private void rtxContent_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        
{
            
if ( e.Button == MouseButtons.Right )
            
{
                System.Windows.Forms.RichTextBox rt 
= ( RichTextBox )sender;
                Crownwood.Magic.Menus.MenuControl muMenu 
= new Crownwood.Magic.Menus.MenuControl();
                Crownwood.Magic.Menus.MenuCommand menu1 
= new Crownwood.Magic.Menus.MenuCommand( "&Copy" );
                Crownwood.Magic.Menus.MenuCommand menu2 
= new Crownwood.Magic.Menus.MenuCommand( "&Cut" );

                Crownwood.Magic.Menus.PopupMenu pm 
= new Crownwood.Magic.Menus.PopupMenu();
                pm.MenuCommands.AddRange( 
new Crownwood.Magic.Menus.MenuCommand[]{ menu1,menu2 } );
                pm.TrackPopup( rt.PointToScreen( 
new Point( e.X,e.Y ) ) );
            }

        }
posted @ 2005-10-28 13:56  横渡  阅读(7063)  评论(2编辑  收藏  举报