Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C#

Single Instance String Store for .NET

Rate me:
Please Sign up or sign in to vote.
4.89/5 (48 votes)
16 Jul 2009CPOL8 min read 64.9K   994   114  
By implementing a single instance string store, you can significantly reduce the memory footprint of your application.
#region File Header

using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace Gibraltar.TraceMonitorSamples
{
    public partial class VerticalProgressBar : Control
    {
        private Color m_BorderColor = Color.Black;
        private int m_BorderWidth = 50;
        private Color m_FillColor = Color.Green;
        private bool m_invalidated;
        private int m_ProgressInPercentage = 10;

        public VerticalProgressBar()
        {
            m_ProgressInPercentage = 0;

            InitializeComponent();

            // Enable double buffering
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
        }

        public Color FillColor
        {
            get { return m_FillColor; }
            set
            {
                m_FillColor = value;

                ThreadSafeInvalidate();
            }
        }

        public Color BorderColor
        {
            get { return m_BorderColor; }
            set
            {
                m_BorderColor = value;

                ThreadSafeInvalidate();
            }
        }

        public int BorderWidth
        {
            get { return m_BorderWidth; }
            set
            {
                m_BorderWidth = value;

                ThreadSafeInvalidate();
            }
        }

        public int ProgressInPercentage
        {
            get { return m_ProgressInPercentage; }
            set
            {
                m_ProgressInPercentage = value;


                if (m_ProgressInPercentage > 95)
                    Trace.TraceError("Man, that's not good! The value is now about 95%, well, actually it's {0}%", m_ProgressInPercentage);
                else if (m_ProgressInPercentage > 75)
                    Trace.TraceWarning("Uh, oh, the value is still raising, now already by {0}%", m_ProgressInPercentage);
                else if (m_ProgressInPercentage > 50)
                    Trace.TraceWarning("Hmm, the value is going up to now {0}%.", m_ProgressInPercentage);
                else if (m_ProgressInPercentage > 10)
                    Trace.TraceInformation("Aha, something is going on, the value seems to increase, it's now by {0}%", m_ProgressInPercentage);
                else
                    Trace.WriteLine("Cool, nothing to worry about, still below 10%: " + m_ProgressInPercentage + "%", Name);

                ThreadSafeInvalidate();
            }
        }

        private void ThreadSafeInvalidate()
        {
            if (!m_invalidated)
            {
                m_invalidated = true;
                if (InvokeRequired)
                    Invoke(new MethodInvoker(Invalidate));
                else
                    Invalidate();
            }
        }

        protected override void OnResize(System.EventArgs e)
        {
            m_invalidated = true;
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Pen pen = null;
            SolidBrush brush = null;

            base.OnPaint(pe);

            try
            {
                pen = new Pen(m_BorderColor, m_BorderWidth);
                brush = new SolidBrush(m_FillColor);

                Rectangle borderRec = ClientRectangle;

                borderRec.X = borderRec.X + m_BorderWidth / 2;
                borderRec.Y = borderRec.Y + m_BorderWidth / 2;
                borderRec.Width = borderRec.Width - m_BorderWidth;
                borderRec.Height = borderRec.Height - m_BorderWidth;

                int fillHeight = (borderRec.Height * (100 - m_ProgressInPercentage)) / 100;

                Rectangle fillRec = new Rectangle(borderRec.X, borderRec.Y + fillHeight, borderRec.Width, borderRec.Height - fillHeight);

                pe.Graphics.FillRectangle(brush, fillRec);
                pe.Graphics.DrawRectangle(pen, borderRec);
            }
            finally
            {
                m_invalidated = false;

                if (pen != null)
                    pen.Dispose();

                if (brush != null)
                    brush.Dispose();
            }
        }
    }
}

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)


Written By
Founder Gibraltar Software
United States United States
Kendall Miller has been designing, creating, and deploying information systems (hardware, software, and networks) since 1993.  Currently, Kendall is one of the founders of Gibraltar Software, creating developer tools for .NET developers. 

Prior to working at Gibraltar Software, Kendall helped get two Software as a Service startups off the ground creating complete IT infrastructure from the ground up.  He got his career start at John Deere working on Microsoft software and strategies
for the world wide John Deere dealership network.

You can follow the Gibraltar development team at RockSolid.GibraltarSoftware.com.

Kendall lives near Baltimore, MD. 

Comments and Discussions