473,440 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,440 developers and data experts.

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 4481

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

Similar topics

1
by: David Yu | last post by:
Hi, anybody knows how to use debug window to start console application program with input arguments. In DOS window I can start my application program likes MQGET QTT1 DAVID.LOCAL which input...
4
by: Thomas Anderson | last post by:
I would like to replicate the debug window on a form so I can easily pass usefull info to users on long processes. Is there a simple way to do this. thanks
3
by: Mike Turco | last post by:
Is there a way to clear the debug window other than just sending a bunch of line feeds? Tanks -- Mike
2
by: MLH | last post by:
What is the top pane of the ms access 97 debug window used for? I'm used to the Access 2.0 immediate window. I cannot figure out what the top pane is all about. Could someone give me a small...
6
by: MLH | last post by:
I have this little snippet running whenever I want to list my table field names in the debug window, along with their field types... For i = 0 To TempRecordset.Fields.Count - 1 Debug.Print...
7
by: Thomas Pecha | last post by:
Sorry for all who think this is easy, I was not able to handle this Coming from VB6 where with simple debug.print strAString you could write to debug window, I am totalling failing in vb.net...
1
by: Adrian Constantinescu | last post by:
Hi, When a project VB.Net start, the output window if filled with this kind on informations: 'TextBox.exe': Loaded...
2
by: MLH | last post by:
Seems ctrl-g is the hotkey combo to open debug window. W/O using sendkeys, can I open the debug window from VBA?
11
by: Yannick Turgeon | last post by:
Hello all, I'm using A97 and W2k. We have a little problem here. Sometimes (often?), users (all co-workers) are hitting <ctrl>-G by mistake and are stucked in the debug window. I would like...
2
by: Johann Schuler | last post by:
Let's say I have a Person class with a private int age member variable. I have a get and set accessor for the Age property. When I am running the code in debug mode, I would like to have a debug...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.