Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capitalize First Letter in EVERY SENTENCE in a TextBox 7

Status
Not open for further replies.
Right, back to the topic at hand. Capitalising the first word of a sentence as you type. No-one seems to want o take up the RegExp challenge, so here's my version. Note that this a starting point rather than a complete solution, in that the English language has tons of exceptions to the basic rules, which this does not handle (although adding to the pattern being looked for is relatively straightforward). However, it should correcty handle most standard cases.

It takes a tiny bit of setting up, because it makes use of a very under-documented but, IMHO, powerful feature of the current Regular Expression library.

Here's the setup. Firstly, you need to add a reference to the Microsoft VBScript Regular Expressions Library (5.5 or later). Secondly, add a class module to your project (example assumes Class1), and drop in the following simple code:

[tt]
Option Explicit

Public Function ReplacerFunction(ParamArray a()) As String
ReplacerFunction = a(1) & UCase(a(2))
End Function
[/tt]
Now make this the default method of the class (Tools/Procedure Attributes/Advanced and set the ProcedureID to (default))

OK, we're all done with the support class. Here's the main capitalisation function:
[tt]
Option Explicit

Public Function SentenceCapper(strText As String) As String
Dim re As RegExp
Dim strUpper As String
Dim myReplacer As Class1

Set re = New RegExp
re.Global = True
re.MultiLine = True

re.Pattern = "(^|""|[\.!\?]\s+)(\w{1})"

Set myReplacer = New Class1
SentenceCapper = re.Replace(strText, myReplacer) ' This is where the feature gets used...

' Clean up to be on safe side
Set myReplacer = Nothing
Set re = Nothing
End Function
[/tt]
Right, you can now pass any string of text to this function and it should return a capitalised version.

To use it to work as you type (as per previous examples in this thread):
[tt]
Option Explicit

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim oldstart As Long
Dim strUpper As String

With Text1
oldstart = .SelStart
' Keep number of Text1 updates to a minimum...
' If we don't care about this we could have just done:
' .Text=SentenceCapper(.Text)
strUpper = SentenceCapper(.Text)
If strUpper <> .Text Then
.Text = strUpper
.SelStart = oldstart
End If
End With
End Sub


 
Yes I agree CClint...
For team projects it is a good practice...
That is why I said that I do know people that use it.
I, on the other hand never really liked HAVING to declare everything...
I usually do not share code with a team on a project.
I am usually making interface programs to &quot;talk&quot; to other programs using API calls.
And... The programs that I talk to use mainly variants in the API calls so you are not limited to the type you can pass through... some API calls pass different types through the same variable so they require a varient.
Personally I like variants... But They have Ups an Downs just like EVERY other Data type.

I also Agree with the fact that you need to setup a structure... And there is a structure in place...

Personally I stay away from long names...
A variable like Supercalifragilisticexpialidotious is asking for a typo.

The sturcture we tend to use consist of having multiple smaller Subroutines. As most common structures due since the creation of Sub/Function/Procedures...

For counters, coordinates, temporary variables...
Use 1 or 2 letters, You can use comments to tell what they are.

take a look at the sample below:
For myIndexCounter = 1 to 100
myChangingVariable = myIndexCounter * 2
myStringToAddToo = myStringToAddToo + Str(myChangingVariable)
Next myIndexCounter


Functionally is the same as:
For I = 1 to 100 'index
CV = I * 2 'Changing Variable
Temp = Temp + Str(CV) 'Add to temp string
Next 'next index

Note that this is an example for Variable names, I know I could have used:
For I = 1 to 100: Temp = Temp + Str(I * 2): Next

This way you can name almost any variable with 1-5 letters with less chance of a typo...

Make use of cut and paste... Thats what it is there for and there are plenty of ways to do it... <ctrl><C>/<ctrl><V> , <ctrl><Ins>/<shift><ins> , Highlight - Drag (or right drag) , Right click - Copy/Paste, Menu>Edit>Copy/Paste
Right there is 5 different ways to copy... they are not there for good looks.

I am NOT saying that I never make typos... I am saying there are ways to stay away from typos...

most of the time typos occur when you are not paying attention... and you can just as easily type the wrong (existing) variable instead of the one you intended... Option Explicit would not catch that.

Don't Get me wrong on this... I am not 100% aggainst Option Explicit... Like I have said all the way through this thread... It does have good uses... from time to time I do use it for debugging... But usually my bugs are in typos in constant numbers in equations or misplacing a (... so with what I use VB for it is more of a pain for me to use option explicit... I do use Dim quite a bit and most of the time a majority of my variables are defined.

