Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another possible way to handle this would be to create some interfaces to represent the role of data provider and data receiver, and then you would implement those interfaces on your form. It would be very similar to doing it with a common data source, but instead of running things through an object, you would implement the interfaces and the data can go directly where it is needed. It may be a bit more efficient that doing it through a DataSource, although it's hard to say without knowing all the specifics, but if you are really transferring loads of data putting it through a separate datasource could cost you some efficiency, especially if you never have a need for all the data in one spot.</p> <p>In the example code here I'm showing what it would look like if you implemented your own event args for different types of data, this also could be used in a common data source for the events if you wanted to be able to have a little more granularity over what got sent when. (Please keep in mind I've typed this all in on the webpage without trying to compile it; this is supposed to give you the idea of how to do it, but its possible (I would estimate 100% change) that I didn't get everything in perfectly. :D) </p> <pre><code>public class FirstDataKindEventArgs : EventArgs { public FirstDataKindEventArgs(int parID, string parName, string parOtherInfo) { Id = parId; Name = parName; OtherInfo = parOtherInfo; } public int ID { get; set; } public string Name { get; set; } public string OtherInfo { get; set; } } // plus other event arg definitions public interface IExchangeDataProvider { event EventHandler&lt;FirstDataKindEventArgs&gt; FirstDataKindReceived; event EventHandler&lt;SecondDataKindEventArgs&gt; SecondDataKindReceived; event EventHandler&lt;ThirdDataKindEventArgs&gt; ThirdDataKindReceived; } public interface IExchangeDataReceiver { void ConnectDataProvider(IExchangeDataProvider Provider); } </code></pre> <p>then in your data providing form you would implement the interface:</p> <pre><code>public partial class MyProvidingForm : System.Windows.Forms.Form, IExchangeDataProvider { // normal form stuff // ... #region IExchangeDataProvider public event EventHandler&lt;FirstDataKindEventArgs&gt; FirstDataKindReceived; public event EventHandler&lt;SecondDataKindEventArgs&gt; SecondDataKindReceived; public event EventHandler&lt;ThirdDataKindEventArgs&gt; ThirdDataKindReceived; public void FireDataReceived(EventArgs Data) { FirstDataKindEventArgs FirstKindData = Data as FirstDataKindEventArgs; if (FirstDataKindEventArgs != null) if (FirstDataKindReceived != null) FirstDataKindReceived(FirstKindData); //... etc. } public void GotSomeDataOfTheFirstKind(int TheID, string SomeName, string Other) { FirstDataKindEventArgs eArgs = new FirstDataKindEventArgs(TheId, SomeName, Other); FireDataReceived(eArgs); } </code></pre> <p>and in your receiver form(s) or other classes you wish to receive data:</p> <pre><code> public partial class FirstDataKindReceivingForm : System.Windows.Forms.Form, IExchangeDataReceiver { // usual form stuff // ... private IExchangeDataProvider myDataProvider; public void ConnectDataProvider(IExchangeDataProvider Provider) { myDataProvider = Provider; myDataProvider.FirstDataKindReceived += new EventHandler&lt;FirstDataKindEventArgs&gt;( HandleFirstKindOfDataReceived ); } private void HandleFirstKindOfDataRecieved ( object sender, FirstDataKindEventArgs ) { // do whatever with data } } #endregion } </code></pre> <p>and so forth.</p>
 

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