south_paw
asked on
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(FileNa me);
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(M yFile[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(M yFile.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.
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(FileNa
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(M
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(M
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or also with
Form1.ListBox2.Items.Assig n(MyFile);
F68 ;-)
Form1.ListBox2.Items.Assig
F68 ;-)
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 ;-)
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 ;-)
well, reread the question, and guess i have tomatos on my eyes ;-))
this
>Form1.ListBox2.Items.Add( MyFile.Tex t); // 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 ;-)
this
>Form1.ListBox2.Items.Add(
should be
Form1.ListBox2.Items.Text := MyFile.Text;
meikl ;-)
meikl take care on this:
Form1.ListBox2.Items.Add(M yFile.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)
Form1.ListBox2.Items.Add(M
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)
yep, f68, got it now :-))
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(Contro l: 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(Co ntrol: 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(Flag s);
//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.t ext)
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
listbox1.Items.Add(memo1.t ext)
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
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(Contro
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(Co
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(Flag
//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
var Height: Integer);
var
n:Integer;
l: TListBox;
begin
n := 0;
l := Control as TListBox;
while l.Canvas.TextWidth(l.Items
Inc(n);
height := l.Canvas.TextHeight('A') * n +2;
end;
procedure TForm1.Button1Click(Sender
begin
listbox1.Items.Add(edit1.t
end;
procedure TForm1.Button2Click(Sender
begin
listbox1.Items.Add(memo1.t
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
ASKER
Thanks Geobul,
"Form1.ListBox2.Items.Text := MyFile.Text;" did exactly what I was after.
"Form1.ListBox2.Items.Text
The pleasure was mine :-)
a thirdparty multiline listbox
or try to paint your items by self