Solved

An easy 500 point ListBox question...

Posted on 2004-09-19
11
212 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

12 Experts available now in Live!

Get 1:1 Help Now