Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

How to implement EditFocusChange in Winforms? 28 Sep 2022 07:01 #24034

  • ic2
  • ic2's Avatar
  • Topic Author


  • Posts: 1615
  • 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:
    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:
    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

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 07:42 #24036

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3299
  • Hi Dick,
    the C# documentation says this:
    private void Control1_GotFocus(Object sender, EventArgs e)
    and traduced to X# it would be
    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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 07:48 #24038

    • Chris
    • Chris's Avatar


  • Posts: 3856
  • Hi Wolfgang,

    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! :)

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 07:53 #24039

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3299
  • Hi Chris,
    it does, and indeed it is very helpful!


    Step1:



    Step2:



    Step3:


    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it
    Attachments:

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 08:09 #24041

    • Chris
    • Chris's Avatar


  • Posts: 3856
  • Hi Wolfgang,

    Ah, thanks, now I understood what you meant!
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 08:37 #24043

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3299
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 13:38 #24045

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1615
  • Hello Wolfgang,

    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 learn.microsoft.com/en-us/dotnet/api/sys...w=windowsdesktop-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

    Please Log in or Create an account to join the conversation.

    Last edit: by ic2.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 13:53 #24046

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3299
  • Hi Dick,
    for me this works:
    self:oField1:GotFocus += self:Field1GotFocus
    self:oField1:Location := System.Drawing.Point{96 , 6}
    self:oField1:LostFocus += self:Field1LostFocus
    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

    And here is the application, based on the XIDE sample Windows Forms app:

    File Attachment:

    File Name: FocusChangeApp.zip
    File Size:2 KB

    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

    www.riedmann.it - docs.xsharp.it
    Attachments:

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 14:11 #24047

    • Fabrice
    • Fabrice's Avatar


  • Posts: 350
  • 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.

    Now, the GotFocus Event doesn't exist with Windows Forms, you have to use Enter there.
    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

    File Attachment:

    File Name: EventTest.zip
    File Size:11 KB
    Attachments:

    Please Log in or Create an account to join the conversation.

    Last edit: by Fabrice. Reason: Err...My bad... GotFocus exist...

    How to implement EditFocusChange in Winforms? 28 Sep 2022 17:01 #24049

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1615
  • Hello Wolfgang, Fabrice,

    for me this works:
    [code]self:oField1:GotFocus += self:Field1GotFocus

    Thank you very much for your efforts and I can tell you that it works (even if not generated from VS). It does 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

    Please Log in or Create an account to join the conversation.

    How to implement EditFocusChange in Winforms? 28 Sep 2022 17:08 #24051

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3299
  • Hi Dick,
    in .NET every event needs to be registered - and it can also be de-registered.
    This concept has the advantage that you can call your callback method how do you like it, you can register more than one callback for every control, and you can register the same callback on different controls.
    But, if you have learned the VO lessons, you can also create your own subclass (I have done that for the pushbutton class):
    using System.Windows.Forms
    using rdm.BaseLib
    
    class rdmButton inherit Button
    constructor()
    	super()
    	return
    
    protect method OnClick( e as EventArgs ) as void
    	super:OnClick( e )
    	if self:Parent:IsMethod( self:Name )
    		self:Parent:Send( self:Name, null_object )
    	endif
    
    	return
    end class
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1