Adding Event handlers

OK So now we have a control on a Window
But 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 – for now.

In Xojo you’d just add the event handler to the control & put code in it.
In C# its much the same. In C# we write an event handler and attach the handler to the controls event.

But hat events are there ? We can see these in the Assembly Browser.

Right click on TextField and select Go to definition

If we fold open TextField we see the following

You’ll see properties like BackColor, Editable, and events like GotFocus, LostFocus, KeyDown and KeyUp.

If you click on the GotFocus event handler you’ll see it says

using System;

public event EventHandler GotFocus;

This says that GotFocus is an EventHandler that takes NO parameters.
C# defines the EventHandler type. In Xojo terms its a delegate.
And that is a method that matches some specific signature.
In this case you can look at what that signature is – but I’ll save you the trouble.

It looks like

public delegate void EventHandler (object? sender, EventArgs e);

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)

private void textfield_GotFocus(object sender, EventArgs e)
{
}

But where to put this ?
Believe it or not, even in Xojo, the event handlers for controls are part of the WINDOW.
So we’ll write this event handler and put it in the MainWindow.cs as part of the class.

Now we have a handler. How do we attach it to the control itself ?

If you look closely at the image above its that one line after we created the textfield and put text in the field.

    textfield.GotFocus += textfield_GotFocus;

That line is roughly the equivalent of

    Addhandler textfield.GotFocus, addressof textfield_GotFocus

The handler doesnt DO much – yet – but we’ll fix that.
Lets just make it write to the debug log when we get focus.

In the event handler put

    Debug.Print("textfield_GotFocus");

Now when you run you probably want see anything. But if you click on Application Output you should see the debug messages

Now because there IS only one field on the layout that can get focus we’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’d be able to move focus and see a got focus event each time focus shifted.

One thing you’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 control sets (controls arrays)

And in the event handler you can see which control was clicked

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Einhugur.Forms;

namespace getting_started
{
	public class MainWindow : Window
	{
        TextField textfield1;
        TextField textfield2;

        // much like the constructor in Xojo
        protected override void SetupWindow(WindowCreationParameters prm)
        {
            prm.Left = 200;
            prm.Top = 100;
            prm.Width = 600;
            prm.Height = 400;
            prm.Title = "My first window";
            prm.InitialPlacement = WindowPlacement.Center;
        }

        protected override IEnumerable<Control> SetupControls()
        {
            textfield1 = new TextField( 10, 23, 80,  23);
            textfield1.Text = "not edit no select";
            textfield1.GotFocus += textfield_GotFocus;

            textfield2 = new TextField(10, 55, 80, 23);
            textfield2.Text = "not edit no select";
            textfield2.GotFocus += textfield_GotFocus;

            return new Control[] { textfield1, textfield2 };
        }

        private void textfield_GotFocus(object sender, EventArgs e)
        {
            Debug.Print("textfield_GotFocus " + ((TextField)sender == textfield1 ? "textField1" : "textField2") );
        }               
    }
}

Now the output looks like

The project as it stands now