Hi..

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.

Recommended Answers

All 9 Replies

First of all, why would you be having problems using ListView outside of the main form? It's probably not being accessed properly.

Secondly, instance methods can access static methods/objects but not the other way around. If you're having problems with events, it's probably because the event handler methods are not static.

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.

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.

Yes i want to do this.. But How ?

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 );
_foo.DoSomething();

// OR pass it to a method of Foo
Foo _foo = new Foo();
_foo.DoSomething( listview1 );


class Foo
{
     private ListView _listview;
     public Foo( ListView listview)
    {
         this._listview = listview;
    }

    public  void DoSomething()
    {
         _listview.Items.Add("Test from Foo");
    }
}

OR

class Foo
{

     public void DoSomething( ListView listview )
     {
          listview.Items.Add("Another Test from Foo");
      }
}

Thank You JerryShaw... Yes This Woks Fine.. But i want to Add Item into list box when an Event in Form2 occurs .. so can i modify the parameters of the event func.. or else is there any other way..

If you want the listbox on the Main form to be updated when something happens on the other class, you need to create an event in the other class, and subscribe to it from the main form.

If you want the event to provide you with some information then you will need to either use an existing delegate or create your own.

You will want to create a simple arguments class to contain the information you want passed back to the main form.

class FooArgs
{
       private string _text;
       public string Text
       {  
            get{return _text;}
            private set{_text = value;}
       }
       public FooArgs (string value)
      {
          Text = value;
      }
}

You can put anything else you want into that FooArgs class that you need.

Next you want to setup a delegate for it, and assign an event in the Foo class.

public delegate void FooChanged(object sender, FooArgs e)
   public event FooChanged onFooChanged;

Now in your Foo class, when you want to notify the main form of some change:

public void DoSomething(string something)
{
      if( onFooChanged != null )
          onFooChanged(this, new FooArgs("erata"));
}

In the main form class, you would assign this event handler.

Foo foo = new Foo();
   foo.FooChanged += FooChanged(someFooHandler);

Give it a try... I free handed this, so hopefully there are no errors.
// Jerry

Thank You JerryShaw.. You Saved me a lot of Time .

Thank You JerryShaw.. You Saved me a lot of Time .

Welcome.... Don't forget to mark this as Solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.