 |
|
ขออนุญาติสอบถามเรื่องการ Pass parameter in EventHandler [C#] มีวิธีส่งแบบไหนครับ? |
|
 |
|
|
 |
 |
|
เกาะๆไปด้วยครับ อยากรู้เช่นกัน
|
 |
 |
 |
 |
Date :
2016-07-04 21:11:13 |
By :
deksoke |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอิ่มมม เหมือนผมเล่นตลกเลยนะครับ หวังว่า Example ของผมจะเข้าใจง่าย และเป็นประโยชน์กับคุณ Furikuri และท่านอื่นๆด้วยนะครับ ไว้จะผมจะแวะเวียนมาใหม่ ^^'
Answer Code (C#)
using System;
using System.Windows.Forms;
namespace ExampleEventHandler {
public partial class EventTutorial : Form {
public event EventHandler openCustomDialog;
public EventTutorial() {
InitializeComponent();
}
private void EventTutorial_Load(object sender, EventArgs e) {
openCustomDialog += EventTutorial_openCustomDialog;
}
private void EventTutorial_openCustomDialog(object sender, EventArgs e) {
MessageBox.Show("Hello Event! : " + sender, "Message");
}
private void mbtnSendEvent_Click(object sender, EventArgs e) {
EventHandler handler = openCustomDialog;
Button btnParameter = sender as Button;
if (btnParameter != null) {
btnParameter.Text = "I was clicked! ^^'";
}
if (handler != null) handler(btnParameter.Text, e);
}
}
}
|
ประวัติการแก้ไข 2016-07-04 22:58:33 2016-07-04 22:59:19
 |
 |
 |
 |
Date :
2016-07-04 22:52:35 |
By :
cre_kiwsan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จัดปายครับ
|
 |
 |
 |
 |
Date :
2016-07-06 14:56:41 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
delegate เรอะ 555+ กำลังหาอ่านอยู่เมื่อเช้านี่เลยครับ
ความรู้สึกหลังจากที่อ่านแล้วให้อารมณ์เหมือนการส่ง function ให้ทำ callback ในภาษา javascript เลยนะ
งืมๆๆ เพียงแต่ delegate นั้น function ที่ส่งไป return type และ parameter ต้องตรงกันเป๊ะๆครับ
แตกต่างจาก function ใน javascript ที่มันไม่ได้สนใจอะไรพวกนี้
ไม่ส่งอะไรไปก็ได้ค่าเป็น null, undefined กันไป
แต่ทั้งนี้ทั้งนั้น ยังไม่เข้าใจว่าจะสามารถนำไปใช้ในโอกาสแบบไหนได้บ้างนี่สิ
นึกเคสที่จะใช้งาน delegate ไม่ออกเลย ใครเคยใช้แนะนำหน่อยนะครับ
http://www.maxmodify.com/article/4/delegate-%E0%B9%83%E0%B8%99-c-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3
https://gpluspluss.com/2012/06/24/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-deletgate-basic/
|
ประวัติการแก้ไข 2016-07-06 21:30:35 2016-07-06 21:33:05
 |
 |
 |
 |
Date :
2016-07-06 21:19:54 |
By :
deksoke |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมสรุปแบบง่ายฯให้ฟัง Delegate มันคืออะไร
ตัวอย่างที่คุณเข้าใจและให้มันอยู่แค่ 2. ครับ
1. แนวทางเดิมฯ --> 2.Delegate --> 3. LINQ --> 4. LAMPDA
ผมอธิบายด้วยภาพและ Source code (VB ผมเขียน C# ได้คล่องยิ่งกว่า VB)

Code (VB.NET)
Public Class Calculator : Inherits Form
Private Delegate Function DelegateFormula(ByVal m As Double, ByVal n As Double) As Double
Private Formula As New Dictionary(Of String, DelegateFormula)() From {{"+", Function(m, n) m + n},
{"-", Function(m, n) m - n},
{"*", Function(m, n) m * n},
{"/", Function(m, n) m / n}
}
'Make Love Click
Private Sub WhenMakeLove(sender As Object, e As EventArgs) Handles btnPlus.Click, btnSubstract.Click, btnMultiply.Click, btnDivision.Click
Dim op As String = DirectCast(sender, Button).Tag
Dim dict = Formula.ElementAt((New Random).Next(0, Formula.Count - 1)) ' Rnd() As Single
txtResult.Text = "N/A"
If (("+-*/").Contains(op)) AndAlso (IsNumeric(txtA.Text) AndAlso IsNumeric(txtB.Text)) AndAlso (Not (op = "/" AndAlso CDbl(txtB.Text) = 0)) Then
txtResult.Text = Formula(op).Invoke(txtA.Text, txtB.Text)
End If
End Sub
End Class
ผมเลือกใช้ VB มากกว่า C# เสมอ
ปล. ลองสร้างฟอร์มขึ้นมา ลากคอนโทรลมาวาง ตั้งชื่อตาม Source code คุณก็จะรู้คำตอบด้วยตัวเอง
|
 |
 |
 |
 |
Date :
2016-07-08 19:16:45 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|