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
Solved

An easy 500 point ListBox question...

Posted on 2004-09-19
11
214 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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