Event Delegate Kullanarak Formlar Arasında Olay Tetikleme

miRBey

Forum Üyesi
Katılım
5 Eki 2022
Mesajlar
180
Tepkime puanı
0
Puanları
16
Kod:
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ı);
}
}
}
 

Yusuf

Forum Üyesi
Katılım
25 Ara 2022
Mesajlar
840
Tepkime puanı
0
Puanları
16
Bilgi için teşekkürler
 
Üst Alt