473,395 Members | 1,393 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,395 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 4479

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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.