• Web sitemizin içeriğine ve tüm hizmetlerimize erişim sağlamak için Web sitemize kayıt olmalı ya da giriş yapmalısınız. Web sitemize üye olmak tamamen ücretsizdir.
  • Sohbetokey.com ile canlı okey oynamaya ne dersin? Hem sohbet et, hem mobil okey oyna!
  • Soru mu? Sorun mu? ''Bir Sorum Var?'' sistemimiz aktiftir. Paylaşın beraber çözüm üretelim.

Event Delegate Kullanarak Formlar Arasında Olay Tetikleme

Üyelik Tarihi
7 Ocak 2015
Konular
4,091
Mesajlar
4,274
MFC Puanı
40
Kod:
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 C_Sharp_Event_Delegate
{
    public partial class Form1 : Form
    {
        //Delegate oluşturalım, parametre olarak string bir değer alsın

        public delegate **** tetikleyici(string tuşabasıldı);

        //Delegate den bir olay oluşturalımç bunu istediğimiz her yerde 
        //çağırmak için statik yapalım

        public static event tetikleyici tetikevent; 

        public Form1()
        {
            InitializeComponent();
        }

        private **** Form1_Load(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();

            //form2 penceresini göstertelim

            f2.Show(); 
        }

        private **** button1_Click(object sender, EventArgs e)
        {
            //button1  tıklandığında bizim oluşturduğumuz olay tetiklensin
            //bunu da Form2 listbox içinde görelim

            tetikevent(button1.Text);
        }

        private **** button2_Click(object sender, EventArgs e)
        {
            //button2  tıklandığında bizim oluşturduğumuz olay tetiklensin
            //bunu da Form2 listbox içinde görelim

            tetikevent(button2.Text);
        }

        private **** button3_Click(object sender, EventArgs e)
        {
            //button3  tıklandığında bizim oluşturduğumuz olay tetiklensin
            //bunu da Form2 listbox içinde görelim

            tetikevent(button3.Text);
        }
    }
}



//////////////////////////////////////////////////////////////////////////
// Form2.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 C_Sharp_Event_Delegate
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private **** Form2_Load(object sender, EventArgs e)
        {
            Form1.tetikevent +=new Form1.tetikleyici(Form1_tetikevent);
             
        }

        **** Form1_tetikevent(string tuşabasıldı)
        {
            listBox1.Items.Add(tuşabasıldı);
        }
    }
}
 
Üst