Topic: how to calculate a number of spaces for each combobox elements

hello all

I manually centered ComboBox2 elements by adding a suitable space for each element .

in ComboBox1 i want to use for loop to add elements  but how can i calculate a number of spaces for each combobox elements



var Names : array[0..10] of string ;
   var i,j:Integer;

procedure Form1_OnShow_Event (Sender: TObject; Action: string ;DataSet: TDataSet); begin
    Names:=['all','john','alisa','makhmara','kiven','brad toki','salamoiya','lomia gone','steven baloka','irak fone','qutazami phoronix'];

     Form1.ComboBox2.Items.Add(Names[0]+'             ');  // 13 space
     Form1.ComboBox2.Items.Add(Names[1]+'            ');   // 12 space
     Form1.ComboBox2.Items.Add(Names[2]+'            ');   // 12 space
     Form1.ComboBox2.Items.Add(Names[3]+'        ');       // 8 space
     Form1.ComboBox2.Items.Add(Names[4]+'            ');    //12 space
     Form1.ComboBox2.Items.Add(Names[5]+'          ');     // 10 space
     Form1.ComboBox2.Items.Add(Names[6]+'         ');      // 9 space
     Form1.ComboBox2.Items.Add(Names[7]+'         ');       //9 space
     Form1.ComboBox2.Items.Add(Names[8]+'        ');       // 8 space
     Form1.ComboBox2.Items.Add(Names[9]+'           ');     //11 space
     Form1.ComboBox2.Items.Add(Names[10]+'     ');         // 5 space
     Form1.ComboBox2.ItemIndex:=0 ;

     for i := 0 to Length(Names) -1 do begin

          Names[i]:= Names[i]+'             ' ;     // how to calculate a number of spaces for each element
            Form1.ComboBox1.Items.Add(Names[i]);
      end;
       Form1.ComboBox1.ItemIndex:=0 ;
    end;


any idea or solve this problem

thanks for help

Post's attachments

Attachment icon combobox_spaces.rar 293.7 kb, 238 downloads since 2020-12-23 

2 (edited by kofa 2020-12-23 20:09:55)

Re: how to calculate a number of spaces for each combobox elements

hello all

According to my understanding of mathematics, the mathematical equation is as follows:

space  =   (  ComboBox.width / 2  )   -   ( element.Length / 2  )

i   =  i + space 


i tired it

3 (edited by brian.zaballa 2020-12-23 20:34:23)

Re: how to calculate a number of spaces for each combobox elements

something like this? Sorry, I'm not so sure on where you are going to use the count of spaces so I just put it in the combobox to showcase

Post's attachments

Attachment icon combobox_spaces2.zip 495.64 kb, 241 downloads since 2020-12-23 

brian

Re: how to calculate a number of spaces for each combobox elements

hello brian.zaballa

i want to center a combobox elements  not need to add spaces number

it means how can i set combobox alignment = center

thanks for help

Re: how to calculate a number of spaces for each combobox elements

Here's a close solution to the problem.

var Names : array[0..10] of string;
    i,j,longest:Integer;

procedure Form1_OnShow_Event (Sender: TObject; Action: string ;DataSet: TDataSet); begin
     Names:=['all','john','alisa','makhmara','kiven','brad toki','salamoiya','lomia gone','steven baloka','irak fone','qutazami phoronix'];

     // get length of longest string
     longest := Length(Names[0]);
     for i:=1 to Length(Names)-1 do
        if Length(Names[i]) > longest then longest := Length(Names[i]);

     Form1.ComboBox2.Items.Add(Names[0]+'             ');  // 13 space
     Form1.ComboBox2.Items.Add(Names[1]+'          ');   // 12 space
     Form1.ComboBox2.Items.Add(Names[2]+'            ');   // 12 space  
     Form1.ComboBox2.Items.Add(Names[3]+'        ');       // 8 space
     Form1.ComboBox2.Items.Add(Names[4]+'            ');    //12 space
     Form1.ComboBox2.Items.Add(Names[5]+'          ');     // 10 space
     Form1.ComboBox2.Items.Add(Names[6]+'         ');      // 9 space
     Form1.ComboBox2.Items.Add(Names[7]+'         ');       //9 space
     Form1.ComboBox2.Items.Add(Names[8]+'        ');       // 8 space
     Form1.ComboBox2.Items.Add(Names[9]+'           ');     //11 space
     Form1.ComboBox2.Items.Add(Names[10]+'     ');         // 5 space
     Form1.ComboBox2.ItemIndex:=0 ;

     for i := 0 to Length(Names) -1 do begin
           // Names[i]:= ;     // how to calculate a number of spaces for each element
           if  Length(Names[i]) = longest then
               Form1.ComboBox1.Items.Add(AddSpaces(Names[i], longest-Length(Names[i])+1))
           else if (Length(Names[i]) mod 2) = 1 then
               Form1.ComboBox1.Items.Add(AddSpaces(Names[i], longest-Length(Names[i])))
           else
              Form1.ComboBox1.Items.Add(AddSpaces(Names[i], longest-Length(Names[i])-1));

     end;
     Form1.ComboBox1.ItemIndex:=0 ;
end;

function AddSpaces(str: String; count:Integer=5):String ;
var
    i: Integer;
begin
    result := str;
    for i:=0 to count do result := result +' ';
end;

{
//https://stackoverflow.com/questions/15294501/how-to-count-number-of-occurrences-of-a-certain-char-in-string
}
function OccurrencesOfChar(const S: string; const C: char=' '): integer;
var
  i: Integer;
begin
  result := 0;
  for i := 1 to Length(S) do
    if S[i] = C then
      inc(result);
end;


begin
end.
brian

6 (edited by kofa 2020-12-24 07:35:19)

Re: how to calculate a number of spaces for each combobox elements

hello brian.zaballa

thank you very much 

can you help me to change the (18) to suitable number for each element to more control a center position


for i := 0 to Length(Names) -1 do begin
        
          Names[i]:= Format('%-18s', [Names[i]]);
            Form1.ComboBox1.Items.Add(Names[i]);
      end;
       Form1.ComboBox1.ItemIndex:=0 ;

thanks for help