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

Spin Edit

Status
Not open for further replies.

siu01jam

Programmer
Oct 2, 2003
12
GB
Hopefully this should be quite an easy one . . .

I think all I need is to get the cursor to go to the end of a spinedit box . .

I have a line of code that works out a new width of an image if the height is changed and vice versa.

The problem is that when I select everything in one of the spinedits then press a number to replace it all, first, it has nothing in there so calls the onchange event where I catch an StrToInt exception. This also puts a 0 into the spinedit box. The cursor then goes back to the beginning of the box (ie. before the 0). Then the number that was pressed is inserted into the box.

This means that if I highlight everything and press 1, I get 10 in the spinedit which is really annoying!

procedure TResize.SpinEdit1Change(Sender: TObject);
begin
if CheckBox3.Checked then
try
SpinEdit2.Value:= Round(SpinEdit1.Value * (RealImage.picture.Height/RealImage.picture.Width))
except on EConvertError do begin end;
end;
end;

procedure TResize.SpinEdit2Change(Sender: TObject);
begin
if CheckBox3.Checked then
try
SpinEdit1.Value:= Round(SpinEdit2.Value * (RealImage.picture.Width/RealImage.picture.Height))
except on EConvertError do begin end;
end;
end;
 
The problem is that when you change the value of SpinEdit2 from SpinEdit1 change event handler, then you automatically trigger SpinEdit2 change event handler and goes on. One way to avoid this is to deactivate SpinEdit2_ChangeEvent, calculate the value of SpinEdit2.Value and the reassign SpinEdit2 ChangeEvent. The same you should do for SpinEdit1.

Another way is to let your program know when you change SpinEdit values programmatically and not by pushing the buttons of SpinEdit. I mean that before you do SpinEdit2.Value = ...
you write
value_changed_flag = true
(value_changed_flag is a boolean variable that you define in order to let the program know that the value of SpinEdit2 has changed programmatically).
Then in your SpinEdit2 ChangeEvent handler you write the following:
if value_changed_flag = true then
begin
value_changed_flag = false;
exit;
end;
With this you tell to the event handler of SpinEdit2 (that was just triggered) that it should not change anything and it should exit. It also brings back value_changed_flag to its initial state.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top