I had a problem using a Control [ListView] out side its Class[main form class].. So i made that control [ListView] Static ..Now I can Add and Remove Items from The ListView with ease..
Now the problem is that i cannot use the Event's of this ListView.
I need Your Help.. Is there any other way to do it.
THANK YOU Nelis.
1) Can you tell me how To Access ListBox out side the main class..
I tried Creating an Instance of Form1(Listbox is in This Class) and tried to Add an Item To ListView.. but i dont see any Change in The Form .The ListView Remain the Same.
2) How do i Create Static Event Handlers..
I tried Something Like This but Gave an Error
Form1.listView1.Click += new System.EventHandler(Form1.listView1_Click(EventArgs));
If I'm reading this correctly, it sounds like you just want to be able to manipulate the ListView on a Form from another class somewhere in your application. Have you tried making a public instance method in your form class that takes whatever value you want to add to the ListView as a parameter and then calls the Add method of the ListView? (You could do this for the other methods of ListView as well)
If you did that, you should be able to create an instance of the Form and manipulate the list. One thing you need to do, however, is make sure that the instance of the form that you create is the one that is being shown on the screen.
Not knowing the layout of your application, I will make the assumption that you have the ListView on the main form.
You have some other form or class that is called upon to do something and you want something to appear in the ListView of the main form.
The problem Is the Scoping of the ListView instance. You need to send that instance to the other class, or make it public (modifier on the poperty editor <frowned upon>) on the main form, and make sure the class can see this instance.
Lets say your main form is named form1, your ListView is named listview1, and your other class is named Foo.
// Pass ListView into the constructor of Foo
Foo _foo = new Foo( listview1 );
// OR pass it to a method of Foo
Foo _foo = new Foo();
_foo.DoSomething( listview1 );
private ListView _listview;
public Foo( ListView listview)
this._listview = listview;
public void DoSomething()
_listview.Items.Add("Test from Foo");
public void DoSomething( ListView listview )
listview.Items.Add("Another Test from Foo");