2012-12-05 12 views
5

मुझे एक विंडोज फॉर्म एप्लिकेशन बनाने की आवश्यकता है जो डेटा भेजने और Form इंस्टेंस से डेटा प्राप्त करने में सक्षम है। इसका क्या मतलब है, Form उनमें से एक प्रकाशक और ग्राहक है।फॉर्मों के बीच डेटा भेजने के लिए ईवेंट और प्रतिनिधि का उपयोग कैसे करें?

दुर्भाग्यवश, मैं उस दिन बीमार था, और मैं उस दिन व्याख्यान में नहीं हो सका।

मैं फिर से समझाएंगे:

मैं एक छोटे से चैट Form, जिनके पास है:, नया उदाहरण बटन संदेशों प्राप्त किया, और संदेश भेजें।

के रूप में आप सही नीचे देख सकते हैं:

enter image description here

जब मैं एक संदेश भेजने के लिए, यह सभी उदाहरणों के "प्राप्त" पाठ बॉक्स में दिखाया जाएगा।

मुझे लगता है कि मुझे प्रतिनिधियों और घटनाओं का उपयोग करने की आवश्यकता है।

ऐसा कैसे करें? धन्यवाद!!

+0

"पर्यवेक्षक" पैटर्न पर नज़र डालें। http://msdn.microsoft.com/en-us/library/ee817669.aspx –

उत्तर

7

यहां एक त्वरित समाधान है .. मुझे पता है कि आप किसी भी प्रश्न हैं, तो चलो या आप टिप्पणी भ्रामक ..

पाते हैं यहाँ फार्म वर्ग (कोड के पीछे) है। ध्यान दें कि फॉर्म को तत्काल चालू करने के बाद, यह संदेश ईवेंट के अंदर हैंडलमेसेज ईवेंट में ईवेंट ईवेंट को "तारों" द्वारा "सब्सक्राइब" करता है। फॉर्म क्लास के भीतर, यह वह जगह है जहां ListView का आइटम संग्रह पॉप्युलेट किया गया है।

जब भी नया प्रपत्र बटन क्लिक किया जाता है, एक ही फार्म मिलता है बनाया है और प्रदर्शित किया

public partial class Form1 : Form 
{ 
    Messages _messages = Messages.Instance; // Singleton reference to the Messages class. This contains the shared event 
              // and messages list. 

    /// <summary> 
    /// Initializes a new instance of the <see cref="Form1"/> class. 
    /// </summary> 
public Form1() 
{ 
    InitializeComponent(); 

    // Subscribe to the message event. This will allow the form to be notified whenever there's a new message. 
    // 
    _messages.HandleMessage += new EventHandler(OnHandleMessage); 

    // If there any existing messages that other forms have sent, populate list with them. 
    // 
    foreach (var messages in _messages.CurrentMessages) 
    { 
     listView1.Items.Add(messages); 
    } 
} 

/// <summary> 
/// Handles the Click event of the buttonNewForm control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
private void buttonNewForm_Click(object sender, EventArgs e) 
{ 
    // Create a new form to display.. 
    // 
    var newForm = new Form1(); 
    newForm.Show(); 
} 

/// <summary> 
/// Handles the Click event of the buttonSend control. Adds a new message to the "central" message list. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
private void buttonSend_Click(object sender, EventArgs e) 
{ 
    string message = String.Format("{0} -- {1}", DateTime.UtcNow.ToLongTimeString(), textBox1.Text); 
    textBox1.Clear(); 
    _messages.AddMessage(message); 
} 

/// <summary> 
/// Called when [handle message]. 
/// This is called whenever a new message has been added to the "central" list. 
/// </summary> 
/// <param name="sender">The sender.</param> 
/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
public void OnHandleMessage(object sender, EventArgs args) 
{ 
    var messageEvent = args as MessageEventArgs; 
    if (messageEvent != null) 
    { 
     string message = messageEvent.Message; 
     listView1.Items.Add(message); 
    } 
} 
(के बाद से एक ही तर्क फार्म के सभी उदाहरणों के लिए बिल्कुल वैसा ही हो जाएगा इस, कोड पुनः उपयोग के लिए अनुमति देता है)

}

