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

SpinEdit drives me crazy

Status
Not open for further replies.

wilsona

Technical User
Jan 26, 2001
131
GB
Hi,
I want to use a SpinEdit control, but the defauilt behaviour is driving me mad! I want the user to be able to type a value, in addition to using the up/down buttons. I want the OnChange handler to process the value entered. The buttons work OK, but as soon as the user types a value an exception is raised (presumably when the spin edit box is cleared) - "ECOnvertError '' is not a valid integer value". This happens in the OnChange handler when an attempt is made to access the value in the SpinEdit. Can anyone suggest a way to fix this? (Delphi 4).

N.B. I have tried the following code, which does NOT work:

procedure TForm.SpinChange(Sender: TObject);
var Value : Integer;
begin
try
Value:= Spin.Value;
except
on EConvertError do Value := 0;
end;
Process(Value);
end;


Please Help!!

 
Hi wilsona

I am using Delphi 5 and have no problems with clearing the value in TSpinEdit. However, I think that your problem may be related to something else.

OnChange is called every time a change happens to the Value or Text property. If you change either property from inside the OnChange handler then it will call itself until you get a stack overflow or some other exception.

To protect your code against this, you have to declare a unit scope boolean which you can call something like InChangeEvent. Your handler will look something like this.

(InChangeEvent's default value is always false)

procedure TForm.SpinChange(Sender:TObject);
begin
If InChangeEvent then
exit;
InChangeEvent:=true;
(...your other code goes here)

InChangeEvent:=false;
end;


If you look at the Borlands library modules, you will see plenty of cases where Borland does the same thing.


Regards
Roger



 
The SpinEdit is a fairly cheap hack on a TEdit and a TSpinButton, basically to show you what's possible. Of course, being such a useful beastie, everyone uses it; and like me, you've run into its little oddity.

The SpinEdit's Value property is just StrToInt(Text). When the underlying TEdit's Text property is not a valid integer, it spits.

When the user highlights the eniter existing text in the SpinEdit, and then types a new value, Windows first deletes the old text, then adds the first character of the new. So momentarily the SpinEdit's Text is '', and when it StrToInt()'s that, Delphi raises the exception. Naughty naughty Borland: should have used StrToIntDef().

Well; this gives you an explanation; unfortunately, I can't give you a solution. Perhaps edit the VCL source code and change the IntToStr() to IntToStrDef()? Perhaps create your own SpinEdit control without this bug, or download a third party one from somewhere like Torry's? -- Doug Burbidge mailto:doug@ultrazone.com
 
Hi Wilsona -

Maybe as Ronin441 says, try an alternative. You can download some excellent freeware components at ..


That has a spin-edit replacement that should not give you any problems.

Also, the Raize list pack is available for Delphi 5 and is free once you register (not sure if its got a spin-edit in it Im afraid - but worth having anyway.)


Opp.
 
Or use the UpDown component on the W32 pallet, I know this is a little more clumbersome to set up but you will have more control over blank box error thing.

A Warning The worst thing about an Updown and an edit box is if you want to reposition it after doing the edit box alloction. They will stay attached programmaticaly but they dont stay physicaly attached and you will have to move both bits seperatly.
Steve.
 
If I change the VCL code, how do I recompile it?

I'll probably put an exception handler around the StrToInt(Text), as no default value is applicable.
 
I gave up and just used StrToIntDef using the minimum value as the default.

i.e. Something like:

try
Value:=StrToInDef(Text,FMinValue)
except
Value:=FMinValue;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top