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
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- namespace SomeNamespace
- {
- public class DebugWindow
- {
- #region Public Enum
- /// <summary>
- /// The ways in which a debug window can write its text
- /// </summary>
- public enum WriteModes
- {
- Prepend,
- Append
- }
- #endregion
- #region Private Members
- private Form m_hostForm = new Form();
- private RichTextBox m_console = new RichTextBox();
- private WriteModes m_writeMode = WriteModes.Append;
- #endregion
- #region Constructor
- public DebugWindow()
- {
- // Set up our form and put a textbox on it
- m_hostForm.Size = new Size(600, 400);
- m_hostForm.FormClosing += new FormClosingEventHandler(m_hostForm_FormClosing);
- m_hostForm.MinimizeBox = false;
- m_hostForm.MaximizeBox = false;
- m_hostForm.Text = "Debug";
- m_console.Location = new Point(0, 0);
- m_console.Size = new Size(m_hostForm.Width - 8, m_hostForm.Height - 35);
- m_console.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
- m_console.ReadOnly = true;
- m_console.BorderStyle = BorderStyle.None;
- m_hostForm.Controls.Add(m_console);
- }
- #endregion
- #region Public Properties
- /// <summary>
- /// The write mode for this debug window
- /// </summary>
- public WriteModes WriteMode
- {
- get { return m_writeMode; }
- set { m_writeMode = value; }
- }
- /// <summary>
- /// The window location
- /// </summary>
- public Point Location
- {
- get { return m_hostForm.Location; }
- set
- {
- m_hostForm.Location = new Point(value.X, value.Y);
- m_hostForm.StartPosition = FormStartPosition.Manual;
- }
- }
- /// <summary>
- /// The window size
- /// </summary>
- public Size Size
- {
- get { return m_hostForm.Size; }
- set { m_hostForm.Size = new Size(value.Width, value.Height); }
- }
- /// <summary>
- /// The debug text for this window
- /// </summary>
- public string DebugText
- {
- get { return m_console.Text; }
- }
- /// <summary>
- /// The title for this debug window
- /// </summary>
- public string WindowTitle
- {
- get { return m_hostForm.Text; }
- set { m_hostForm.Text = value; }
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Show the debug window
- /// </summary>
- public void Show()
- {
- m_hostForm.Show();
- }
- /// <summary>
- /// Hide the debug window
- /// </summary>
- public void Hide()
- {
- m_hostForm.Hide();
- }
- /// <summary>
- /// Writes a string to the debug window, ending in a line termination character
- /// </summary>
- /// <param name="str">The string to write</param>
- public void WriteLine(string str)
- {
- Write(str);
- Write(Environment.NewLine);
- }
- /// <summary>
- /// Writes a line termination character to the debug window
- /// </summary>
- public void WriteLine()
- {
- Write(Environment.NewLine);
- }
- /// <summary>
- /// Writes a string to the current debug window
- /// </summary>
- /// <param name="str"></param>
- public void Write(string str)
- {
- // Write depending on the write mode
- switch (m_writeMode)
- {
- case WriteModes.Append:
- m_console.AppendText(str);
- break;
- case WriteModes.Prepend:
- m_console.Text = str + m_console.Text;
- break;
- }
- }
- /// <summary>
- /// Clears the debug window
- /// </summary>
- public void Clear()
- {
- m_console.Clear();
- }
- #endregion
- #region Event Handlers
- void m_hostForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- // Don't allow the form to be disposed...
- this.Hide();
- e.Cancel = true;
- }
- #endregion
- }
- }
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.