Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would suggest that your listener has no knowledge about how the messages are to be presented. Instead, expose an event that the form can listen to:</p> <pre><code>// event args class for transmitting the message in the event public class MessageEventArgs : EventArgs { public IMessage Message { get; private set; } public MessageEventArgs(IMessage message) { Message = message; } } </code></pre> <p>In your listener class:</p> <pre><code>public class MyListener : IMessageListener { public event EventHandler&lt;MessageEventArgs&gt; MessageReceived; public void MessageTransfer(IMessage m) { OnMessageReceived(new MessageEventArgs(m)); } protected void OnMessageReceived(MessageEventArgs e) { EventHandler&lt;MessageEventArgs&gt; temp = MessageReceived; if (temp != null) { temp(this, e); } } } </code></pre> <p>Now you can add an event listener in your form and add the message information to a listbox or any other kind of control you like.</p> <p><strong>Update</strong><br> Here is an example on how to hook up the event handler in the form. This code makes two assumptions:</p> <ul> <li>The event <code>MessageReceived</code> is defined in the <code>IMessageListener</code> interface</li> <li>The <code>IMessage</code> interface has a property called <code>Text</code>.</li> </ul> <p>Code sample:</p> <pre><code>public partial class MainUI : Form { private IMessageListener _messageListener; public MainUI() { InitializeComponent(); _messageListener = new MyListener(); _messageListener.MessageReceived += MessageListener_MessageReceived; } void MessageListener_MessageReceived(object sender, MessageEventArgs e) { _messageListBox.Items.Add(e.Message.Text); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload