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

Textbox control - accepting a number

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I apologise for such a simple question.

I have a text box in which I wish the user enter a number. When he enters 23, then txtmynumber.value has the value "23" and it has a data-type C (character)

I have optimistically tried specifying the Input mask as "99", but this does not help. I realise that I can use the VAL() function, but is it possible to make txtmynumber.value have a numeric data type?
 
Set value to 0 initially, and you have a numeric textbox.

Just a side note: I gave this tip elsewhere recently, this is perhaps the third or fourth in a row, while this hasn't been asked for years before this series. I wonder how that happens, I know you can't answer that.

Bye, Olaf.
 
Wow! That is a very quick response, thank you.

In answer to your question, random events are not for us to predict! The underlying reason for the question being asked at all is, I suppose, because it is not particularly intuitive! Perhaps the language could have been designed so that the textbox.value itself had a property of its data type which you can set without having to have an actual value of zero.

I don't mind having an initial value of 0 displayed, but it is perhaps more common on data entry forms (and certainly paper forms) for a field to be blank rather then zero.

I suspect many people had (like me) thought that the Format or InputMask might have some bearing on the matter. I also notice that VFP Help is silent on this matter - its is just a thing that users are expected to know!.

But again, thank you.
 
Andrew,

I agree that it is often preferable to show "empty" numeric fields as blanks rather than zero. The solution is to set the control's Format property to Z. In fact, I do that in my base numeric field class.

You also said:

Perhaps the language could have been designed so that the textbox.value itself had a property of its data type which you can set without having to have an actual value of zero.

That would be inconsistent with the way VFP works. Keep in mind that textbox.value is really just a variable. As far as data typing is concerned, it behaves just like any other variable. You can assign any value to it, and the variable takes on the data type of that value. That's how it's always worked, and textbox.value is no different.

By the way, if the textbox is bound to a field in a table (via its ControlSource), then the data type will always be that of the underlying field.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
>ControlSource

Yes, that's the other thing you can use, I also explained that. I left it out this time, as it wasn't the accepted solution. It takes a bit more work, but you can define an object with properties to bind to several controls and then can pass on the object, which I often do in otherwise unbound forms, eg forms, where users may enter data filter values.

Code:
oFilter = CreateObject("empty")
Addproperty(oFiler,"nNumber",CAST(.NULL. As Int))
Addproperty(...) && other properties to bind other controls
* finally store oFilter as a form property, to keep it "alive"
Thisform.Addproperty("oFilter",oFilter)
Thisform.txtmynumber.controlsource = "Thisform.oFilter.nNumber"

What displays then is also blank, if you SET NULLDISPLAY TO ""

Overall this work the same as a nullable integer field. You can define any type with CAST.
Later you can work with Thisform.oFilter properties in queries etc. instead of thisform.somcontrol.value or even thisform.pageframe1.pag3.grid1.colum4.text1.value.

Caution: If you define a property as int this way it's still float. Also, if you define a property as C(20) via cast, you still can store Space(30). As Mike said, VFP isn't strong typed. But what works is, you have a property with no value (.NULL. denotes that), but still having a type (N,C,D,T,... all the normal variable types are possible). And though you still can set oFilter.nNumber = "abc" programmatically. If you keep it at a "numerically typed .NULL.", the textbox is blank and will detect the type of the controlsource as N and only allow numbers. That's the more advanced solution than Format "Z", as that allows blank controls for any type, of course.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top