Advertisement
steverobinson

Custom Events |Modal and Modeless Forms|Steve Robinson

Feb 9th, 2011
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. Creating Custom Events in C#:
  2.  
  3. //Great for Sending back values from modal forms
  4.  
  5. http://www.dreamincode.net/forums/topic/176796-quick-and-easy-custom-events/
  6.  
  7. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. //Example application: Uses both Modal and Modeless Forms to show the use of Custom Events and Event Handlers.
  10.  
  11. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12.  
  13. //I have just included the Partial Classes of Main form, FirstNameForm and LastNameForm.
  14.  
  15. //Form1.cs
  16.  
  17.  
  18. using System;
  19. using System.Collections.Generic;
  20. using System.ComponentModel;
  21. using System.Data;
  22. using System.Drawing;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Windows.Forms;
  26.  
  27. namespace WindowsFormsApplication1
  28. {
  29.     public partial class Form1 : Form
  30.     {
  31.        
  32.         public Form1()
  33.         {
  34.             InitializeComponent();
  35.             Form2 FirstNameForm = new Form2();
  36.             FirstNameForm.FirstNameUpdated += new EventHandler<Form2.UpdatedArguments>(FirstNameForm_FirstNameUpdated);
  37.             FirstNameForm.Show();
  38.         }
  39.  
  40.         void FirstNameForm_FirstNameUpdated(object sender, Form2.UpdatedArguments e)
  41.         {
  42.             if (e != null)
  43.                 this.label4.Text = e.FirstName;
  44.         }
  45.  
  46.         private void getlastname_Click(object sender, EventArgs e)
  47.         {
  48.             Form3 LastNameForm = new Form3();
  49.             if (LastNameForm.ShowDialog() == DialogResult.OK)
  50.                 this.label3.Text = LastNameForm.LastName;
  51.         }
  52.        
  53.  
  54.  
  55.     }
  56. }
  57.  
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. //Form2.cs -Modeless
  60. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62. using System;
  63. using System.Collections.Generic;
  64. using System.ComponentModel;
  65. using System.Data;
  66. using System.Drawing;
  67. using System.Linq;
  68. using System.Text;
  69. using System.Windows.Forms;
  70.  
  71. namespace WindowsFormsApplication1
  72. {
  73.     public partial class Form2 : Form
  74.     {
  75.         public Form2()
  76.         {
  77.             InitializeComponent();
  78.         }
  79.    
  80.     //Creating of EventArgs for the custom Event
  81.         public class UpdatedArguments : EventArgs
  82.         {
  83.             private string firstName;
  84.  
  85.             public string FirstName
  86.             {
  87.                 get { return firstName; }
  88.                 set { firstName = value; }
  89.             }
  90.         }
  91.    
  92.     //Creation of Event
  93.         public event EventHandler<UpdatedArguments> FirstNameUpdated;
  94.    
  95.     //Function that raises the Event and broadcasts it to the Classes or Forms
  96.         private void eventRaiser(UpdatedArguments e)
  97.         {
  98.  
  99.             if (FirstNameUpdated != null)
  100.                 FirstNameUpdated(this, e);
  101.         }
  102.  
  103.         private void button1_Click(object sender, EventArgs e)
  104.         {
  105.             UpdatedArguments newEvent = new UpdatedArguments();
  106.             newEvent.FirstName = this.textBox2.Text;
  107.             eventRaiser(newEvent);
  108.         }
  109.     }
  110. }
  111.  
  112. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. //Form3.cs -Modal
  114. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115.  
  116.  
  117. using System;
  118. using System.Collections.Generic;
  119. using System.ComponentModel;
  120. using System.Data;
  121. using System.Drawing;
  122. using System.Linq;
  123. using System.Text;
  124. using System.Windows.Forms;
  125.  
  126. namespace WindowsFormsApplication1
  127. {
  128.     public partial class Form3 : Form
  129.     {
  130.         public Form3()
  131.         {
  132.             InitializeComponent();
  133.         }
  134.  
  135.  
  136.     String Property that wraps the TextBox property.
  137.         public string LastName
  138.         {
  139.             get { return this.textBox1.Text; }
  140.         }
  141.    
  142.     //Sends OK Dialog Status
  143.         private void button1_Click(object sender, EventArgs e)
  144.         {
  145.             this.DialogResult = DialogResult.OK;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement