you are adding a string to the end which has nothing in it.
you have declared that string at the beginning but it has nothing in it so you are adding a blank string to the end of your other string combining.
Also its very possible but I can provide evidence at the moment cause I am looking at Delphi instead, its possible the string may control garbage or left overs from the last call.
Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke //<<<<<<<
You have not placed any data in that string that I can see of?
PS:
I just took another look.. it appears you think the string maintains content between calls , it does not..
A quick and not really thought-out suggestion (I haven't delved into the logic of your code :-[) ...
Instead of:
var Merke: string;
use:
const Merke: string = '';
procedure TForm2.SpinEdit1Change(Sender: TObject); var Merke: string; begin Merke:=Label1.caption; if SpinEdit1.value> 1 then Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke else if SpinEdit1.value=1 then begin Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke; Label1.caption:=StringReplace(Label1.caption,'H1','H',[rfReplaceAll, rfIgnoreCase]) ; end; if SpinEdit1.value<=0 then begin Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke; Label1.caption:=Stringreplace(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]); end; end;
You seem you want to add 'H' or 'He' and a number based on the SpinEdits values.
Preserve the values of Merke variables in both procedures SpinEdit1Change and SpinEdit2Change by adding them to the form as two different names Merke1 and Merke2:
TForm2 = class(TForm) ... public Merke1, Merke2: string; end;
Since both SpinEdit1Change and SpinEdit2Change do the same thing, have them call one common procedure:
procedure ProcessValue(var AMerke: String; AValue: integer; APrefix:String); begin if AValue<=0 then AMerke := '' else begin if AValue=1 then AMerke := APrefix else AMerke := APrefix + AValue.ToString; end; end;
This way if the logic changes, you only need to deal with one procedure.
procedure TForm2.SpinEdit1Change(Sender: TObject); begin ProcessValue(Merke1, SpinEdit1.Value, 'H'); Label1.Caption := Merke1+Merke2; end; procedure TForm2.SpinEdit2Change(Sender: TObject); begin ProcessValue(Merke2, SpinEdit2.Value, 'He'); Label1.Caption := Merke1+Merke2; end;
By the logic of the last algorithm I could theoretically add more SpinEdits. Is that correct?Yes, and as pointed out by jamie, there are other alternative ways. You could use arrays for Merke variables, and for prefixes. One OnChange event and identify the spin edit from its Tag.