Comments on Ends of Lines and Comment Blocks
First, some comments about end-of-line markers.
You don't need to know this as a Windows user, but Windows, the Mac, and Linux use different end-of-line markers: the Mac uses Chr(13) all of the time and Linux uses Chr(10) all of the time, while Windows uses Chr(13) + Chr(10) ... most of the time (more about that in a minute). REALbasic, however, has a useful constant which varies according to the operating system on which RB is being run: EndOfLine.
If you're running RB on a Mac, EndOfLine is always Chr(13). If you're running RB on Linux, EndOfLine is always Chr(10). And if you're running RB in Windows, EndOfLine is always Chr(13) + Chr(10). Can you see the problem? EndOfLine works most of the time in Windows, but NOT all of the time.
Here's what you need to know: in Windows, the end-of-line marker is NOT Chr(13) + Chr(10) in text in an EditField, but (don't ask me why - ask Microsoft!) simply Chr(13). That's important, because if you want to divide text in an EditField into separate lines (say, so that you can put each line into an element in a string array), EndOfLine won't work for you.
Don't take my word for it. Prove it for yourself. Put an EditField and a PushButton in a window. Put this code in the Action event handler for the PushButton:
Dim Lines(-1) As String
Lines = Split (EditField1.Text, EndOfLine)
MsgBox Lines(0)
Run the program, type "one" in the EditField, press enter, type "two," press enter, type "three," and press enter. The MsgBox (contrary to what you might expect) will display this:
one
two
three
Split did NOT split the EditField text into separate lines!
Now change the the PushButton Action event handler to read like this:
Dim Lines(-1) As String
Lines = Split (EditField1.Text, Chr(13))
MsgBox Lines(0)
This time the MsgBox will display this:
one
The preceding has nothing to do with my next topic, which is the matter of commenting blocks, which I find to be a very helpful feature of REALbasic. The only thing is that I prefer // to ' as a comment marker. Thus I wrote a "commenter" program, which has no other purpose than to allow you to do just that.
Brief digression: years ago I wrote a "paster" program in Visual Basic which also made use of an always-on-top utility program. It worked beautifully: click on a button, and not only would associated "boilerplate" text be inserted at the appropriate point in the text of whatever program you were using (e.g., Notepad, WordPad, Microsoft Word, or whatever), but the focus was also immediately restored to that window, so that you could immediately continue work from where you left off.
Well, with one of Microsoft's updates (either to the Windows operating system or to Visual Basic, but my guess is the former), that feature stopped working. It became impossible for me to restore automatically the focus back to the original program (or at least so difficult that I could never figure out how to do it) or, for that matter, to paste the text into that original program automatically. And I have the same problem in REALbasic that I had in Visual Basic, so two additional steps are now needed
If you're happy with the apostrophe as a comment marker, use the built-in routines in the RB IDE to "comment block" and to "uncomment block." If you - like me - prefer two slashes as a comment marker, then here are the directions on how to use my commenter.rbp:
To comment a block within the RB IDE, with commenter.rbp compiled and running:
(1) Select the block of text you want to comment out.
(2) Use Ctrl-Insert to copy the text to the Windows clipboard.
(3) Click on "Comment Lines" in the "commenter" program
(4) Click on someplace safe in the RB IDE that will leave the selected text still highlighted (the title bar for the RB IDE is fine).
(5) Use Shift-Insert to paste the (now commented) block into the RB IDE (replacing the selected text).
To uncomment a block within the RB IDE, with commenter.rb compiled and running:
(1) Select the block text you want to uncomment.
(2) Use Ctrl-Insert to copy the text to the Windows clipboard.
(3) Click on "Uncomment Lines" in the "commenter" program
(4) Click on someplace safe in the RB IDE that will leave the selected text still highlighted (the title bar for the RB IDE is fine).
(5) Use Shift-Insert to paste the (now uncommented) block into the RB IDE (replacing the selected text).
By the way, when you select the text, I find that it is best to begin with the first actual character of the first line and end with the entire last line to be commented (or uncommented).
One final comment: In RB 2005rx and in RB 2006r1, Ctrl-C, Ctrl-X, and Ctrl-V do not always work (it's an apparent bug) to Copy, Cut, and Paste text, but Ctrl-Insert, Shift-Delete, and Shift-Insert do seem to work consistently. (At least that has been my experience.)
Barry Traver
Home Page for This Blog: http://traverrbw.blogspot.com/
Programs and Files Discussed in the Blog: http://traver.org/traverrbw/
1 Comments:
Steve,
I forgot to mention that. You are absolutely right. And you have to use ReplaceLineEndings again, if you've manipulated the text and want to put it back into the EditField, so that Chr(13) rather than Chr(13) + Chr(10) is again the end-of-line marker for EditField.Text.
Barry
Post a Comment
<< Home