Alan
Alan

Reputation: 21

Calling a function in a form

please be gentle. I'm very new to coding and finding my way as I go along.

I have created a set of client / server sockets in a class called communications which are all functioning OK.

How do I use the trigger of receiving a set of data to call a set of functions within a form to update the contents of the form?

thanks

Upvotes: 2

Views: 49

Answers (2)

McAden
McAden

Reputation: 13972

One option is an event

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/

If your communication class receives something to trigger it can publish that to the event. Anything that has subscribed to that event will receive the information you've specified.

Be sure to clean up subscriptions before you dispose of any subscribers.

Note that depending on your approach you're probably already using events since you're using forms. Things like myButton.Click += ....

Upvotes: 2

Ali Faris
Ali Faris

Reputation: 18602

you can pass a reference of your form (or view) to the communications class. and call the method that responsible of changing the content of your form

class Communications { 

     private MyForm myForm;
     ....
}

 ...
 //the code when you receiving a set of data
 this.myForm.ChangeContent(data);
 ...

but you need to be aware that when receiving data on other thread than MainThread(UIThread) then you figure out how to change UI from other thread (I think you can find answers here in Stackoverflow)

Upvotes: 0

Related Questions