One of the main reasons I like to use VB is ease of use...
For anyone who has used the following 3 languages... tell me if you dissagree on the following:
1) Pascal was a Strict Language where EVERYTHING had to be defined at the top and there were no exceptions... Your definitions had to come before the executable portion of your code, in a block that started with var, and you could not go down and place a variable in the middle of the code without defining it at the top.
2) C/C++ is/was not quite as Strict but still all variable were required to be defined. However you were not limited to Defining everything At the top... it was common to see int Temp = 1; in the middle of a block of code, or for(int i=0,i<10,i++). This was a little easier to deal with and keep up with.
3) then the basic language family... you are not required to define your variables at the top of the program. In the older basic languages they defaulted to single data types unless you used something such as DEFINT A-Z where they would then default to integers, etc... With VB came a whole string of new data types and varity of uses for variables with the integration of OOP (Object Oriented Programing) A variable can now be the original 5 data types as well as Boolean, Byte, Currency, Date, a whole new world of objects, as well as any other user defined data types there are. So they created a new type to default to... Variant... that could store the data from almost any other type. Some people liked this and some still don't. As Pascal Died away and C/C++ gained popularity as well as those still wanting the ease of Basic, they decided to offer Option Explicit, where you could force yourself to define all of your variables as in C and Pascal. And still have the freedom to not use the option and be able to have the undefined variables default to a predetermined type.

This is the reason I prefer VB... there is more freedom and yet there is still quite a bit of power...

I like the ease of use, and to be able to type in a small temporary line of code to make runtime checks in my code and not be forced to define the one-time-use variable when I am going to turn right around and delete it when I get done with it. That is one of the Luxuries of being an interpreter and a compiler... You can make changes at runtime and still be able to compile it for more speed.
Which fits what I use it for perfectly.
The way I use the software works for me and it has for over the past 5 years. Why change what I am doing now.

By the time my code reaches production level my code is well documented with comments etc... to where anyone can read it and be able to maintain it.

Thank you also to strongm, zemp, and JohnYingling for posting code and everyone else for your input into this lengthy discussion...
Like I said... every one has different opinions... also remember, just because something works better for you one way doesn't mean it is the best for EVERYONE else... if Option Explict was the best thing to use for everyone in the world, I believe it would have been buit into to the compiler just like in Pascal and in C/C++, do you not agree?
Some people in the world still use one time variables and variants on a regular basis to test portions of code to develop great pieces of software. Just because you don't use option explicit DOES NOT mean you are not a professional. Even though Option Explicit is a great tool for use in groups that share code back and forth to maintain a required definition of variables used and one type of structure... It is not the only Structure available.
It is still an option that some professionals prefer and others do not, and no one has the right to judge another to say that they are not professional if they do not use it.

these are a few definitions From Online Dictionary:
Professional - having a particular profession as a permanent career.

Profession - a calling requiring specialized knowledge and often long and intensive academic preparation.

In the long run there are not a whole lot of bugs that a breakpoint or two can not catch.

I will give everyone a star that has had input into this discussion due to the fact that it required some time to provide ongoing feedback. (I thought I was done with debate class when I got out of high school... lol... after this... maybe I need to consider being a politician... I think not.)

I just hope that a few of you catch my point, that just because a particular method works great for you, does not mean that it produces the same results for everyone else.
There are a great number of uses for the VB language... Not all of them need the same structure nor the same standards.
Just because some one does not practice the same methods as you do does not mean that they are any less of a professional.

Also strongm, I have never used RegExp before and I am curious as to how it is implemented... could you post a little more information on it. In the VB Documentation, in the index it is greyed out... is it actually a VBScript object? And what are some other common uses for this object? Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
One other thing to think about...
I still see people using line numbers and gosub statements...
This does not nessisarily mean they are not unprofessional... maybe out of date but not unprofessional.
Over the years persons as well as groups develop a varity of standards and way of doing things that differ from group to group and person to person. If one group can be productive and use line numbers, so be it. And if another can go there whole life without touching Subs, Functions, or anything relating to them such as objects, and it works for them, they probably are not going to change just because there is a new way of doing things... Not to say they won't change, but sometimes people find a &quot;groove&quot; that they get comfortable to and when there is a change, even if it is for the best, it can end up to be non-productive, if the change is drastic and requires getting used to. If you ever had to use line numbers... do you remember the first time you did not have to use them? Or going from Goto/Gosub/Return to Subs, functions, libraries, and on into classes and objects...
As for the NO-line numbers and Subs and Functions I, personally, like the change. As for objects, I hated them at first but have begun to develop a &quot;like&quot; for them over the years... I am still getting used to them though.

Now for Option Explicit... it is really not a big deal to me... I could either use it or not and it would not make a difference for me, personally.

