How do I reference a ListView Control?

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

Hi, I took the ListView control from the toolbox, put it on the form,
and when I tried to compile this (from my constructor):

listViewFiles.Columns.Add("Source",-1,HorizontalAlignment.Left);

I get an object reference not set error. The control on the form has
this created by VS:

private System.Windows.Forms.ListView listViewFiles;


Now, if I do this instead in my constructor:

listViewFiles = new ListView();
listViewFiles.Parent=this;
listViewFiles.View=View.Details;
listViewFiles.Columns.Add("Source",-1,HorizontalAlignment.Left);

it works. Why can't I reference my ListView (listViewFiles) directly,
since I dropped it on the form with the toolbox? Thanks.
 
Hi,

Controls get instantiated in the auto-generated InitializeComponent()
method, which is called from the form's constructor. If you are missing this
method call or if you try to access the controls before this call, you will
get the error.

HTH

Hi, I took the ListView control from the toolbox, put it on the form,
and when I tried to compile this (from my constructor):

listViewFiles.Columns.Add("Source",-1,HorizontalAlignment.Left);

I get an object reference not set error. The control on the form has
this created by VS:

private System.Windows.Forms.ListView listViewFiles;


Now, if I do this instead in my constructor:

listViewFiles = new ListView();
listViewFiles.Parent=this;
listViewFiles.View=View.Details;
listViewFiles.Columns.Add("Source",-1,HorizontalAlignment.Left);

it works. Why can't I reference my ListView (listViewFiles) directly,
since I dropped it on the form with the toolbox? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top