Solved

An easy 500 point ListBox question...

Posted on 2004-09-19
11
217 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
Technology Partners: 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

624 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