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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Length of a string in a text box

Status
Not open for further replies.

Toman

Technical User
Mar 30, 2004
190
CZ
Hi,
Sorry for such a basic question.

In textbox one writes a character expression that I later search in a table.
Because text property returns some additional spaces I use
Code:
ALLTRIM(thisform.txtFind.text)
as a search string.
It looked good till now, when someone want to distinguish between "Smith Wesson" and "SmithWesson". He writes "Smith " (space after the letter h) into the box, willing not to get SmithWesson records as an answer, but he gets back both types of records, because the space in his question is discarded by ALLTRIM function.
To solve this I can imagine only creating my own text property by collecting characters as they are typed (KeyPress method, watch out backspace, etc.). I hope that professionals are dealing with this other way.

Thank you for any tip, Tom.
 
why make it more complicated. If it's a requirement to be able to enter spaces as part of a search strng, then simply don't alltrim, take the pure thisform.txtFind.Text or thisform.txtFind.Value

Bye, Olaf.
 
Tom,

If you simply want to discard all internal spaces, you can search on STRTRAN(thisform.txtFind.Value, " ", "").

If you want to prevent the user entering spaces in the first place, look at the InputMask and Format properties. For example, by setting Format to A, you can force the user to enter letters only.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Lets begin by understanding that "because the space in his question is discarded by ALLTRIM function" is not accurate.

The ALLTRIM() function will remove leading and trailing blank spaces only and not remove any spaces within a character string.

You can test this yourself:
Code:
mTest = " Smith Wesson  "
?"Test1: |" + mTest + "|"
?"Test2: |" + ALLTRIM(mTest) + "|"
?"Test3: |" + STRTRAN(mTest," ","") + "|"

Mike's suggestion above of using STRTRAN() or even CHRTRAN() will, when used as he suggests, remove all occurrences of blank spaces.

NOTE - if this input text is being used to search for existing values within a table, etc. be aware of the consequences of the SET EXACT parameter.
Since an ALLTRIM() will remove leading and trailing spaces, the length of the resultant value might be less than expected thereby resulting in a possible not found value if SET EXACT ON were in effect.

Good Luck,
JRB-Bldr

 
Just a note on Olaf's suggestion. If it wouldn't bother your users too much, I would warn them when there are trailing blanks to make sure they understand what they are really searching for.
Code:
IF LEN(ALLTRIM(thisform.txtFind.text)) <> LEN(thisform.txtFind.text)...

Auguy
Northwest Ohio
 
Sorry, Mike, jrbbldr

It's not about internal spaces in the search string, it's about the results, if there are results 'SmithWesson' and 'Smith Wesson', then searching for 'Smith' finds both, while searching for 'Smith ' finds only 'Smith Wesson', which is what the user wants. If you trim the search term you'll always find both.

So simply don't trim the search term. You might do as Auguy suggests and ask the user, if they really want to search including a trailing space in the search term, but you could simply display what they were searching eg as status bar text: "search results for [Smith ]", then they know they typed a space in the search term.

Bye, Olaf.
 
There is no easy solution. If you ALLTRIM() then you lose the space the user typed in. If you don't ALLTRIM, then the search will always include all trailing spaces and it's likely that you won't get a match.

Best solution is to train the user that not all searches will find an exact match and ALLTRIM() it. Then if more than one record meets the search crteria, display the results in a grid so the user can choose the one they want.

Craig Berntson
MCSD, Visual FoxPro MVP,
 
Many thanks to all people posting here.

I've studied all thoroughly and I see now, that my problem is more difficult to solve then I ever thought. Now the reason seems to be clearer to me.

The textbox consists of text1.MaxLength characters and/or spaces all the time (if text1.MaxLength is zero, the number of characters is something corresponding with textbox length, maybe with font size also, I don't know, it doesn't matter here).

If one writes into the textbox the text is continuously padded with spaces to a mentioned length. It means that writing spaces at the end of a string is effectively the same as moving the cursor right by cursor arrows. It has no impact to the textbox value and therefore cannot be recognized or distinguished. In other words – one only writes spaces where they already are.

I wanted to give it up at that moment, but here comes the last inspiration: exploit SelStart property.

With this I take a search string as left x characters from textbox.value, where x is either
length of right trimmed textbox.value: LEN( RTRIM( Thisform.text1.text))
or
position of cursor: Thisform.text1.SelStart
- whatever is bigger.

Packed together:
Code:
cLookFor = LEFT( Thisform.text1.value, MAX( LEN( RTRIM(Thisform.text1.text)), Thisform.text1.SelStart))

At the first moment it looks acceptable, must get more testing.
Demo follows:
Code:
PUBLIC oform1

oform1=CREATEOBJECT("form1")
oform1.Show
RETURN