यहां एक संदेश वर्ग है जिसे मैंने फ़ॉर्म के बीच भेजे गए संदेशों की "केंद्रीय" सूची को संभालने के लिए बनाया है। संदेश वर्ग एक सिंगलटन है (जिसका अर्थ है, इसे केवल एक बार तत्काल किया जा सकता है), जो एक फॉर्म के सभी उदाहरणों में एक सूची को जारी रखने की अनुमति देता है। सभी फॉर्म एक ही सूची साझा करेंगे, और जब भी कोई सूची अपडेट हो जाती है तो अधिसूचित हो जाएंगे। जैसा कि आप देख सकते हैं, "हैंडलमेज" ईवेंट वह ईवेंट है जो प्रत्येक फॉर्म जब भी बनाया/दिखाया गया हो, तो सदस्यता लेगा।

यदि आप NotifyNewMessage फ़ंक्शन पर नज़र डालते हैं, तो इसे संदेश वर्ग द्वारा बुलाया जाता है ताकि सदस्यता में बदलाव हो सके कि परिवर्तन हुआ था। चूंकि EventArgs का उपयोग ग्राहकों को डेटा पास करने के लिए किया जाता है, इसलिए मैंने सभी ग्राहकों को नए जोड़े गए संदेशों को पास करने के लिए एक विशेष EventArgs बनाया है।

class Messages 
{ 
    private List<string> _messages = new List<string>(); 
    private static readonly Messages _instance = new Messages(); 
    public event EventHandler HandleMessage; 

    /// <summary> 
    /// Prevents a default instance of the <see cref="Messages"/> class from being created. 
    /// </summary> 
    private Messages() 
    { 
    } 

    /// <summary> 
    /// Gets the instance of the class. 
    /// </summary> 
    public static Messages Instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 

    /// <summary> 
    /// Gets the current messages list. 
    /// </summary> 
    public List<string> CurrentMessages 
    { 
     get 
     { 
      return _messages; 
     } 
    } 

    /// <summary> 
    /// Notifies any of the subscribers that a new message has been received. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void NotifyNewMessage(string message) 
    { 
     EventHandler handler = HandleMessage; 
     if (handler != null) 
     { 
      // This will call the any form that is currently "wired" to the event, notifying them 
      // of the new message. 
      handler(this, new MessageEventArgs(message)); 
     } 
    } 

    /// <summary> 
    /// Adds a new messages to the "central" list 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void AddMessage(string message) 
    { 
     _messages.Add(message); 
     NotifyNewMessage(message); 
    } 
} 

/// <summary> 
/// Special Event Args used to pass the message data to the subscribers. 
/// </summary> 
class MessageEventArgs : EventArgs 
{ 
    private string _message = string.Empty; 
    public MessageEventArgs(string message) 
    { 
     _message = message; 
    } 

    public String Message 
    { 
     get 
     { 
      return _message; 
     } 
    } 
} 

भी .. संदेशों के लिए आदेश होने के लिए "प्रदर्शित" सही ढंग से, "सूची" के लिए ListView नियंत्रण के "दृश्य" गुण सेट मत भूलना। एक आसान (और पसंदीदा तरीका) केवल ऑब्जर्जेबल कोलेक्शन सूची प्रकार का उपयोग करेगा, जो पहले से ही एक ऐसी घटना प्रदान करता है जिसका आप सदस्यता ले सकते हैं।

उम्मीद है कि इससे मदद मिलती है।

+0

ठीक है, धन्यवाद, मैं घर पर रहने पर कोशिश करूंगा (क्योंकि मैं अपने लैपटॉप पर उबंटू का उपयोग कर रहा हूं)। क्या आप कृपया मुझे जल्द ही बता सकते हैं कि आपने वहां क्या लिखा था? – Billie

+0

@ user1798362 अधिक जानकारी प्रदान करने के लिए अपडेट किया गया। यदि आपके कोई और प्रश्न हैं तो मुझे बताएं .. –

+0

यह प्रतिनिधियों और घटनाओं का उपयोग नहीं कर रहा है, लेकिन विचार स्पष्ट है। धन्यवाद। – Billie

संबंधित मुद्दे