{"id":1419,"date":"2022-09-30T08:45:58","date_gmt":"2022-09-30T14:45:58","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1419"},"modified":"2022-09-30T08:45:58","modified_gmt":"2022-09-30T14:45:58","slug":"adding-event-handlers","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2022\/09\/30\/adding-event-handlers\/","title":{"rendered":"Adding Event handlers"},"content":{"rendered":"\n<p>OK So now we have a control on a Window<br>But how do we react to events that happen to that control ?<br>Like KeyDown, Keyup, GotFocus, LostFocus, and TextChanged. T<em>hats all the events there are on TextFields &#8211; for now<\/em>.<\/p>\n\n\n\n<p>In Xojo you&#8217;d just add the event handler to the control &amp; put code in it.<br>In C# its much the same.  In C# we write an event handler and attach the handler to the controls event.<\/p>\n\n\n\n<p>But hat events are there ? We can see these in the Assembly Browser.<\/p>\n\n\n\n<p>Right click on TextField and select <em>Go to definition<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"460\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.29.04-PM.png?resize=525%2C460&#038;ssl=1\" alt=\"\" class=\"wp-image-1420\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.29.04-PM.png?w=818&amp;ssl=1 818w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.29.04-PM.png?resize=300%2C263&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.29.04-PM.png?resize=768%2C672&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>If we fold open TextField we see the following<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"511\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.30.56-PM.png?resize=525%2C511&#038;ssl=1\" alt=\"\" class=\"wp-image-1421\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.30.56-PM.png?w=1000&amp;ssl=1 1000w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.30.56-PM.png?resize=300%2C292&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.30.56-PM.png?resize=768%2C748&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>You&#8217;ll see properties like BackColor, Editable, and events like GotFocus, LostFocus, KeyDown and KeyUp.<\/p>\n\n\n\n<p>If you click on the GotFocus event handler you&#8217;ll see it says <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\npublic event EventHandler GotFocus;<\/code><\/pre>\n\n\n\n<p>This says that GotFocus is an EventHandler that takes NO parameters.<br>C# defines the EventHandler type. In Xojo terms its a delegate.<br>And that is a method that matches some specific signature.<br>In this case you can look at what that signature is &#8211; but I&#8217;ll save you the trouble.<\/p>\n\n\n\n<p>It looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public delegate void EventHandler (object? sender, EventArgs e);\n<\/code><\/pre>\n\n\n\n<p>So the method we want to write to handle Got Focus needs to look like the following (the name is totally arbitrary but again good names help)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void textfield_GotFocus(object sender, EventArgs e)\n{\n}<\/code><\/pre>\n\n\n\n<p>But where to put this ?<br>Believe it or not, even in Xojo, the event handlers for controls are part of the WINDOW. <br>So we&#8217;ll write this event handler and put it in the MainWindow.cs as part of the class.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"378\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.47.47-PM.png?resize=525%2C378&#038;ssl=1\" alt=\"\" class=\"wp-image-1422\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.47.47-PM.png?w=1007&amp;ssl=1 1007w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.47.47-PM.png?resize=300%2C216&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.47.47-PM.png?resize=768%2C553&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Now we have a handler. How do we attach it to the control itself ?<\/p>\n\n\n\n<p>If you look closely at the image above its that one line after we created the textfield and put text in the field.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    textfield.GotFocus += textfield_GotFocus;\n<\/code><\/pre>\n\n\n\n<p>That line is roughly the equivalent of<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    Addhandler textfield.GotFocus, addressof textfield_GotFocus<\/code><\/pre>\n\n\n\n<p>The handler doesnt DO much &#8211; yet &#8211; but we&#8217;ll fix that.<br>Lets just make it write to the debug log when we get focus.<\/p>\n\n\n\n<p>In the event handler put<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    Debug.Print(\"textfield_GotFocus\");<\/code><\/pre>\n\n\n\n<p>Now when you run you probably want see anything. But if you click on <strong><i>Application Output <\/i><\/strong><i>yo<\/i>u should see the debug messages<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"334\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.55.14-PM.png?resize=525%2C334&#038;ssl=1\" alt=\"\" class=\"wp-image-1423\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.55.14-PM.png?resize=1024%2C652&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.55.14-PM.png?resize=300%2C191&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.55.14-PM.png?resize=768%2C489&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.55.14-PM.png?w=1192&amp;ssl=1 1192w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"330\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.56.07-PM.png?resize=525%2C330&#038;ssl=1\" alt=\"\" class=\"wp-image-1424\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.56.07-PM.png?resize=1024%2C644&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.56.07-PM.png?resize=300%2C189&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.56.07-PM.png?resize=768%2C483&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.56.07-PM.png?w=1189&amp;ssl=1 1189w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Now because there IS only one field on the layout that can get focus we&#8217;re not going to see a lot of changes. If you added a second text field and hooked up the SAME got focus handler to it you&#8217;d be able to move focus and see a got focus event each time focus shifted.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"329\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.59.43-PM.png?resize=525%2C329&#038;ssl=1\" alt=\"\" class=\"wp-image-1425\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.59.43-PM.png?resize=1024%2C641&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.59.43-PM.png?resize=300%2C188&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.59.43-PM.png?resize=768%2C481&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-10.59.43-PM.png?w=1200&amp;ssl=1 1200w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>One thing you&#8217;ll notice is that we reused the SAME event handler between the two text fields. More or less you can easily do what Xojo does for<em> control sets (controls arrays)<\/em> <\/p>\n\n\n\n<p>And in the event handler you can see which control was clicked<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing Einhugur.Forms;\n\nnamespace getting_started\n{\n\tpublic class MainWindow : Window\n\t{\n        TextField textfield1;\n        TextField textfield2;\n\n        \/\/ much like the constructor in Xojo\n        protected override void SetupWindow(WindowCreationParameters prm)\n        {\n            prm.Left = 200;\n            prm.Top = 100;\n            prm.Width = 600;\n            prm.Height = 400;\n            prm.Title = \"My first window\";\n            prm.InitialPlacement = WindowPlacement.Center;\n        }\n\n        protected override IEnumerable&lt;Control&gt; SetupControls()\n        {\n            textfield1 = new TextField( 10, 23, 80,  23);\n            textfield1.Text = \"not edit no select\";\n            textfield1.GotFocus += textfield_GotFocus;\n\n            textfield2 = new TextField(10, 55, 80, 23);\n            textfield2.Text = \"not edit no select\";\n            textfield2.GotFocus += textfield_GotFocus;\n\n            return new Control&#91;] { textfield1, textfield2 };\n        }\n\n        private void textfield_GotFocus(object sender, EventArgs e)\n        {\n            Debug.Print(\"textfield_GotFocus \" + ((TextField)sender == textfield1 ? \"textField1\" : \"textField2\") );\n        }               \n    }\n}<\/code><\/pre>\n\n\n\n<p>Now the output looks like<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"129\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-11.08.48-PM.png?resize=525%2C129&#038;ssl=1\" alt=\"\" class=\"wp-image-1426\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-11.08.48-PM.png?w=980&amp;ssl=1 980w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-11.08.48-PM.png?resize=300%2C73&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-29-at-11.08.48-PM.png?resize=768%2C188&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>The<a href=\"https:\/\/great-white-software.com\/miscellaneous\/CSharp\/getting_started-2022-09-30.zip\"> project as it stands now<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK So now we have a control on a WindowBut how do we react to events that happen to that control ?Like KeyDown, Keyup, GotFocus, LostFocus, and TextChanged. Thats all the events there are on TextFields &#8211; for now. In Xojo you&#8217;d just add the event handler to the control &amp; put code in it.In &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2022\/09\/30\/adding-event-handlers\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding Event handlers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[34],"class_list":["post-1419","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1419"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1419\/revisions"}],"predecessor-version":[{"id":1428,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1419\/revisions\/1428"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}