Important alert: (current site time 7/16/2013 1:01:48 AM EDT)
 

VB icon

A Code Commenter/Uncommenter for VB 3.0

Email
Submitted on: 6/9/1997
By: VB Tips and Source Code 
Level: Not Given
User Rating: By 36 Users
Compatibility: VB 3.0
Views: 21304
 
     Shows you how you can write a program to interface with Visual Basic (version 3.0 in this case).
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A Code Commenter/Uncommenter for VB 3.0
' Description:Shows you how you can write a program to interface with Visual Basic (version 3.0 in this case).
' By: VB Tips and Source Code
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=154&lngWId=1'for details.'**************************************

start up Visual Basic. Remove all custom controls (VBXs) from the project since they are not needed. All we will need is the one default form. Size the form so that it is rather small since it will need to contain only one label and one command button. If you like, set the form's caption property to something like "VB 3.0 Code Commenter/Uncommenter". 
Now, place a label onto the form. Set it's caption property to instruct the user to click the command button below to comment or uncomment VB3.0 code window text. 
Finally, place a command button on the form and set it's caption to " Comment/Uncomment Text", or something along those lines. 
Now, for the code: 
The only procedure we will need is the procedure behind the command button's click event. Listed below is that procedure complete with comments. 
Sub Command1_Click ()
' We don't want the program to crash and burn if an error is encountered so we will
' simply include an ignore all errors type of error handler here.
On Local Error Resume Next
' Toggle the commented state of selected text
' within the project's edit window
' local declarations
Const CF_TEXT = 1 ' This constant is found in CONSTANT.TXT
Const TextWindow = "Microsoft Visual Basic [design]" ' Caption of VB3 main titlebar
Dim TextRetrieved As String ' Text that is retrieved
Dim ModifiedText As String ' Modified text to be pasted back
Dim TextLength As Integer' Length of Retrieved String
Dim LF As String' Line Feed
' Step 1 is to activate VB and obtain any highlighted text
' in the active code window.
' Activcate Visual Basic Design Environment
AppActivate TextWindow
' Copy the Selected Text so it will be placed onto the clipboard
SendKeys "%EC", True
' Read clipboard text
TextRetrieved = Clipboard.GetText(CF_Text)
If TextRetrieved = "" Then
' There was no text copied to the clipboard so exit the procedure
Exit Sub
End If
' Clear Clipboard
Clipboard.SetText "", CF_Text 
LF = Chr$(10)
Dim x As Integer
Dim Spaces As String
Dim WorkText As String
Do
' Where is the first Line Feed character?
x = InStr(TextRetrieved, LF)
If x = 0 Then
' There are no more line feed characters to get out of the loop
Beep
Exit Do
End If
' We want the entire line (excluding carriage return and line feed)
WorkText = Left(TextRetrieved, x - 2)
' Update TextRetrieved since we only want that line once
TextRetrieved = Right$(TextRetrieved, Len(TextRetrieved) - x)
Spaces = ""
Do
' Preserve indentation by transferring spaces
If Left$(WorkText, 1) = " " Then
If Len(WorkText) = 1 Then Exit Do
Spaces = Spaces + " "
WorkText = Right$(WorkText, Len(WorkText) - 1)
Else
Exit Do
End If
Loop
' Toggle the state. If commented, uncomment otherwise comment it
' Being sure to add carriage returns and line feeds when needed
If Left$(Spaces + WorkText, 2) = "' " Then
' This line is commented
WorkText = Spaces + Right$(WorkText, Len(WorkText) - 2)
ModifiedText = ModifiedText + WorkText + Chr$(13) + Chr$(10)
ElseIf Left$(Spaces + WorkText, 1) = "'" Then
' This line is commented
WorkText = Spaces + Right$(WorkText, Len(WorkText) - 1)
ModifiedText = ModifiedText + WorkText + Chr$(13) + Chr$(10)
Else
' This line is NOT commented
WorkText = "' " + Spaces + WorkText
ModifiedText = ModifiedText + WorkText + Chr$(13) + Chr$(10)
End If
Loop
' Paste this code back to VB
' To do this, put the new code back onto the clipboard
Clipboard.SetText ModifiedText, CF_Text
' Make sure VB is still the active application
AppActivate TextWindow
' Since the text in VB is still highlighted, pasting
' the new text will simply replace what was highlighted
SendKeys "%EP", True
' Clear the clipboard once we're done
Clipboard.SetText "", CF_Text
End Sub
Now, that's all the code there is. You can run the program to make sure that there are no errors in your code, but the commenter/uncommenter will not work until you have compiled it. After you compile the program, leave VB up, but run the EXE also. If necessary, arrange the windows so you can see both the commenter program and a code window in VB. Highlight text in VB's code window, then click the button on your program and watch it do its magic. 
Keep in mind, that if you highlight text that has both commented and uncommented lines, it will reverse ALL of them (sometimes not a desired trait). Only highlight text that is in one state or the other (all highlighted text commented, or all uncommented). Now you have no need to buy a program to do it for you!


Other 13 submission(s) by this author

 


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 Not Given 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

12/4/2002 8:01:45 AMMichiel Schermer

Goto www.bizzit.com , you can chat, share files, make profiles, own mailbox, voice chat, and a lot more (10295)
(If this comment was disrespectful, please report it.)

 
4/20/2006 1:18:01 PMhooman

tarkan
(If this comment was disrespectful, please report it.)

 
10/10/2010 9:31:49 AM

okey!!
(If this comment was disrespectful, please report it.)

 

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.