Recent

Author Topic: Problem with replacing a part of a string  (Read 734 times)

Clemensch

  • New member
  • *
  • Posts: 9
Problem with replacing a part of a string
« on: February 28, 2021, 04:06:31 pm »
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][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2. var Merke: string;
  3. begin
  4.   if SpinEdit1.value> 1
  5.    then Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke
  6.   else
  7.  if SpinEdit1.value=1
  8.   then  begin Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  9.   Label1.caption:=StringReplace(Label1.caption,'H1','H',[rfReplaceAll, rfIgnoreCase]) ;
  10.   end;
  11.  if SpinEdit1.value<=0
  12.   then
  13.   begin
  14.   Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  15.  Label1.caption:=Stringreplace(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]);
  16.  end;
  17.  Merke:=Label1.caption;
  18. end;
  19.  
  20. procedure TForm2.SpinEdit2Change(Sender: TObject);
  21. var Merke: string;
  22. begin
  23.      if SpinEdit2.value> 1
  24.    then Label1.caption:='He'+IntToStr(SpinEdit2.Value)+Merke
  25.   else
  26.  if SpinEdit2.value=1
  27.   then  begin Label1.caption:='He'+IntToStr(SpinEdit2.Value)+Merke;
  28.   Label1.caption:=StringReplace(Label1.caption,'He1','He',[rfReplaceAll, rfIgnoreCase]) ;
  29.   end;
  30.  if SpinEdit2.value<=0
  31.   then
  32.   begin
  33.   Label1.caption:='He'+IntToStr(SpinEdit2.Value)+Merke;
  34.  Label1.caption:=Stringreplace(Label1.caption,'He','', [rfReplaceAll, rfIgnoreCase]);
  35.  end;
  36.  Merke:=Label1.caption;
  37. end;
  38.  
  39. end.  

kind regards
Clemens

jamie

  • Hero Member
  • *****
  • Posts: 4366
Re: Problem with replacing a part of a string
« Reply #1 on: February 28, 2021, 05:04:02 pm »
At a quick glance..

You are adding an non initialized string to the mix of "Merke"

that is just first look.
The only true wisdom is knowing you know nothing

Clemensch

  • New member
  • *
  • Posts: 9
Re: Problem with replacing a part of a string
« Reply #2 on: February 28, 2021, 05:33:14 pm »
What exactly does that mean? I'm relatively new to Lazarus and programming in general :)

jamie

  • Hero Member
  • *****
  • Posts: 4366
Re: Problem with replacing a part of a string
« Reply #3 on: February 28, 2021, 05:38:43 pm »
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.

Code: Pascal  [Select][+][-]
  1. Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke  //<<<<<<<
  2.  

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..
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 3779
Re: Problem with replacing a part of a string
« Reply #4 on: February 28, 2021, 05:58:22 pm »
A quick and not really thought-out suggestion (I haven't delved into the logic of your code :-[) ...

Instead of:
Code: Pascal  [Select][+][-]
  1. var Merke: string;

use:
Code: Pascal  [Select][+][-]
  1. const Merke: string = '';
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Clemensch

  • New member
  • *
  • Posts: 9
Re: Problem with replacing a part of a string
« Reply #5 on: February 28, 2021, 06:31:33 pm »
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.

Code: Pascal  [Select][+][-]
  1. Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke  //<<<<<<<
  2.  

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

  • New member
  • *
  • Posts: 9
Re: Problem with replacing a part of a string
« Reply #6 on: February 28, 2021, 06:40:44 pm »
A quick and not really thought-out suggestion (I haven't delved into the logic of your code :-[) ...

Instead of:
Code: Pascal  [Select][+][-]
  1. var Merke: string;

use:
Code: Pascal  [Select][+][-]
  1. const Merke: string = '';

Just a curious question. How does that help the program? :D

bytebites

  • Sr. Member
  • ****
  • Posts: 393
Re: Problem with replacing a part of a string
« Reply #7 on: February 28, 2021, 06:45:54 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2. var Merke: string;
  3. begin
  4.   Merke:=Label1.caption;
  5.   if SpinEdit1.value> 1
  6.    then Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke
  7.   else
  8.  if SpinEdit1.value=1
  9.   then  begin Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  10.   Label1.caption:=StringReplace(Label1.caption,'H1','H',[rfReplaceAll, rfIgnoreCase]) ;
  11.   end;
  12.  if SpinEdit1.value<=0
  13.   then
  14.   begin
  15.   Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  16.  Label1.caption:=Stringreplace(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]);
  17.  end;
  18. end;

Clemensch

  • New member
  • *
  • Posts: 9
Re: Problem with replacing a part of a string
« Reply #8 on: February 28, 2021, 06:53:37 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2. var Merke: string;
  3. begin
  4.   Merke:=Label1.caption;
  5.   if SpinEdit1.value> 1
  6.    then Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke
  7.   else
  8.  if SpinEdit1.value=1
  9.   then  begin Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  10.   Label1.caption:=StringReplace(Label1.caption,'H1','H',[rfReplaceAll, rfIgnoreCase]) ;
  11.   end;
  12.  if SpinEdit1.value<=0
  13.   then
  14.   begin
  15.   Label1.caption:='H'+IntToStr(SpinEdit1.Value)+Merke;
  16.  Label1.caption:=Stringreplace(Label1.caption,'H','', [rfReplaceAll, rfIgnoreCase]);
  17.  end;
  18. end;

thank you! the same issue of instead of displaying "H5" it shows "H5H4H3H2H" after pressing the SpinEdit 5 times

engkin

  • Hero Member
  • *****
  • Posts: 2662
Re: Problem with replacing a part of a string
« Reply #9 on: February 28, 2021, 07:15:44 pm »
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][+][-]
  1. TForm2 = class(TForm)
  2. ...
  3. public
  4.   Merke1, Merke2: string;
  5. end;