I tend to be somewhat &quot;hard-headed&quot; in certain areas and when I get used to doing something one way and excel at it. I do not like to change drastically... if it is not needed. I started programming when I was very young and most of my skills were self taught, typing with one hand and holding a book in the other (with line numbers and goto's). Thank God I got away from the annoying line numbers and the goto crap... for me Subs and Functions are much easier to deal with. Yet I still type with one hand and can now type 60 wpm with one hand. Just because that is the way I have been doing it since I was 10 or 11, now I am going on 23 and if you tell me I should use 2 hands to type because everyone else does and it is professional, I would probably tell you where you could put it... I type twice as fast with one hand than with 2, not to say more accurate... One plus with knowing the keyboard with one hand is that You only have to keep track of where one hand is on the key board thus allowing less typos due to one hand pressing the wrong key before the other... I don't have that problem... and I still have a free hand to use for whatever, whether it is holding a book, a coke, the keyboard, a space mouse or whatever... (even scratching my head trying to figure out API calls and data base queries, with minimum (if any) documentation).
So maybe that will give some of you a few more things to think about. Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
This is, ironicly, an interesting thread...
thread222-277125 Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Regular Expressions:

RegExps are indeed a part of Scripting. When MS decided they needed a scripting language (or two) for use in their browsers they also realised that, to compete, they needed a similar set of features to already available scripting languages such as Perl. Perl borrowed Regular Expressions from Unix, where they were used in a utility called grep.

They allow you to search for pieces of text that match a certain form, instead of searching for a piece of text identical to the one you supply. And they allow you to search a whole chunk of text in one go, rather than having to loop through it. The RegExp engine either returns all the matches found, or can be instructed to make replacements on the fly.

Whilst by no means an authoratative description of regular expressions in general, a reasonable introduction to the Microsoft implementation can be found at:
 

strongm:

What are you passing to the ReplacerFunction method in your posted code? [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
That's the undocumented feature I mentioned. I'm not passing anything myself, but the regexp engine does. Effectively it is a technique that allows you to call your own function for each and every replacement in the text block. The template for the replacer function is:

replaceFunc(matchedString [, subMatch1 [, ...]] , matchPos, source)

i.e for each match made in the string the function gets passed the full match, all the submatches, the position of the match, and the full original text.

This means that you can do some impressive on-the-fly text changes, without having to resort to using the Execute method, and then parsing through the Matches collection.
 

Aaah, yes of course.

But, I'm getting an error 13 on:

re.Replace(strText, myReplacer)

(yes, ReplacerFunction is the default method)

It's almost 7pm. I'll have another look at it tomorrow. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Check which version of the RegExp library you are using. As I mentioned, this technique won't work on versions prior to 5.5
 

hi strongm:
Yes, I am using 5.5
I did another copy and paste, but to no avail....

[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Odd. Can't think of a reason for this behaviour off the top of my head. let me mull it over.
 


In order to try to narrow the problem down, I'd be interested to know who here has used this successfully, and who cannot get it also to work?

Using:

-Win NT
-VB6 SP5
-New Project
-Reference to MS VB Regular Expressions 5.5 (5.6.0.6626)

-Form1 with one Text box and copied code for KeyUp event from above
-Module1 with copied SentenceCapper function from above

-Class1 with copied ReplacerFunction method from above, set as default and tested that it was actually the default method of Class1


[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Just to get the ball rolling (as the author...), it works fine on my W98 SE and NT4 SP6a boxes, both running VB6 Enterprise SP5, IE 6.0, vbscript.dll version 5.6.0.7426
 

strongm: do you know by chance where your version of VBScript.dll came from?

(I have the MSDN subscription, so everything is available here, but do not have everything loaded on all of my systems)

[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
As CCLINT, I didn't managed to make it work.My system is:
-Windows XP
-VB6 - SP5
-Reference to MS VBScript Regular Expressions 5.5

IDE returns, when a key is pressed:
Error 13 - Type mismatch

As CCLINT said, i also don't understand how the myReplacer gets their parameters.

In debug mode, with brakpoint at myReplacer, 1st code line, never get entered this function (it never breaks).

Regards,
Carlos Paiva
 
Ouch! It's beginning to look like the trick doesn't work with anything less than version 5.6 in VB, even though the documentation suggests support was introduced in 5.5.
 
Strongm

It works for me

W2000
VB6
Library VBScript_RegExp_55
C:\WINNT\System32\vbscript.dll\3
Microsoft VBScript Regular Expressions 5.5
(Cut & paste for Object browser)

So there you go!

Matt
 
Works for me
XP
VB6 SP5
vbscript.dll says 5.5 in references but actual file properties show ver 5.6.0.7426 dated 29/8/2002
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
FYI to whoever cares.

Works fine with:

Win NT 4 SrvPk 6a
VB6 SrvPk 5
IE 5.5
vbscript.dll ver 5.5.0.5207

Does Not Work with:

Win2000 SrvPk2
VB6 SrvPk 5
IE 6
vbscript.dll ver 5.6.0.6626


strongm I think your assumption is correct.[neutral]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top