DEFINE CLASS form1 AS form

	Height = 97
	Width = 297
	DoCreate = .T.
	AutoCenter = .T.
	Caption = "Form1"
	Name = "Form1"

	ADD OBJECT text1 AS textbox WITH ;
		Height = 24, ;
		Left = 101, ;
		Top = 8, ;
		Width = 182, ;
		Name = "Text1"

	ADD OBJECT text2 AS textbox WITH ;
		Value = "><", ;
		Height = 25, ;
		Left = 101, ;
		Top = 56, ;
		Width = 182, ;
		Name = "Text2"

	ADD OBJECT label1 AS label WITH ;
		Caption = "Look for:", ;
		Height = 18, ;
		Left = 20, ;
		Top = 13, ;
		Width = 70, ;
		Name = "Label1"

	ADD OBJECT label2 AS label WITH ;
		Caption = "Looking for:", ;
		Height = 18, ;
		Left = 21, ;
		Top = 58, ;
		Width = 70, ;
		Name = "Label2"

	PROCEDURE text1.KeyPress
		LPARAMETERS nKeyCode, nShiftAltCtrl
		DODEFAULT()			&& SelStart must be read after key press is completed
							&& needed for this demo only

		cLookFor = LEFT(Thisform.text1.value, MAX( LEN( RTRIM(Thisform.text1.text)), Thisform.text1.SelStart))
		Thisform.text2.value = ">" + cLookFor + "<"

		NODEFAULT			&& don't write twice
	ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************

Thank you for opening my head, Tom.
 
Tom, sorry but I disagree.

If I've understood it right, what you are saying is that the result of your search will depend in part on the current SelStart setting, that is, on the location of the cursor.

This is not exactly intuitive, and not what the user would expect. Just look at Google. Do you have to worry about the placement of the cursor when doing a search?

I'd also disagree with any solution that involves "training the users". It's up to us to make our software intuitive. Users are accustomed to doing searches in other software, and won't expect to use our software any differently.

I know this doesn't help you find a solution, but I do feel you need to keep users' expectations in mind.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Hi,

I know it's not worth much but I have to put in my 2 cents.

I'll have to agree with Mike. Training the user is not the solution.

My opinion is that you have to be aware of all the ways a user can input data in a form and defend for them.

Just my 2 cents.


Thanks,
F1
 
If you trap the input keystrokes or maybe use the
interactive change method you can simply add the
space back after trimming like this

GcMSTK = UPPER(ALLTRIM(THIS.VALUE))
IF LASTKEY()=32 .AND. LEN(GcMSTK)>0
GcMSTK=GcMSTK+' '
ENDIF
This will produce the desired results.

Charlie Huff
CJ's Homecenter












 
Craig,

If you put search text that ends with a space in Google, does it search for the string + the space?

Surely not. I wouldn't expect it to, nor would most users. Try searching for FoxPro, and then for FoxPro plus some spaces. I'd expect to see pretty well the same result.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Simply ditch the maxlength and the textbox value will only hold the entered text. If someone enters longer text you can trim it to a max length when searching.

Even if google does not search that way with trailing space, it's not wrong to do so, and you have your reasons. Google does search for spaces if you put your search term in quotes: "search ". You could do that too, but I'd find it easier if you display the searched term in [] in the status bar or in some label at the result list.

Bye, Olaf.
 
Olaf,

but how to take that searched term (spaces inclusive) from the textbox to show them in [] in the status bar or somewhere… Do you take my above suggestion (with cLookFor = LEFT…) for appropriate?
I am somewhat sad after reading Mike's post and this would make me feel better.

Thank you, Tom
(try the demo please)
 
I don't like the idea, because it's much too complicted.

Code:
cLookFor = thisform.txtSearch.Value
SQL SELECT ... Where field = cLookfor
*...
SET MESSAGE TO 'You searched for ['+cLookfor+'].'

Where txtSearch is a simple textbox without inputmaske, maxlength or controlsource set. now if the user typed a space at begin or end of the textbox, he'll see in the status bar message and can change, if leading/trailing spaces were not meant to be searched.

Bye, Olaf.
 
Ran your demo, it's okay, you display in a secondary textbox, what the user will serch for marked with >search<.

Much too cpmplicated though. I'd simply show '>'+txtSearch.Value+'<' there, or in the status bar via SET MESSAGE. Nothing that fancy. No need for the complicated operations you do, just so you can set maxlength. Why set it at all?

Bye, Olaf.
 
Olaf, there is no problem with visualization a variable in VFP. In demo I show search string in separate textbox, so that you/they could in "real time" see, what is sending for searching, especially those leading and trailing spaces.
In real app I'd probably select other way, according to the forms design.

What I still don't know is how to get entered text (with leading/ trailing spaces of course).
You write:
Simply ditch the maxlength and the textbox value will only hold the entered text
Please, could you explain it to me in other words, best without "ditch".
150px-Sloot.JPG

This is a well maintained ditch according to Wikipedia. Thank you Tom
 
Ditch" can be used as a verb in English, meaning "throw out." No doubt that comes from the idea that you can discard things by throwing them into a ditch.

Tamar
 
Hi Toman,

What Olaf is trying to say by 'ditch the maxlength and the textbox value will only hold the entered text' is to "get rid of the maxlength and the textbox value will only hold the entered text"

The meaning/definition of "ditch" is:

"To get rid of" or "To discard".


Thanks,
F1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top