Fill Stringgrid row with variable of N characters

Asked

Viewed 515 times

0

Fill Stringgrid row with variable of N characters

Greetings to all

I am using this code only to play the letters without values in stringgrid


procedure TForm1.Button1Click(Sender: TObject);
var
  t: TStringList;
  i:integer;
begin
 t := TStringList.create; // cria uma classe
 t.text := stringReplace(Edit1.Text, '|', #13,[rfReplaceAll]);//substitui | por quebra de linha
  for i := 0 to Pred(t.Count) do
      StringGrid1.cells[i+1,2]:=t[i]; // joga todos elementos do edit na stringgrid 
                                      //i+1 para prencher a partir da coluna 2
  t.free; //  destruir a classe
end;

it would be possible to play an unknown variable number of characters in only one Stringgrid row, knowing that each character has a value, which corresponds to the amount of columns it will occupy, using Tstringlist?

xdyz the variable string is composed N characters
1342 ( each character of the string has a value and each of them corresponds to a number of columns )

for:

character x = 1 cells [ x ]

character d = 3 cells [ d ] [ d ][ d ]

y character = 4 cells [ y] [ y ][ y ][ y ]

z character = 2 cells [ z] [ z ]

............

final result on a single Stringgrid line for this 4-character variable

[ x ][ d ][ d ][ d ][ y] [ y ][ y ][ y ][ z] [ z ] .........

  • The most simple would create a matriz of 2 columns, storing the character and its value, and to fill the StringGrid, go through the string and check each character in the matrix, after finding the character, fill in the StringGrid x times depending on the value found, and pass to next.

  • hello Pedro Roweder, I could give an example, because I need to play the information to stay in a single stringgrid line

1 answer

0

I put something together for you to come up with, I hope it helps.

const
  Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  Numeros = '123456789';
var
  oSLDados, oSLGrid: TStringList;
  iContValor, iContSort, nValor, iSort, iIncGrid : Integer;
  sCaracteres: String;
begin      
  nValor   := StrToIntDef(VALOR DIGITADO NO EDIT,0);
  oSLDados := TStringList.Create;
  oSLGrid  := TStringList.Create;
  try
    //PERCORRER O N VALOR PARA SORTEAR OS NUMEROS
    for iContValor := 0 to nValor-1 do
    begin
      sCaracteres := '';
      iSort       := StrToIntDef(Numeros[Random(Length(Numeros))],0);
      for iContSort := 0 to iSort-1 do
      begin
        if iContSort <> iSort-1 then
          sCaracteres := sCaracteres+Trim(Letras[Random(Length(Letras))])+';'
        else
          sCaracteres := sCaracteres+Trim(Letras[Random(Length(Letras))]);
      end;
      oSLDados.Add(sCaracteres);
    end;

    //PERCORRE STRINGLIST E PREENCHE A STRINGGRID
    iIncGrid := 1;
    for iContValor := 0 to oSLDados.Count-1 do
    begin
      oSLGrid.AddStrings(oSLDados);
      oSLGrid.Delimiter       := ';';
      oSLGrid.StrictDelimiter := True;
      oSLGrid.DelimitedText   := oSLDados.Strings[iContValor];

      for iContSort := 0 to oSLGrid.Count-1 do
      begin
        StringGrid1.Cells[iIncGrid,0] := oSLGrid.Strings[iContSort];
        Inc(iIncGrid);
      end;
    end;
  finally
    FreeAndNil(oSLDados);
    FreeAndNil(oSLGrid);
  end;
end;
  • Jefferson Rudolf that nice thank you very much, I’m using the 7 He does not recognize this line of instruction (Strictdelimiter) from the line oSLGrid.Strictdelimiter := True; ai the program stops there and does not copy in the 7 and does not execute

Browser other questions tagged

You are not signed in. Login or sign up in order to post.