Highlighted
Absent Member.
Absent Member.
1631 views

ignore "Leave" in a Windows Forms Textfield when a Button is pushed

Jump to solution

I need to trap the Event, when a Textfield is left. I.e. when I push a defined (e.g. List-) button, the field that had Focus causes a "Leave" Event occuring before the button pressed Event is detected.

I need to suppress any Action on Leave, when not caused by Key Input (Tab or Return). Thanks in advance for any help.

Regards

Rainer

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

If you place your code in the textbox validating event and then set the CausesValidation property of the button to false then the validating event for the textbox should not even be fired when the button is clicked.

If you do need to check for the tab key it is available in the KeyData property:

You can check for the tab key pressed in KeyData as follows:

method-id Form1_PreviewKeyDown final private.

      procedure division using by value sender as object e as type System.Windows.Forms.PreviewKeyDownEventArgs.

          if e::KeyData = type Keys::Tab

             set tabwaspressed to true

          else

             set tabwaspressed to false

          end-if

end method.

View solution in original post

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

If I am understanding your request correctly you can check in the Leave event to see if the button has the new focus and then ignore the leave event accordingly.

What is it that you are doing in the Leave event?

If you are validating the entry it may be better to use the Validating event instead.

Anyway, somthing like the following:

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
          set textBox2::Text to "click"
       end method.

       method-id textBox1_Leave final private.
       procedure division using by value sender as object e as type System.EventArgs.
          
          if button1::Focused not = true
             *> process your leave action
             set textBox2::Text to "leave"
          end-if
       end method.
0 Likes
Highlighted
Absent Member.
Absent Member.

No Chris, I can check the Leave Event which, when the field looses Focus calls a method "EvaluateInput". This I need to suppress when no key is pressed. Leave or Validating don't detect another object like a button having been pressed because they occur first.

I can trap Key Events like Function or Enter key through System.Windows.Forms.KeyEventArgs, but, not the Tab Key. Latter would allow me to evaluate a KeyPressed Event rather than Leave to do something.

0 Likes
Highlighted
Absent Member.
Absent Member.

Sorry Chris, I made a mistake: The Validating Event is the right way. But, I'd appreciate if could tell me how to trap the Tab Key.

Regards

Rainer

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

If you place your code in the textbox validating event and then set the CausesValidation property of the button to false then the validating event for the textbox should not even be fired when the button is clicked.

If you do need to check for the tab key it is available in the KeyData property:

You can check for the tab key pressed in KeyData as follows:

method-id Form1_PreviewKeyDown final private.

      procedure division using by value sender as object e as type System.Windows.Forms.PreviewKeyDownEventArgs.

          if e::KeyData = type Keys::Tab

             set tabwaspressed to true

          else

             set tabwaspressed to false

          end-if

end method.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.