Wrap text in ListBox

Hello there,

I am trying to display SQL scripts from a CSV file in a listbox, which, so far I have been able to do.  Problem now is, the text is not wrapping.  I am trying to get each new record on a new line, however, it is on one continuous line right now.  Here is my code:

procedure CSVconvert(FileName : String);
var
Row, Col: Integer;
MyFile, MyCol:   TStringList;

begin
 
  MyFile := TStringList.Create;
  MyCol := TStringList.Create;
  MyCol.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);  
    If MyFile.Count > 0 then
    Begin
      MyCol.DelimitedText := MyFile[Row];

      For Col := 0 to MyCol.Count -1 Do  
        MyCol[Col] := MyCol[Col] + ' varchar(50)';

      MyFile[Row] := 'CREATE TABLE TABLE1 (ID INT(2), ' + MyCol.DelimitedText +  ', PRIMARY KEY(ID));';
    end;
    For Row := 1 to MyFile.Count-1 Do
    Begin

      Form1.ListBox1.Items.Add(MyFile[row]);

      MyCol.CommaText := MyFile[Row];
      For Col := 0 to MyCol.Count -1 Do
      begin
        MyCol[Col] := MyCol[Col];
      end;
        MyFile[Row] := 'INSERT INTO TABLE1 VALUES (' + IntToStr(Row) + ',' + MyCol.DelimitedText + ');';

   end;

    begin
    Form1.ListBox2.Items.Add(MyFile.Text); // this is the display box, which is currently only showing one line of text for all records.
    end;

    Finally
    showmessage('You have successfully created the following SQL Scritps: '+MyFile.Text); // when I display them here they are fine, one record per line.
    MyFile.Free;
    MyCol.Free;
  end;
end;
end.

thanks, once again your assistance is greatly appreciated.
south_pawAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
geobulConnect With a Mentor Commented:
Hi,

You may replace the line:

Form1.ListBox2.Items.Add(MyFile.Text); // this is the display box, which is currently only showing one line of text for all records.

with either:

Form1.ListBox2.Items.Text := MyFile.Text; // previous content of the list box will be cleared

or

for i := 0 to MyFile.Count - 1 do Form1.ListBox2.Items.Add(MyFile[i]);

Regards, Geo
0
 
kretzschmarCommented:
you may use a tmemo/Tstringgrid instead or
a thirdparty multiline listbox
or try to paint your items by self
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
or also with

Form1.ListBox2.Items.Assign(MyFile);

F68 ;-)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kretzschmarCommented:
geo, f68,

was the question not, how to wrap the text of an item?

like itemtext

|- Listbox View Area-|
"Very long Text for a Listbox Item"
....other items


should become

|- Listbox View Area-|
"Very long Text for
a Listbox Item"
....other items


or am i on a wrong track?

meikl ;-)
0
 
kretzschmarCommented:
well, reread the question, and guess i have tomatos on my eyes ;-))

this
>Form1.ListBox2.Items.Add(MyFile.Text); // this is the display box, which is >currently only showing one line of text for all records.

should be
Form1.ListBox2.Items.Text := MyFile.Text;
   
meikl ;-)
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
meikl take care on this:

Form1.ListBox2.Items.Add(MyFile.Text); // this is the display box, which is currently only showing one line of text for all records.
showmessage('You have successfully created the following SQL Scritps: '+MyFile.Text); // when I display them here they are fine, one record per line.

It already grabs correctly what he need...
He's simply adding to the ListBox as 1 line text instead of a multilines (as geo have right discovered)
0
 
kretzschmarCommented:
yep, f68, got it now :-))
0
 
vadim_tiCommented:
hi, attached unit and form source.
i hope this help
if you have any questions you are welcome


Unit source
-------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    Memo1: TMemo;
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  Flags: Longint;
  Data: String;
begin
  with Control as TListBox do begin
    Canvas.FillRect(Rect);
    if Index < Count then begin
      Flags := DT_NOPREFIX or DT_WORDBREAK;//or DT_CALCRECT or DT_WORDBREAK;
      Flags := DrawTextBiDiModeFlags(Flags);
                                     //DT_VCENTER or
      if not UseRightToLeftAlignment then
        Inc(Rect.Left, 2)
      else
        Dec(Rect.Right, 2);
      Data := Items[Index];
      DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
    end
  end
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  n:Integer;
  l: TListBox;
begin
  n := 0;
  l := Control as TListBox;
  while l.Canvas.TextWidth(l.Items[Index]) > l.width * n do
    Inc(n);
  height := l.Canvas.TextHeight('A') * n +2;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  listbox1.Items.Add(edit1.text)
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  listbox1.Items.Add(memo1.text)
end;

end.

form source
--------------
object Form1: TForm1
  Left = 192
  Top = 133
  Width = 544
  Height = 375
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 48
    Top = 32
    Width = 233
    Height = 249
    Style = lbOwnerDrawVariable
    ItemHeight = 16
    TabOrder = 0
    OnDrawItem = ListBox1DrawItem
    OnMeasureItem = ListBox1MeasureItem
  end
  object Button1: TButton
    Left = 328
    Top = 96
    Width = 97
    Height = 25
    Caption = 'Add Short Line'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 312
    Top = 64
    Width = 121
    Height = 21
    TabOrder = 2
    Text = 'Edit1'
  end
  object Button2: TButton
    Left = 328
    Top = 240
    Width = 97
    Height = 25
    Caption = 'Add Long Line'
    TabOrder = 3
    OnClick = Button2Click
  end
  object Memo1: TMemo
    Left = 312
    Top = 136
    Width = 129
    Height = 89
    Lines.Strings = (
     
        'I am trying to display SQL scripts from a CSV file in a listbox,' +
        ' which, so far I have been able to do. Problem now is, the text ' +
        'is not wrapping. I am trying to get each new record on a new lin' +
        'e, however, it is on one continuous line right now')
    TabOrder = 4
    WordWrap = False
  end
end
0
 
south_pawAuthor Commented:
Thanks Geobul,

"Form1.ListBox2.Items.Text := MyFile.Text;" did exactly what I was after.  
0
 
geobulCommented:
The pleasure was mine :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.