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

Stop spin button from going below 0

Status
Not open for further replies.

purcion

Programmer
Feb 14, 2005
32
AU
I m hoping this is simple and possible I have a editbox
thats value is controlled by a spin button . How would I stop it from going into minus numbers ie -10 below 0.
here is the code i a using ...
{=======================================================}

procedure TForm1.SpinButton1DownClick(Sender: TObject);

var
Temp : Integer;
begin
Temp := StrToInt (Edit3.Text);

Temp := Temp - 10;

Edit3.Text := IntToStr (Temp)


end;

procedure TForm1.SpinButton1UpClick(Sender: TObject);
var
Temp : Integer;
begin
Temp := StrToInt (Edit3.Text);

Temp := Temp + 10;

Edit3.Text := IntToStr (Temp)
end;

{=========================================================}
Thanks
 
its ok i answered my own question

{=====================================}
procedure TForm1.SpinButton1DownClick(Sender: TObject);

var
Temp : Integer;
I:integer;
begin
Temp := StrToInt (Edit3.Text);
for I := Temp downto 0 do
Edit3.Text := IntToStr (Temp)

end;
{========================================}
 
I use Delphi 6 and prefer to use a TUpDown control (in the Win32 tab) in conjunction with a TEdit.
1) Drop both components on a form
2) Set the following TUpDown properties at design-time:
[tt]
Associate: Edit1 (or whatever the name of your control is)
Min: 0
Max: 100 (or whatever you want)
Increment: 10
[/tt]

The TUpDown then controls what appears in the TEdit.

Hope this is helpful!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I only have one gripe with Tupdown, when you associate it with an edit box it will snap to it on the form editor, fine, but if you decide to move it, and can't grab both parts, the bits break apart again and you have to re-align. (unless this has been fixed after D4).


Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
Yeah it's not ideal from that respect. However, if you move the TEdit to the desired position, then reselect your TEdit from the TUpDown's Associate property drop-down box, it will snap the TUpDown back next to the TEdit. But I accept your point, ideally it would operate like the TLabeledEdit component and the two would move as one component.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Clive:
So it does, have to say that I named my trial Tedit 'A1' so it was at the top of the list, if you have a lot of controls and lots of couplings to move even that would be a bit tedious, but it was only a minor grip anyway.



Steve:
A Delphi Programmer
 
How about a simple 'if' to check if the value is 0, and so so don't decrement it?
 
A simple 'if', fair enough but where will you put it, not in onchange anyway
e.g
This stops the error but then the control is locked.

procedure TAVGUpdate.spinChange(Sender: TObject);
begin
if spin.Value = 0 then spin.Enabled := false;
end;

You cant increment the value here when it gets to zero as you will never see zero in the control.
not so easy.


Steve:
A Delphi Programmer
 
It's a simple modification to the original code

Code:
procedure TForm1.SpinButton1DownClick(Sender: TObject);
var
 Temp : Integer;
begin
 Temp := StrToInt (Edit3.Text);

 if Temp >= 10 then dec(Temp, 10);

 Edit3.Text := IntToStr (Temp)
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top