Solved

Wrap text in ListBox

Posted on 2004-09-22
10
892 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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