469,934 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Quick n' Dirty Debug Window

GaryTexmo
1,501 Expert 1GB
Nothing amazing here, this is just a base groundwork for a debug window that someone could use to output debug text in a windows application. It provides fairly basic functionality so feel free to modify to suit your purposes.

The topic came up in a question thread (somewhat) and mostly it reminded me that I had been meaning to make one of these so figured I'd share it. It's important to note that there are other methods of gathering debug information in a windows application. This is simply a one method of doing so packaged in the form of a fairly lightweight class.

Feel free to inform me of any glaring issues and please enjoy. Happy coding!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4.  
  5. namespace SomeNamespace
  6. {
  7.     public class DebugWindow
  8.     {
  9.         #region Public Enum
  10.         /// <summary>
  11.         /// The ways in which a debug window can write its text
  12.         /// </summary>
  13.         public enum WriteModes
  14.         {
  15.             Prepend,
  16.             Append
  17.         }
  18.         #endregion
  19.  
  20.         #region Private Members
  21.         private Form m_hostForm = new Form();
  22.         private RichTextBox m_console = new RichTextBox();
  23.         private WriteModes m_writeMode = WriteModes.Append;
  24.         #endregion
  25.  
  26.         #region Constructor
  27.         public DebugWindow()
  28.         {
  29.             // Set up our form and put a textbox on it
  30.             m_hostForm.Size = new Size(600, 400);
  31.             m_hostForm.FormClosing += new FormClosingEventHandler(m_hostForm_FormClosing);
  32.             m_hostForm.MinimizeBox = false;
  33.             m_hostForm.MaximizeBox = false;
  34.             m_hostForm.Text = "Debug";
  35.  
  36.             m_console.Location = new Point(0, 0);
  37.             m_console.Size = new Size(m_hostForm.Width - 8, m_hostForm.Height - 35);
  38.             m_console.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
  39.             m_console.ReadOnly = true;
  40.             m_console.BorderStyle = BorderStyle.None;
  41.  
  42.             m_hostForm.Controls.Add(m_console);
  43.         }
  44.         #endregion
  45.  
  46.         #region Public Properties
  47.         /// <summary>
  48.         /// The write mode for this debug window
  49.         /// </summary>
  50.         public WriteModes WriteMode
  51.         {
  52.             get { return m_writeMode; }
  53.             set { m_writeMode = value; }
  54.         }
  55.  
  56.         /// <summary>
  57.         /// The window location
  58.         /// </summary>
  59.         public Point Location
  60.         {
  61.             get { return m_hostForm.Location; }
  62.             set
  63.             {
  64.                 m_hostForm.Location = new Point(value.X, value.Y);
  65.                 m_hostForm.StartPosition = FormStartPosition.Manual;
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// The window size
  71.         /// </summary>
  72.         public Size Size
  73.         {
  74.             get { return m_hostForm.Size; }
  75.             set { m_hostForm.Size = new Size(value.Width, value.Height); }
  76.         }
  77.  
  78.         /// <summary>
  79.         /// The debug text for this window
  80.         /// </summary>
  81.         public string DebugText
  82.         {
  83.             get { return m_console.Text; }
  84.         }
  85.  
  86.         /// <summary>
  87.         /// The title for this debug window
  88.         /// </summary>
  89.         public string WindowTitle
  90.         {
  91.             get { return m_hostForm.Text; }
  92.             set { m_hostForm.Text = value; }
  93.         }
  94.         #endregion
  95.  
  96.         #region Public Methods
  97.         /// <summary>
  98.         /// Show the debug window
  99.         /// </summary>
  100.         public void Show()
  101.         {
  102.             m_hostForm.Show();
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Hide the debug window
  107.         /// </summary>
  108.         public void Hide()
  109.         {
  110.             m_hostForm.Hide();
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Writes a string to the debug window, ending in a line termination character
  115.         /// </summary>
  116.         /// <param name="str">The string to write</param>
  117.         public void WriteLine(string str)
  118.         {
  119.             Write(str);
  120.             Write(Environment.NewLine);
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Writes a line termination character to the debug window
  125.         /// </summary>
  126.         public void WriteLine()
  127.         {
  128.             Write(Environment.NewLine);
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Writes a string to the current debug window
  133.         /// </summary>
  134.         /// <param name="str"></param>
  135.         public void Write(string str)
  136.         {
  137.             // Write depending on the write mode
  138.             switch (m_writeMode)
  139.             {
  140.                 case WriteModes.Append:
  141.                     m_console.AppendText(str);
  142.                     break;
  143.                 case WriteModes.Prepend:
  144.                     m_console.Text = str + m_console.Text;
  145.                     break;
  146.             }
  147.         }
  148.  
  149.         /// <summary>
  150.         /// Clears the debug window
  151.         /// </summary>
  152.         public void Clear()
  153.         {
  154.             m_console.Clear();
  155.         }
  156.         #endregion
  157.  
  158.         #region Event Handlers
  159.         void m_hostForm_FormClosing(object sender, FormClosingEventArgs e)
  160.         {
  161.             // Don't allow the form to be disposed...
  162.             this.Hide();
  163.             e.Cancel = true;
  164.         }
  165.         #endregion
  166.     }
  167. }
  168.  
NOTE: Change the namespace to whatever you like.

To use this, simply instantiate a debug window for your form (or in a static class), then put debug messages in important places where you need info. As your program runs, it will output the debug messages to this window and if you show it, you will be able to see it just as if you had a console.
Jun 4 '10 #1
0 4167

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Thomas Anderson | last post: by
3 posts views Thread by Mike Turco | last post: by
2 posts views Thread by MLH | last post: by
7 posts views Thread by Thomas Pecha | last post: by
1 post views Thread by Adrian Constantinescu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.