Separate Letters from a String and Add in a ComboBox

3

Searching the web I found this function to return the letters of the HDDs.

function tbDriveLetters: string;
{ Uso: S := tbDriveLetters; - retorna 'ACD' se existir as unidades A:, C: e D: }
var
  Drives: LongWord;
  I: byte;
begin
  Result := '';
  Drives := GetLogicalDrives;

  if Drives <> 0 then
    for I := 65 to 90 do
      if ((Drives shl (31 - (I - 65))) shr 31) = 1 then
        Result := Result + Char(I);
end;

I need another function to separate the letters and add in the ComboBox so that each letter is a Combobox item.

    
asked by anonymous 29.05.2017 / 22:59

1 answer

4

Instead of doing the process through a function, you can directly use ComboBox directly.

procedure tbDriveLetters;
{Uso: S := tbDriveLetters; - retorna 'ACD' se existir as unidades A:, C: e D:}
var
  Drives: LongWord;
  I: byte;
begin
  Drives := GetLogicalDrives;

  if Drives <> 0 then
    for I := 65 to 90 do
      if ((Drives shl (31 - (I - 65))) shr 31) = 1 then
        ComboBox.Itens.Add(Char(I));
end;

But if it really needs to be done by function, imagining it to be in different Units, you can modify the Return of its function that is String to a TComboBox.

That is, you can return an already populated TComboBox to the component that the client will use on the screen.

Construction:

function tbDriveLetters:TComboBox;

Food:

Result.Itens.Add(Char(I));

Attribution:

SeuComboBox := tbDriveLetters;
    
31.05.2017 / 13:35