Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Creating Custom Events in C#:
- //Great for Sending back values from modal forms
- http://www.dreamincode.net/forums/topic/176796-quick-and-easy-custom-events/
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Example application: Uses both Modal and Modeless Forms to show the use of Custom Events and Event Handlers.
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //I have just included the Partial Classes of Main form, FirstNameForm and LastNameForm.
- //Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- Form2 FirstNameForm = new Form2();
- FirstNameForm.FirstNameUpdated += new EventHandler<Form2.UpdatedArguments>(FirstNameForm_FirstNameUpdated);
- FirstNameForm.Show();
- }
- void FirstNameForm_FirstNameUpdated(object sender, Form2.UpdatedArguments e)
- {
- if (e != null)
- this.label4.Text = e.FirstName;
- }
- private void getlastname_Click(object sender, EventArgs e)
- {
- Form3 LastNameForm = new Form3();
- if (LastNameForm.ShowDialog() == DialogResult.OK)
- this.label3.Text = LastNameForm.LastName;
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Form2.cs -Modeless
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- //Creating of EventArgs for the custom Event
- public class UpdatedArguments : EventArgs
- {
- private string firstName;
- public string FirstName
- {
- get { return firstName; }
- set { firstName = value; }
- }
- }
- //Creation of Event
- public event EventHandler<UpdatedArguments> FirstNameUpdated;
- //Function that raises the Event and broadcasts it to the Classes or Forms
- private void eventRaiser(UpdatedArguments e)
- {
- if (FirstNameUpdated != null)
- FirstNameUpdated(this, e);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- UpdatedArguments newEvent = new UpdatedArguments();
- newEvent.FirstName = this.textBox2.Text;
- eventRaiser(newEvent);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Form3.cs -Modal
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form3 : Form
- {
- public Form3()
- {
- InitializeComponent();
- }
- String Property that wraps the TextBox property.
- public string LastName
- {
- get { return this.textBox1.Text; }
- }
- //Sends OK Dialog Status
- private void button1_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement