VB icon

First time ever! Clear the Debug/Immediate window with code!

Email
Submitted on: 2/19/2015 7:00:00 PM
By: José Lucio Gama (from psc cd)  
Level: Advanced
User Rating: By 4 Users
Compatibility: VB 5.0, VB 6.0
Views: 4510
 
     For the first time ever, a code that can clear the debug window from code! It's actually a cheat, since VB5-VB6 doesn't allow to clear the window without stopping the running project - the program sets the focus on the debug window, go to the last char and start printing a lot of empty lines - effectively getting rid of all clutter visible on the window.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :First time ever! Clear the Debug/Immediate window with code!
'**************************************
FindWindow, SetForegroundWindow and SetFocus
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: First time ever! Clear the Debug/Immediate window with code!
' Description:For the first time ever, a code that can clear the debug window from code!
It's actually a cheat, since VB5-VB6 doesn't allow to clear the window without stopping the running project - the program sets the focus on the debug window, go to the last char and start printing a lot of empty lines - effectively getting rid of all clutter visible on the window.
' By: José Lucio Gama (from psc cd)
'
' Assumes:Win32 API knowledge
'**************************************

Put this on the top of your form:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Then, paste this on your code:
Private Sub clearDebug()
'this will try to get the handle to your
'Immediate window. To make this work on
'VB4, you can change the string "Immediate"
'to "Debug"
parent_hwnd = FindWindow(vbNullString, "Immediate")
If parent_hwnd = 0 Then Exit Sub
' Set the focus on the debug window
SetFocusAPI parent_hwnd
'go to the last line / position on the window
'(same as pressing CTRL + END on your keyboard
SendKeys "^{END}", True
'you can adjust the number of lines
'printed according to your Immediate 
'window size
For i = 1 To 100
Debug.Print ""
Next
'give the focus back to your program!
SetForegroundWindow Me.hwnd
End Sub
Then just call clearDebug() anywhere in your codeand that it!


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.