开发者

how to wordwrap the button text?

开发者 https://www.devze.com 2023-01-04 13:13 出处:网络
i have created dynamic button with code below, the button caption is too long so i have to change the size of the caption to fit the button width but the wordwrap seen t开发者_JS百科o be not function

i have created dynamic button with code below, the button caption is too long so i have to change the size of the caption to fit the button width but the wordwrap seen t开发者_JS百科o be not function at all.

var
  Reg: TRegistry;
  lstKey, lstSubKey : TStringList;
  sKeyName, sSubKeyName : string;
  i, j, iSize, iSize2, iTop, iSpace, iComp : integer;    
begin
  lstKey     := TStringList.Create;
  lstSubKey  := TStringList.Create;
  lstBtnName := TStringList.Create;
  Reg        := TRegIniFile.Create;
try
  Reg.OpenKeyReadOnly('registryPath'); 
  Reg.GetKeyNames(lstSubKey);    // get registry key
  Reg.CloseKey;  

  iSize := 5;
  iSize2 := 5;
  iTop := 5;
  iSpace := 5;

  if ScrollBox1.ControlCount > 0 then begin
     for j := ScrollBox1.ControlCount - 1 downto 0 do begin
        with ScrollBox1.Controls[j] AS TBitBtn do begin
          Free;
        end;
     end;
  end;

  for i := 0 to lstSubKey.Count - 1 do begin
      with TBitBtn.Create(self) do begin    // create dynamic buttons
        Parent := ScrollBox1;
        Height := 50;
        Width  := 50;

        if iSize > ((Width + iSpace) * 3) then begin  //2nd row, 3 btns in 1 row 
           Left := iSize2;
           iSize2 := iSize2 + Width + iSpace;
           Top := iTop + Height + iSpace;
        end else begin    //1st row
           Left := iSize;
           iSize := iSize + Width + iSpace;
           Top := iTop;
        end;
        Caption := lstSubKey.Strings[i];
        WordWrap := TRUE;
      end;
  end;
  finally
  lstKey.Free;
  lstSubKey.Free;
  Reg.Free;
  end;
end;


Using #13 in caption can split the caption string to next row. eg. Caption := 'Stock ID : Bread ' + #13 + 'Price : RM1.00';


Works for me with a simple example of three lstSubKey entries:

  • 'Short'
  • 'Medium Length'
  • 'Longer'

However, if I remove the space between "Medium" and "Length", and make the 2nd item:

  • 'MediumLength'

Then it does not wrap, but this is to be expected because there is no word break in the string on which the string can be broken in order to be wrapped.

0

精彩评论

暂无评论...
验证码 换一张
取 消