How to implement EditFocusChange in Winforms?

This forum is meant for questions and discussions about the X# language and tools
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to implement EditFocusChange in Winforms?

Post by ic2 »

The last few weeks I've been working with some Winforms VOXported from VO forms.

In VO we use EditFocusChange to set a class variable telling which control got focus.
I have not yet understood how this works in X# WInforms.

I tried adding an individual method for each control for which the program needs to be aware of when it gets focus:

Code: Select all

Method textBoxTo_GotFocus(sender As Object, e As EventArgs)
But I am not sure how to register the event handler (which I did in C# programs). I tried this in the constructor:

Code: Select all

Self:textBoxTo:GotFocus+=textBoxTo_GotFocus	
but then I get:

Error XS0407 'usual MyPrg.textBoxTo_GotFocus(object, System.EventArgs)' has the wrong return type

What should I do differently?

Or did I miss a Winforms equivalent of EditFocusChange ?

Dick
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to implement EditFocusChange in Winforms?

Post by wriedmann »

Hi Dick,
the C# documentation says this:

Code: Select all

private void Control1_GotFocus(Object sender, EventArgs e)
and traduced to X# it would be

Code: Select all

method Control1_GotFocus( sender as Object, e as EventArgs ) as void
So you are missing the return type "as void".
Personally, I would build my own control class inherited from the textbox class and add that method there, and eventually call a "EditFocusChange" method in the window itself.
Wolfgang
P.S. I don't know if VS helps in this, but XIDE optionally creates this method automatically
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4900
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

How to implement EditFocusChange in Winforms?

Post by Chris »

Hi Wolfgang,
wriedmann post=24036 userid=336 wrote: P.S. I don't know if VS helps in this, but XIDE optionally creates this method automatically
It does? How? Must have been another feature that I implemented and totally forgot about! :)

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to implement EditFocusChange in Winforms?

Post by wriedmann »

Hi Chris,
it does, and indeed it is very helpful!


Step1:
28-09-2022_07-51-22.png
28-09-2022_07-51-22.png (7.88 KiB) Viewed 884 times

Step2:
28-09-2022_07-52-02.png
28-09-2022_07-52-02.png (12.94 KiB) Viewed 884 times

Step3:
28-09-2022_07-52-35.png
28-09-2022_07-52-35.png (8.07 KiB) Viewed 884 times
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4900
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

How to implement EditFocusChange in Winforms?

Post by Chris »

Hi Wolfgang,

Ah, thanks, now I understood what you meant!
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to implement EditFocusChange in Winforms?

Post by wriedmann »

Hi Chris,
as I wrote: this is very helpful and I use it several times to see what the prototype on an event handler is, even if I don't use the code as it is written by XIDE, because the "Eventhandler" syntax is not needed anymore in most cases.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to implement EditFocusChange in Winforms?

Post by ic2 »

Hello Wolfgang,
wriedmann post=24036 userid=336 wrote: So you are missing the return type "as void".
There was no problem there. With or without a void, Method textBoxTo_GotFocus isn't fired when I enter the textBoxTo.

Hence I thought I had to register an event handler for it (never sure when I need to to that and when not...) and it is there were I got the error.

I've tried multiple alternatives:

1 Private Method textBoxTo_Enter
2 Private Method textBoxTo_Leave
3 Private Method textBoxTo_Validating
4 textBoxTo_TextChanged

Only 4) is actually working when I type something in that textbox but that is not what is needed here.


For 1&2 Microsoft states in https://learn.microsoft.com/en-us/dotne ... esktop-6.0:

The Enter and Leave events are suppressed by the Form class. The equivalent events in the Form class are the Activated and Deactivate events.

No idea why and even less why Activated/Deactivated, which are fired (or at least documented to do so) when the whole window gets or loses focus, are indicated as alternative.

Bottom line is that there doesn't seem to be any working even which is fired when I enter a control on a Winforms window.

So if anyone has an idea of way or alternative event which actually works, that would be great.

Dick
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

How to implement EditFocusChange in Winforms?

Post by wriedmann »

Hi Dick,
for me this works:

Code: Select all

self:oField1:GotFocus += self:Field1GotFocus
self:oField1:Location := System.Drawing.Point{96 , 6}
self:oField1:LostFocus += self:Field1LostFocus

Code: Select all

method Field1GotFocus(sender as System.Object , e as System.EventArgs) as void
oDisplay:Text		:= "Field1 got focus"
return

method Field1LostFocus(sender as System.Object , e as System.EventArgs) as void
oDisplay:Text		:= "Field1 lost focus"
return
28-09-2022_13-51-00.png
28-09-2022_13-51-00.png (5.72 KiB) Viewed 884 times
And here is the application, based on the XIDE sample Windows Forms app:
FocusChangeApp.zip
(2.46 KiB) Downloaded 107 times
I'm sorry, you have to import that file in XIDE, I don't like to transport it to Visual Studio.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Fabrice
Posts: 458
Joined: Thu Oct 08, 2015 7:47 am
Location: France

How to implement EditFocusChange in Winforms?

Post by Fabrice »

Hi Dick,
I think you misunderstand the doc :
When using Control, the fired events are Enter & Leaver
Form is inheriting from Control, so we should have Enter/Leave also, but at that level, the fired events are Activated/Deactivate.

[strike]Now, the GotFocus Event doesn't exist with Windows Forms, you have to use Enter there.[/strike]
In order to link an Event to a Method, I suggest you use the integrated mechanism of VS :
Select the control, go to the Properties Window, and click on the Event button (Lightning) to switch to the event panel.
Then scroll down to the desired event and double-click on it : The X# integration will generate the needed skeleton, create the link-code and open the editor in the corresponding method.
!!! NEVER suppress the generated skeleton if you have made a error... VS generate the code, so you have to remove it FROM VS.
Follow the same process, but instead to double_clicking on it, Right-Click and select Reset in the context menu.

Now, If you want to check the events and their firing order, I have created a small demo app for you, it is attached to this message.

HTH,
Fab
EventTest.zip
(11.37 KiB) Downloaded 107 times
XSharp Development Team
fabrice(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to implement EditFocusChange in Winforms?

Post by ic2 »

Hello Wolfgang, Fabrice,
wriedmann post=24046 userid=336 wrote: for me this works:

Code: Select all

self:oField1:GotFocus += self:Field1GotFocus
[/quote]

Thank you very much for your efforts and I can tell you that it works (even if not generated from VS). It [i]does[/i] need registering the event but now I am lost why it gave the Error XS0407. Only difference seems the missing 2nd self compared to your registering code but when I uncommented my original registering code it still compiled. And it honestly did not this morning.

Never mind, it compiles and it works now as it should. Later I may need Leave/Enter and then I will have Fabrice's sample to implement it.

Thanks again!

Dick
Post Reply