by Mark Trescowthick - GUI Computing
I can already hear my VB developers groaning - "VB's always been renowned for its string handling... why not just get used to it?". And they're right - VB has always been renowned for its string handling. It does a top job of it too. What it's not too flash at is text handling - something ToolBook excels at.
I mean, let's face it "word 3 of textline 2 of txtVariable" is a heck of a lot more friendly than the maze of Instr(Mid(Left(Right(text,len(text,instr(text))))))) that you'd have to do to do the same job in VB. And VB's always been so primitive that you couldn't even get a control's text at runtime without poking around in the API unless you knew the name of the control. Hopelessly inadequate in ToolBook terms!
That latter restriction goes away in VB5, which is great, and enables you to do nifty things like this :
Private Function GetControlText(sControlName) Dim i As Integer GetControlText = "" For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name = sControlName Then GetControlText = Me.Controls(i).Text Exit Function End If Next i End Function
Of course, that's going to die the death of a thousand cuts if your control doesn't have a .text property, but it certainly comes in handy sometimes. But I digress...
The idea was to create a simple set of functions that 'emulated' ToolBook's line, item and word handling abilities - the first step is obviously to define just what they are.
A Word is any text delimited by non-text characters. Simple, and no funny exceptions.
An Item is any text, including non-text characters like Carriage Returns and Linefeeds, delimited by a given character. In ToolBook, this is a comma, but I decided to broaden the range just a little and allow any nominated character as a delimiter.
A Line (more properly in ToolBook terms, a Textline) is just that - any group of text delimited by CR/LF.
ToolBook also allows you to do nice things like get the number of items or lines in one statement - TextLineCount and ItemCount respectively. So I thought I'd better emulate that as well.
The Code for this simple project is pretty unremarkable, and is included for download. I'm sure no gun VB coder, so I'm sure it can be enhanced - one thing I'd like to have the time to get around to, for example, is turning it into a class module.
The project also includes a tiny (and highly unreliable!) command line emulator for ToolBook which uses these functions to unpick a ToolBook style command line. Just to prove it could be done, I guess.
What is remarkable, at least to me, is the fact that I've currently deployed these particular functions three times :- once in VB, once in Word and once to clean up an Excel spreadsheet destined for a database.
Oh, the joys of a common language!