Code Commenting Made A Little Easier

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
[background=transparent]All,[/background]
[background=transparent]In many of the examples that I post here, I try to include comments; it does help the others to understand things. In recent history though I’ve been reluctant to because, let’s face it -- it’s a pain in the … neck. ;-)[/background]
[background=transparent]Aside from that though, formatting them can be a real hassle which is made ESPECIALLY difficult if you decide to modify some parts of it, because now the flow of the text is all screwed up if you just typed them in free-form.[/background]
[background=transparent]Given that, and after an example that I posted earlier today wherein I had quite a lot of prose included in comments, I decided to at least make this second part easier: A program to format the comments with a button which will copy the formatted comment(s) out to your clipboard.[/background]
[background=transparent]I thought I’d share that here today so that if anyone else might ever find use for it they can likewise make life easier on themselves. :)[/background]
[background=transparent]If you’d like to build it yourself, then start with a form similar to the one shown below:[/background]
[background=transparent]
df212f7ca7d4673cb4f04439dc3cecfb.png
[/background]
[background=transparent]The controls are as follows:[/background]
[background=transparent].[/background]
[background=transparent]Control Type[/background]
[background=transparent]Name[/background]
[background=transparent]Comments[/background]
[background=transparent]TextBox[/background]
[background=transparent]tb_NumCharsPerLine[/background]
[background=transparent]This is the textbox where you will enter the maximum number of characters you want shown per line of comment.[/background]
[background=transparent]The program will use this to determine if the next ‘word’ will be too long and, if so, will force a new line then continue on to the end.[/background]
[background=transparent]TextBox[/background]
[background=transparent]tb_OriginalText[/background]
[background=transparent]This is the textbox shown below the label which shows “Type the text to convert” and is where you’ll type (or paste) in the comment(s).[/background]
[background=transparent]TextBox[/background]
[background=transparent]tb_CommentBlock[/background]
[background=transparent]This is the textbox shown below the label which shows “Comment block” and is where you’ll see the formatted text once you click on the ‘Convert’ button.[/background]
[background=transparent]Button[/background]
[background=transparent]btn_Convert[/background]
[background=transparent]Button text = “Convert” and its function is obvious.[/background]
[background=transparent]Button[/background]
[background=transparent]btn_ClearAll[/background]
[background=transparent]Button text = “Clear All” and its function is obvious.[/background]
[background=transparent]Button[/background]
[background=transparent]btn_CopyToClipboard[/background]
[background=transparent]Button text = “Copy To Clipboard” and its function is obvious.[/background]
[background=transparent]ErrorProvider[/background]
[background=transparent]ErrorProvider1[/background]
[background=transparent]This is used along with the .Validating event for the number of characters per line of code.[/background]
[background=transparent].[/background]
[background=transparent]The program works pretty much like you can ascertain that it does just by the way it’s laid out so no need to elaborate on anything but I will show you an example. I took this text from something that I posted as a comment section earlier today.[/background]
[background=transparent]The original text is in a text file which I’ve[/background][background=transparent][background=transparent]uploaded here[/background][/background][background=transparent], and following shows the resulting output when set to 50 characters maximum per line of comment:[/background]
[background=transparent]
e88f558014ddbc21a27e5681bdfe04eb.png
[/background]
[background=transparent]Resulting Comment Code:[/background]
This event - the .Validating event - is the star
of the show. When I post this code Ill include
a link that has information about the event and
I hope youll take time to read through it -
which will include the sequence of events from
the point that the control (in this case a
TextBox) has focus to the point that the focus
is lost.

Youll notice that this event sure does look a
lot like the .Leave event, but thats where the
comparison ends. This event also fires when the
TextBox has lost focus (obviously it has to
first have HAD focus to then lose it), but the
Event Arguments are entirely different, as
youll see momentarily in the code of this
section.

I am going to assume that the value for the
quantity of spools must be a whole number (i.e.,
an integer) so, for example, the quantity cannot
be 1.3 spools. If it CAN be a portion of a
spool, then you want to use a type double which
also has a TryParse method (all numeric types
do) which will be used to verify that it can
safely be converted to an integer.

I will likewise post a link to show information
about Integer.TryParse but Im going to try to
"show by example" in the code in this event.

In order to use TryParse, we have to have
something to tell it to cast the resulting
conversion to.

We can get into casting on some other day, but
suffice for this purpose that it needs a
variable to set to be the value of the attempted
conversion (if successful).

Ill use a variable thats only declared at this
level - once this sub has run, its no longer in
scope and can then be attended to by the GC if
required; that is, the "Garbage Collector" can
reclaim the memory of it which is done on an as
needed basis.

Dim tempInt As Integer = 0

The first test is "can it be converted to a type
integer?" and well do that next BUT ... we need
to leave the user an "out". I will use one of
the event args of this event to lock the user
into the control (the TextBox) until theyve
typed in something valid, but that carries with
it the danger that they may simply not know or
who knows why, but the fact that the program
will not allow them to leave this control until
it passes the tests performed in this event,
quite literally they wont even be able to turn
the program off. Nope, not until they type in a
valid entry!

Well first test that the TextBox isnt empty.
If its empty, thats their way out. In fact if
its NOT empty, the message shown to them will
indicate that they can clear the text entirely
to continue the program (and/or to close the
Program).

Ah yes, now what happens when they clear the
text out?

Look at the event above - the .TextChanged
event. If its blank then fine - but the
Calculate button wont be enabled so they cant
try to calculate the amount based on an empty
string for the quantity of spools to order.

[background=transparent]I have the code on a[/background][background=transparent][background=transparent]page of my website here[/background][/background][background=transparent] and … there are no comments! *LOL* Well honestly, it’s pretty simple and doesn’t really need any, but I had to laugh at that. ;-)[/background]
[background=transparent]I hope someone might find use from it. :)[/background]
[background=transparent][/background]
Please call me Frank :)

View the full article
 
Back
Top