Solved

Wrap text in ListBox

Posted on 2004-09-22
10
839 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:south_paw
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12130355
you may use a tmemo/Tstringgrid instead or
a thirdparty multiline listbox
or try to paint your items by self
0
 
LVL 17

Accepted Solution

by:
geobul earned 500 total points
ID: 12130397
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12131304
or also with

Form1.ListBox2.Items.Assign(MyFile);

F68 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12131343
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12131366
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12131385
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12131415
yep, f68, got it now :-))
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12133772
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
 

Author Comment

by:south_paw
ID: 12134797
Thanks Geobul,

"Form1.ListBox2.Items.Text := MyFile.Text;" did exactly what I was after.  
0
 
LVL 17

Expert Comment

by:geobul
ID: 12135356
The pleasure was mine :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now