I need to make classes in my program to start a chain of events to write a text in a textbox.
I know that the classes shouldn’t know about the form. how to do that? take into account the fact that i’ll instalize the class on another thread.
I’ve already tried making an interface which connects the classes and make method on the form with ref parameters.
*update:*you all misunderstood me- i was talking about events not in proggraming. all i need is add text to textbox from another class. i added the “chain of events” to define it from simila questions, in them they’ve tried to change the text drectly from the class. sorry.
Without knowing enough about your application I’ll suggest that what you need is to invoke an event in the class that is handled by the form. What happens is like this:
So in the code of the class you’ll need to add some definitions:
The first is a delegate with the signature of the event. By convention the first argument is always a reference to the instance of the class itself, and the rest are the values you want to return. The second is the actual event.
Now, in the function that does whatever processing the class does we need to raise the event when appropriate:
The if clause is used to make sure that someone is actually handling our event, otherwise we might get an exception.
Now let’s take a look at the form. We need to add a function that handles the event. It needs to have the same signature as the delegate we defined earlier. Within that function we do whatever changes we need to the form in light of the values we get back:
Now we’re ready to use all that plumbing we just created. First we add the handler to the event, then we can call the class’s processing functions.
Here’s a more detailed tutorial:
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx