473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick n' Dirty Debug Window

GaryTexmo
1,501 Recognized Expert Top Contributor
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 4495

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

Similar topics

1
4895
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 Queue Manager and local queue. But If I want to run in debug mode how do I enter that two
4
4469
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
8144
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
2353
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 example to demonstrate its use?
6
3380
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 TempRecordset.Fields(i).Name; " "; Debug.Print ", "; TempRecordset.Fields(i).Type Next i I don't understand the numbers returned for field type. Take a...
7
18781
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 2003 The sample in the help file does not work. I tried now for days ... Pls could anybody post a sample, including necessary imports and so on -
1
1861
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 'c:\windows\assembly\gac\system.windows.forms\1.0.3300.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded. 'TextBox.exe': Loaded 'c:\windows\assembly\gac\system\1.0.3300.0__b77a5c561934e089\system.dll', No...
2
3341
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
3634
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 to change the keys (<shift><ctrl>-G or something like that) that open this window. And I really want to be able to open it when I (the programmer)...
2
2555
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 window show me the value of age (rather than the value of the Age property). Is there a way to do this? Thanks!
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.