Problem with replacing a part of a string lazarus / 21-General ---------------------------------------------------------------------- Clemensch · 2021-02-28 15:06 Hello everybody I have a problem with my program. I want to create a program which uses many SpinEdit-components. I want to display the result of those SpinEdit-components in one Label. During the click of SpinEdit it saves what the string from the Label.caption and then adds something alongside it. During that there's a problem. When I click the SpinEdit1 for example 5 times, instead of displaying "H5" it displays "H5H4H3H2H" or if I press the SpinEdit1 2 times and the SpinEdit2 3 times, instead of displaying "H2He3" it displays "H2HHe3He2He". is there a workaround to get me this result? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm2.SpinEdit1Change(Sender: TObject);var Merke: string;begin  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.capt ion:='H'+IntToStr(SpinEdit1.Value)+Merke; Label1.caption:=Stringreplac e(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]); end; Merke:=Label1.caption;end; procedure TForm2.SpinEdit2Change(Sender: TObject);var Merke: string;begin     if SpinEdit2.value> 1   then Label1.caption:='He'+IntToStr(SpinEdit2.Value)+Merke  else if SpinEdit2.value=1  then  begin Label1.caption:='He'+IntToStr(SpinEdit2.Value)+Merke;  Label1.caption:=StringReplace(Label1.caption,'He1','He',[rfReplaceAll, rfIgnoreCase]) ;  end; if SpinEdit2.value<=0  then  begin  Label1.capt ion:='He'+IntToStr(SpinEdit2.Value)+Merke; Label1.caption:=Stringrepla ce(Label1.caption,'He','', [rfReplaceAll, rfIgnoreCase]); end; Merke:=Label1.caption;end; end. kind regards Clemens ---------------------------------------------------------------------- Clemensch · 2021-02-28 16:33 What exactly does that mean? I'm relatively new to Lazarus and programming in general ---------------------------------------------------------------------- lucamar · 2021-02-28 16:58 A quick and not really thought-out suggestion (I haven't delved into the logic of your code ) ... Instead of:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var Merke: string; use:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const Merke: string = ''; ---------------------------------------------------------------------- Clemensch · 2021-02-28 17:31 Quote from: jamie on February 28, 2021, 05:38:43 pmyou 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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.. okay I understand. I thought that by adding that string at the end it remembers what was there and then uses it the next time the button is pressed. Do you have an idea as to how to fix my issue? ---------------------------------------------------------------------- Clemensch · 2021-02-28 17:40 Quote from: lucamar on February 28, 2021, 05:58:22 pmA quick and not really thought-out suggestion (I haven't delved into the logic of your code ) ... Instead of:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var Merke: string; use:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const Merke: string = ''; Just a curious question. How does that help the program? ---------------------------------------------------------------------- bytebites · 2021-02-28 17:45 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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.capt ion:='H'+IntToStr(SpinEdit1.Value)+Merke; Label1.caption:=Stringreplac e(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]); end;end; ---------------------------------------------------------------------- Clemensch · 2021-02-28 17:53 Quote from: bytebites on February 28, 2021, 06:45:54 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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.capt ion:='H'+IntToStr(SpinEdit1.Value)+Merke; Label1.caption:=Stringreplac e(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]); end;end; thank you! the same issue of instead of displaying "H5" it shows "H5H4H3H2H" after pressing the SpinEdit 5 times ---------------------------------------------------------------------- engkin · 2021-02-28 18:15 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TForm2 = class(TForm)...public  Merke1, Merke2: string;end; Since both SpinEdit1Change and SpinEdit2Change do the same thing, have them call one common procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; ---------------------------------------------------------------------- Clemensch · 2021-02-28 18:30 Quote from: engkin on February 28, 2021, 07:15:44 pmYou 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TForm2 = class(TForm)...public  Merke1, Merke2: string;end; Since both SpinEdit1Change and SpinEdit2Change do the same thing, have them call one common procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; thank you! I'll try that out! By the logic of the last algorithm I could theoretically add more SpinEdits. Is that correct? ---------------------------------------------------------------------- engkin · 2021-02-28 18:48 Quote from: Clemensch on February 28, 2021, 07:30:20 pmBy 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. ---------------------------------------------------------------------- egsuh · 2021-03-01 04:26 I think you may use function like following.  You can add as many spinedits as you wish, and assgin following procedure to OnChange event of all spinedits.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm2.SpinEdit1Change(Sender: TObject);     funciton SpinEditText (SE:TSpinEdit; prefix: string) : string;     begin          if SE.Value <=0 then Result:= ''         else if SE.Value = 1 then Result:= prefix         else Result:= prefix + IntToStr(SE.Value);   end;  begin      Label1.Caption:= SpinEditText(SpinEdit1, 'H') + SpinEditText(SpinEdit2, 'He'); end;