Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

.NET Regular Expressions Find and Replace Add-In for Visual Studio 2008

, 12 Oct 2009
A .NET Regular Expressions Find and Replace add-in for Visual Studio 2008
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.IO;

namespace RegexFindAndReplace
{
    public class CollapsibleGroupBox : GroupBox
    {
        #region Fields

        private Rectangle m_toggleRect = new Rectangle( 8, 2, 11, 11 );
        private Boolean m_collapsed = false;
        private Boolean m_bResizingFromCollapse = false;

        private const int m_collapsedHeight = 20;
        private Size m_FullSize = Size.Empty;

        private System.ComponentModel.IContainer components = null;
        private Bitmap plusBitmap;
        private Bitmap minusBitmap;

        #endregion

        #region Events & Delegates

        /// <summary>Fired when the Collapse Toggle button is pressed</summary>
        public delegate void CollapseBoxClickedEventHandler( object sender );
        public event CollapseBoxClickedEventHandler CollapseBoxClickedEvent;

        #endregion

        #region Constructor

        public CollapsibleGroupBox()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Properties

        [Browsable( false ), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
        public int FullHeight
        {
            get
            {
                return m_FullSize.Height;
            }
        }

        [DefaultValue( false ), Browsable( false ), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
        public bool IsCollapsed
        {
            get
            {
                return m_collapsed;
            }
            set
            {
                if ( value != m_collapsed )
                {
                    m_collapsed = value;

                    if ( !value )
                    {
                        // Expand
                        this.Size = m_FullSize;
                    }
                    else
                    {
                        // Collapse
                        m_bResizingFromCollapse = true;
                        this.Height = m_collapsedHeight;
                        m_bResizingFromCollapse = false;
                    }

                    foreach ( Control c in Controls )
                    {
                        c.Visible = !value;
                    }

                    Invalidate();
                }
            }
        }

        [Browsable( false ), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
        public int CollapsedHeight
        {
            get
            {
                return m_collapsedHeight;
            }
        }

        #endregion

        #region Overrides

        protected override void OnMouseUp( MouseEventArgs e )
        {
            if ( m_toggleRect.Contains( e.Location ) )
            {
                ToggleCollapsed();
            }
            else
            {
                base.OnMouseUp( e );
            }
        }

        protected override void OnPaint( PaintEventArgs e )
        {
            HandleResize();
            DrawGroupBox( e.Graphics );
            DrawToggleButton( e.Graphics );
        }

        #endregion

        #region Implimentation

        void DrawGroupBox( Graphics g )
        {
            // Get windows to draw the GroupBox
            Rectangle bounds = new Rectangle( ClientRectangle.X, ClientRectangle.Y + 6, ClientRectangle.Width, ClientRectangle.Height - 6 );
            GroupBoxRenderer.DrawGroupBox( g, bounds, Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled );

            // Text Formating positioning & Size
            StringFormat sf = new StringFormat();
            int i_textPos = ( bounds.X + 8 ) + m_toggleRect.Width + 2;
            int i_textSize = (int)g.MeasureString( Text, this.Font ).Width;
            i_textSize = i_textSize < 1 ? 1 : i_textSize;
            int i_endPos = i_textPos + i_textSize + 1;

            // Draw a line to cover the GroupBox border where the text will sit
            g.DrawLine( SystemPens.Control, i_textPos, bounds.Y, i_endPos, bounds.Y );

            // Draw the GroupBox text
            using ( SolidBrush drawBrush = new SolidBrush( SystemColors.ActiveCaption ) )
            {
                g.DrawString( Text, this.Font, drawBrush, i_textPos, 0 );
            }
        }

        void DrawToggleButton( Graphics g )
        {
            if ( IsCollapsed )
                g.DrawImage( plusBitmap, m_toggleRect );
            else
                g.DrawImage( minusBitmap, m_toggleRect );
        }

        void ToggleCollapsed()
        {
            IsCollapsed = !IsCollapsed;

            if ( CollapseBoxClickedEvent != null )
                CollapseBoxClickedEvent( this );
        }

        void HandleResize()
        {
            if ( !m_bResizingFromCollapse && !m_collapsed )
                m_FullSize = this.Size;
        }

        #endregion

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager( typeof( CollapsibleGroupBox ) );
            this.SuspendLayout();
            // 
            // CollapsibleGroupBox
            // 
            this.Name = "CollapsibleGroupBox";
            this.ResumeLayout( false );

            Stream plusBitmapStream = null;
            Stream minusBitmapStream = null;

            plusBitmapStream = this.GetType().Assembly.GetManifestResourceStream( "RegexFindAndReplace.plus.bmp" );
            minusBitmapStream = this.GetType().Assembly.GetManifestResourceStream( "RegexFindAndReplace.minus.bmp" );

            plusBitmap = new Bitmap( plusBitmapStream );
            plusBitmap.MakeTransparent( Color.White );

            minusBitmap = new Bitmap( minusBitmapStream );
            minusBitmap.MakeTransparent( Color.White );
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jhillman
Software Developer
United States United States
I am a software developer currently working in Salt Lake City, Utah. I work primarily with C# for my job, but I mess around with C, Perl, and Windows PowerShell for fun.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 12 Oct 2009
Article Copyright 2008 by jhillman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid