Best way to create a ListView in C# (or

I am rewriting an app in C# that was written in VB6.  I want to write a common routine where I can pass in parameters, including an arraylist of data, and have the listview created for me in that common routine, passed back, and then displayed on a form.  The purpose is to not have code that creates a listivew in the 100+ forms that we will have.  I have tried passing back a listview control into a procedure called from the form, but it doesn't see to work.  Any ideas?
Who is Participating?
jjacksnConnect With a Mentor Commented:
This should be fairly straight forward:

public static ListView CreateListView(arg1, arg2, arg3)
     ListView li = new ListView();
     //Business Logic to change list view items here
     //custom formatting here
     return li;

In the place where you are getting the list view, you need to make sure to add it to the form.  

HOWEVER, I would recommend against this...

You are still going to have to have custom code to put the listview in the correct place, and I don't see why you would want to do that programmatically vs. in the designer.  

It might be better to create a class that has the logic to add the items correctly, so you can always call this function.  Have all of the other forms inherit from this form, and assign the listview variable that is used in the superclass to the actual listview in the constructor.  
I think you want to already have the listview on the form and just pass a reference of it to the common function.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.