Solved

An easy 500 point ListBox question...

Posted on 2004-09-19
11
211 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
  • 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now