[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Wrap text in ListBox

Posted on 2004-09-22
10
Medium Priority
?
935 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 2000 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 23

Expert Comment

by:Ferruccio Accalai
ID: 12131304
or also with

Form1.ListBox2.Items.Assign(MyFile);

F68 ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 23

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 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