{"id":218,"date":"2018-05-12T21:20:23","date_gmt":"2018-05-12T14:20:23","guid":{"rendered":"http:\/\/thietkewebdalat.net\/blog\/?p=218"},"modified":"2018-05-12T21:20:23","modified_gmt":"2018-05-12T14:20:23","slug":"delegate-trong-c","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/delegate-trong-c\/","title":{"rendered":"Delegate trong c#"},"content":{"rendered":"<p>Ch\u00e0o m\u1ecdi ng\u01b0\u1eddi<\/p>\n<p>H\u00f4m nay m\u00ecnh s\u1ebd gi\u1ea3i th\u00edch cho c\u00e1c b\u1ea1n hi\u1ec3u r\u00f5 v\u1ec1\u00a0delegate trong c# nh\u00e9 :<\/p>\n<p><strong>&#8220;Delegate&#8221;\u00a0\u00a0<\/strong>c\u00f3 ngh\u0129a l\u00e0 \u1ee7y nhi\u1ec7m h\u00e0m ,n\u00f3 gi\u1ed1ng nh\u01b0 m\u1ed9t ng\u01b0\u1eddi \u0111\u1ea1i di\u1ec7n hay \u0111\u1ea1i s\u1ee9 ,n\u00f3 c\u00f3 th\u1ec3 tri\u1ec7u g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c b\u1ea5t k\u00ec n\u01a1i n\u00e0o: t\u1eeb \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng kia, t\u1eeb thread n\u00e0y sang thread kia \u0111\u00e2y l\u00e0 \u0111i\u1ec3m quan tr\u1ecdng nh\u1ea5t c\u1ee7a delegate ,c\u00e1c b\u1ea1n n\u00ean nh\u1edb khi s\u1eed d\u1ee5ng n\u00f3<\/p>\n<p>Khai b\u00e1o delegate nh\u01b0 sau:<\/p>\n<p><strong>[public, private, protected, internal]<\/strong>\u00a0<span style=\"color: #ff0000;\">delegate<\/span> <span style=\"color: #0000ff;\">[ki\u1ec3u d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1]\u00a0\u00a0<span style=\"color: #000000;\">T\u00ean H\u00e0m (tham s\u1ed1)<\/span><\/span><\/p>\n<p>M\u00ecnh l\u1ea5y v\u00ed d\u1ee5 cho c\u00e1c b\u1ea1n d\u1ec5 hi\u1ec3u nh\u00e9 ,l\u00fd thuy\u1ebft th\u00ec c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ecdc t\u00e0i li\u1ec7u ,ch\u1ed7 n\u00e0o kh\u00f4ng hi\u1ec3u th\u00ec c\u00e1c b\u1ea1n c\u1ee9 comment m\u00ecnh tr\u1ea3 l\u1eddi nh\u00e9 :<\/p>\n<p>M\u00ecnh c\u00f3 2 form : form 1 v\u00e0 form 2 nh\u00e9<\/p>\n<p><strong>Form 1<\/strong> m\u00ecnh s\u1ebd c\u00f3 1 button v\u00e0 1 label :<\/p>\n<p>+ Button n\u00e0y khi m\u00ecnh click v\u00e0o n\u00f3 s\u1ebd hi\u1ec7n th\u1ecb <strong>Form 2<\/strong><\/p>\n<p>+ Label n\u00e0y m\u00ecnh s\u1ebd hi\u1ec7n th\u1ecb k\u1ebft qu\u1ea3 ,khi m\u00ecnh l\u1ea5y t\u1eeb <strong>Form 2<\/strong><\/p>\n<p><strong>Form 2<\/strong> m\u00ecnh s\u1ebd c\u00f3 1 button v\u00e0 1 textbox:<\/p>\n<p>+ Button n\u00e0y khi m\u00ecnh click th\u00ec n\u00f3 s\u1ebd truy\u1ec1n k\u1ebft qu\u1ea3 t\u1eeb textbox qua<strong> Form 1<\/strong><\/p>\n<p>+ Textbox n\u00e0y m\u00ecnh s\u1ebd nh\u1eadp k\u1ebft qu\u1ea3 v\u00e0o<\/p>\n<p>Sau \u0111\u00e2y l\u00e0 \u0111o\u1ea1n code v\u00ed d\u1ee5 tr\u00ean nh\u00e9 :<\/p>\n<pre>private void btnForm1_Click(object sender, System.EventArgs e)\r\n{\r\n\/\/T\u1ea1o form 2\r\nForm2 form2 = new Form2();\r\n\r\n\/\/ g\u1ecdi delegate\r\nform2.passValue = new Form2.PassData(GetData);\r\n\r\n\/\/ hi\u1ec7n th\u1ecb Form 2\r\nform2.Show();\r\n}\r\n\r\nprivate void GetData(object sender)\r\n{\r\n\/\/ G\u00e1n d\u1eef li\u1ec7u v\u00e0o lable\r\n    txtLabel1.Text = ((TextBox)sender).Text;\r\n}\r\n==========Form 2\r\npublic class Form2 : System.Windows.Forms.Form\r\n{\r\n\/\/ \u0110\u1ecbnh ngh\u0129a delegate\r\n    public delegate void PassData(object sender);\r\n\r\n   \/\/\r\n    public PassData passValue;\r\n\r\n   private void btnForm2_Click(object sender, System.EventArgs e)\r\n   {\r\n      if (passValue != null)\r\n      {\r\n          passValue(txtForm2);\r\n       {\r\n      this.Hide();\r\n   }\r\n}\r\n<\/pre>\n<p>N\u1ebfu b\u1ea1n n\u00e0o kh\u00f4ng hi\u1ec3u th\u00ec comment cho m\u00ecnh gi\u1ea3i th\u00edch cho nh\u00e9<\/p>\n<p>C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 \u0111\u1ecdc b\u00e0i.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o m\u1ecdi ng\u01b0\u1eddi H\u00f4m nay m\u00ecnh s\u1ebd gi\u1ea3i th\u00edch cho c\u00e1c b\u1ea1n hi\u1ec3u r\u00f5 v\u1ec1\u00a0delegate trong c# nh\u00e9 : &#8220;Delegate&#8221;\u00a0\u00a0c\u00f3 ngh\u0129a l\u00e0 \u1ee7y nhi\u1ec7m h\u00e0m ,n\u00f3 gi\u1ed1ng nh\u01b0 m\u1ed9t ng\u01b0\u1eddi \u0111\u1ea1i di\u1ec7n hay \u0111\u1ea1i s\u1ee9 ,n\u00f3 c\u00f3 th\u1ec3 tri\u1ec7u g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c b\u1ea5t k\u00ec n\u01a1i n\u00e0o: t\u1eeb \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng kia, t\u1eeb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[8,9],"tags":[50,49,51],"class_list":{"0":"post-218","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-net","8":"category-c","9":"tag-delegate-c","10":"tag-delegate-trong-c","11":"tag-su-dung-delegate-trong-c"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":1,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/218\/revisions\/220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/219"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}