Solved

An easy 500 point ListBox question...

Posted on 2004-09-19
11
216 Views
Last Modified: 2012-05-05
This should be relatively straight forward, however, it has me stumped.  I have been through all my material and I cannot find a straight forward answer to this.

I am trying to display the contents of a CSV file in a list box.  I can read the file into an array and use showmessage to display the contents, now I just can't figure out how to display the same info in a listbox (to be slightly more specific, I am displaying SQL scripts derived from the CSV file in the LisTBox, showmessage at the moment displays these scripts). My code is as follows:


Procedure CSVimport(FileName: String);
Var
  MyFile, MyColumns: TStringList;
  Row, Col: integer;
begin
  MyFile := TStringList.Create;
  MyColumns := TStringList.Create;
  MyColumns.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);
    If MyFile.Count > 0 then
    Begin
      MyColumns.DelimitedText := MyFile[Row];

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

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

      MyColumns.CommaText := MyFile[Row];

      For Col := 0 to MyColumns.Count -1 Do  
        MyColumns[Col] := MyColumns[Col];    

      MyFile[Row] := 'INSERT INTO TABLE1 VALUES (' + IntToStr(Row) + ',' + MyColumns.DelimitedText + ');';

    end;
   
    ShowMessage(MyFile.Text);
   
    //here's where I would like to add the ListBox display part.
    //I have tried ListBox1.Items.Add(MyFile.text), plus a few variations, but nothing seems to be working.
    //All I want to do is display the MyFile.Text (as in the showmessage above) in a ListBox.

  finally
    MyFile.Free;      
    MyColumns.Free;  
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);

var
  OpenFile : TOpenDialog;
  FileName : String;

begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist];
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
     then begin
        FileName := OpenFile.FileName;

       CSVImport(FileName);
    end
    else
      MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   Edit1.Text := OpenFile.FileName;

end;

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
11 Comments
 
LVL 6

Expert Comment

by:bpana
ID: 12097500
ListBox1.Items.Assign(MyFile.text)
0
 
LVL 17

Expert Comment

by:mokule
ID: 12097513

ListBox1.Items.Text := MyFile.Text;
0
 
LVL 6

Expert Comment

by:bpana
ID: 12097527
sorry, it should be:
ListBox1.Items.Assign(MyFile)
0
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!

 
LVL 6

Expert Comment

by:vadim_ti
ID: 12097578
  ListBox1.Items.LoadFromFile(filename);
0
 

Author Comment

by:south_paw
ID: 12097592
I've tried both of the above methods, but get the error:

"Project1.exe. raised exception class EAccessViolation with message 'Access violation at address ... in module 'Project1.exe'. Read of address ...  Process stopped. Use step or run to continue."

i must be missing something else?
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12097611
 ListBox1.Items.AddStrings(MyFile);
0
 
LVL 6

Expert Comment

by:bpana
ID: 12097625
what version of delphi do you use?
0
 
LVL 6

Expert Comment

by:bpana
ID: 12097669
it seems that you have already freed your MyFile StringList.
if you see the ShowMessage and imediatly after you assign the list to the Listbox it should work.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12097733
If your csv contains more than one column then you should use ListView or threeview. And why not... you culd use a stringGrid. What event or property do you need that you are you so interested to use the listbox?
0
 
LVL 11

Accepted Solution

by:
calinutz earned 500 total points
ID: 12097754
Or try thios last part of your function:
...
  For Row := 1 to MyFile.Count-1 Do
    Begin
      Form1.ListBox1.Items.Add(MyFile[row]);
      MyColumns.CommaText := MyFile[Row];
      For Col := 0 to MyColumns.Count -1 Do
      begin
        MyColumns[Col] := MyColumns[Col];
      end;
        MyFile[Row] := 'INSERT INTO TABLE1 VALUES (' + IntToStr(Row) + ',' + MyColumns.DelimitedText + ');';

    end;
        ShowMessage(MyFile.Text);

    //here's where I would like to add the ListBox display part.
    //I have tried ListBox1.Items.Add(MyFile.text), plus a few variations, but nothing seems to be working.
    //All I want to do is display the MyFile.Text (as in the showmessage above) in a ListBox.

  finally
    MyFile.Free;
    MyColumns.Free;
  end;
end;
0
 

Author Comment

by:south_paw
ID: 12097839
Thanks Calinutz,

That reads the data from the CSV into the Listbox.
0

Featured Post

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!

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

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