Since both SpinEdit1Change and SpinEdit2Change do the same thing, have them call one common procedure:
Code: Pascal  [Select][+][-]
  1. procedure ProcessValue(var AMerke: String; AValue: integer; APrefix:String);
  2. begin
  3.   if AValue<=0 then
  4.     AMerke := ''
  5.   else
  6.   begin
  7.     if AValue=1 then
  8.       AMerke := APrefix
  9.     else
  10.       AMerke := APrefix + AValue.ToString;
  11.   end;
  12. end;

This way if the logic changes, you only need to deal with one procedure.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2. begin
  3.   ProcessValue(Merke1, SpinEdit1.Value, 'H');
  4.  
  5.   Label1.Caption := Merke1+Merke2;
  6. end;
  7.  
  8. procedure TForm2.SpinEdit2Change(Sender: TObject);
  9. begin
  10.   ProcessValue(Merke2, SpinEdit2.Value, 'He');
  11.  
  12.   Label1.Caption := Merke1+Merke2;
  13. end;

Clemensch

  • New member
  • *
  • Posts: 9
Re: Problem with replacing a part of a string
« Reply #10 on: February 28, 2021, 07:30:20 pm »
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][+][-]
  1. TForm2 = class(TForm)
  2. ...
  3. public
  4.   Merke1, Merke2: string;
  5. end;

Since both SpinEdit1Change and SpinEdit2Change do the same thing, have them call one common procedure:
Code: Pascal  [Select][+][-]
  1. procedure ProcessValue(var AMerke: String; AValue: integer; APrefix:String);
  2. begin
  3.   if AValue<=0 then
  4.     AMerke := ''
  5.   else
  6.   begin
  7.     if AValue=1 then
  8.       AMerke := APrefix
  9.     else
  10.       AMerke := APrefix + AValue.ToString;
  11.   end;
  12. end;

This way if the logic changes, you only need to deal with one procedure.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2. begin
  3.   ProcessValue(Merke1, SpinEdit1.Value, 'H');
  4.  
  5.   Label1.Caption := Merke1+Merke2;
  6. end;
  7.  
  8. procedure TForm2.SpinEdit2Change(Sender: TObject);
  9. begin
  10.   ProcessValue(Merke2, SpinEdit2.Value, 'He');
  11.  
  12.   Label1.Caption := Merke1+Merke2;
  13. end;

thank you! I'll try that out! By the logic of the last algorithm I could theoretically add more SpinEdits. Is that correct?

jamie

  • Hero Member
  • *****
  • Posts: 4366
Re: Problem with replacing a part of a string
« Reply #11 on: February 28, 2021, 07:36:56 pm »
alternate way would be to use the same OnSpinEdit1(….) event for all of the spinners..

When you set the event in the object inspector you can scroll the down and select an existing event and it will go there.

if you need to some how identify the control coming  in with a simple check, the SpinEdits have a TAG property like all controls do where you can use this property to store any value there.

 So when the event gets triggered you can test this property of the control

 While in the event...

   With TSpinEdit(Sender) do
    begin
      Case TAG of
       ?:,,,,,,
       ?:…..
      end;
and so on
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 2662
Re: Problem with replacing a part of a string
« Reply #12 on: February 28, 2021, 07:48:56 pm »
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.

egsuh

  • Hero Member
  • *****
  • Posts: 686
Re: Problem with replacing a part of a string
« Reply #13 on: March 01, 2021, 05:26:51 am »
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][+][-]
  1. procedure TForm2.SpinEdit1Change(Sender: TObject);
  2.  
  3.     funciton SpinEditText (SE:TSpinEdit; prefix: string) : string;
  4.     begin
  5.          if SE.Value <=0 then Result:= ''
  6.          else if SE.Value = 1 then Result:= prefix
  7.          else Result:= prefix + IntToStr(SE.Value);
  8.     end;
  9.  
  10. begin
  11.      Label1.Caption:= SpinEditText(SpinEdit1, 'H') + SpinEditText(SpinEdit2, 'He');
  12. end;
     

 

TinyPortal © 2005-2018