Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active control question
#1
Hi all,

In VCL it is possible to find out which control is active, and with before and after events, also to tell what the next control to receive focus is, or which control had the focus before.

I used that to bypass some activities in events like OnEnter and OnExit, only doing certain things, if the previous or next active control, was the expected one.

In Intraweb there is an ActiveControl property of the IWForm, but it do not seem to get updated as the focus shifts from control to control. In the IWForm documentation is says it can be used to set the active control when the form is rendered. I suppose it is like the taborder value.

Are there any other way of getting info about which control is the active one ?

Regards
Soren
Reply
#2
Hi Guys,

As there have been no replies to this question, I take it it is either a very trivial question, which everybody with just the slightest Delphi knowledge should know about and therefore no one feel its necessary to comment or propose solutions, or it is a very complicated issue, possibly impossible, or at least something no one else seem to need (or know about) a solution to.

Well, because I use javascript to activate the OnClick event of a button when hitting a certain key on the keyboard, I need it and now I've found an alternative, at least to the part of knowing which control is the active one, when the OnClick event is fired:

I've created a global (on form level) variable called ActCon.
I've assigned a unique value to the tag property of the edit controls I want to check, and to the tag property of the button.
I've created ONE common OnEnter event for all the Edit controls, and the button, in which I assign the tag value of the "sender" to the global var ActCon.
In the OnClick event of the button, I check the value of ActCon and then know which control has focus when the OnClick event i fired, and can take appropriate action.

NB! I'll be happy to make a small test program if anybody will want an actual example.

Regards
Soren
Reply
#3
(12-09-2019, 09:19 AM)SorenJensen Wrote: Hi Guys,

As there have been no replies to this question, I take it it is either a very trivial question, which everybody with just the slightest Delphi knowledge should know about and therefore no one feel its necessary to comment or propose solutions, or it is a very complicated issue, possibly impossible, or at least something no one else seem to need (or know about) a solution to.

Well, because I use javascript to activate the OnClick event of a button when hitting a certain key on the keyboard, I need it and now I've found an alternative, at least to the part of knowing which control is the active one, when the OnClick event is fired:

I've created a global (on form level) variable called ActCon.
I've assigned a unique value to the tag property of the edit controls I want to check, and to the tag property of the button.
I've created ONE common OnEnter event for all the Edit controls, and the button, in which I assign the tag value of the "sender" to the global var ActCon.
In the OnClick event of the button, I check the value of ActCon and then know which control has focus when the OnClick event i fired, and can take appropriate action.

NB! I'll be happy to make a small test program if anybody will want an actual example.

Regards
Soren
Hi Soren,
I will appreciate a code sample. It is something I have been trying to solve for some time..

TIA
Reply
#4
Hi Tia,

Attached a small Demo showing how to do. You can also follow the below description as a step-by-step guide, if you prefer to create your own demo. Please note that the Keyboard key I use is F10, so when running the program, pressing F10 will execute the OnClick event of te BTNTEST button, without the cursor leaving the control which has focus, and therefore without that controls OnExit event being executed.

To create a small test program to test which control have focus when a button’s onClick event is activated,  follow these steps:

1. Create a new SA project, Indy or http. Name it and place it where you want.

2. On the form of Unit1, put some controls, capable of having focus, like an IWEdit, IWCheckBox, IWListbox and an IW Button. It’s not important how many, or which controls, you have as long as they can receive focus.

3. Put ONE button to be the one to host the OnAsyncClick event. Name this button something special, like BTNTEST. You will need the name in the Javascript activating it.

4. For each of the controls you want to be able to check, including BTNTEST, add a unique number to the controls TAG property. This value will tell you which control is the active one, so make sure they all have a unique value.

5. Create a global variable of type integer. Name it what you want, e.g. “ActControl”.

6. Create an OnAsyncEnter event for one of the controls on the form, like the IWEdit. Put the following code in the event:

ActControl := (sender as TIWCustomControl).tag;

7. For all the other controls, add to them the same OnAsyncEnter event, so that they all share it. Including the BTNTEST button.

NOTE: When the BTNTEST’s OnAsyncClick event is fired, the value of ActControl will be the tag value of the last entered control. If it is zero, it is one of the controls you have not given any value, and if is the same value as the BTNTEST button itself, the OnAsyncClick event was fired by pressing the BTNTEST button. (or at least the BTNTEST had focus when the OnAsyncClick event was fired)

If however, the value is any other of the possible tag values, the OnClick event was fired either by presseing a key, or key combination, on the keyboard as in my case, or by calling the event from any other part of your program. 

NOW…. Whenever the OnAsyncClick event of the BTNTEST is fired, a simple test like:

If ActControl <> BTNTEST.tag then {dosomething}

Or

Case ActControl of
0 : exit;  // if its a control without a tag value, don't do anything
1 : {do the IWEdit1 OnExit event}
2 : {do other controls onexit event};
4 : Exit;  // in my case 4 is the tag value of the BTNTEST button, so no need to do anything
End;

And to get a Keypress (F10 in my case) or Key combination pressed, on the keyboard to activate the BTNTEST.OnClick event, add the following code to the IWForm JavaScript property:


function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

window.addEventListener("keydown", function (event)
{
  if (event.defaultPrevented)
  {
    return; // Do nothing if the event was already processed
  }
  switch (event.keyCode)
  {
    case 121: // F10 key
      event.returnValue = false;
      event.keyCode = 0;
    sleep(100);
      $("#BTNTEST").click();
      break;
    case 27: // ESC key
      event.returnValue = false;
      event.keyCode = 0;
    sleep(100);
      $("#BTNCLOSE").click();
      break;
    default:
      return; // Quit when this doesn't handle the key event.
  }
  // Cancel the default action to avoid it being handled twice
  event.preventDefault();
},
true);


That’s it. Compile and run…. 


Attached Files
.zip   ActiveControlDemo.zip (Size: 3.79 KB / Downloads: 3)
Reply
#5
(08-15-2019, 02:58 PM)SorenJensen Wrote: Hi all,

In VCL it is possible to find out which control is active, and with before and after events, also to tell what the next control to receive focus is, or which control had the focus before.

I used that to bypass some activities in events like OnEnter and OnExit, only doing certain things, if the previous or next active control, was the expected one.

In Intraweb there is an ActiveControl property of the IWForm, but it do not seem to get updated as the focus shifts from control to control. In the IWForm documentation is says it can be used to set the active control when the form is rendered. I suppose it is like the taborder value.

Are there any other way of getting info about which control is the active one ?

Regards
Soren

ActiveControl property is not updated and, in that case, it doesn't make sense to update it. ActiveControl is a static property meaning the control you want to make active each time the form is rendered (Which can happen several times during the form life cycle). Keeping track of every control focus change is somewhat expensive because several requests need to be processed by the server just to keep this property updated.
Reply
#6
Although for what its worth, 17 tracks it client to server and back. But its more of a side effect than a feature.
Reply
#7
Большое вам спасибо за этот пример. Я просто искал, как обнаружить и обработать нажатия клавиш. Очень полезно!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)