Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active control question
#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


Messages In This Thread
Active control question - by SorenJensen - 08-15-2019, 02:58 PM
RE: Active control question - by SorenJensen - 12-09-2019, 09:19 AM
RE: Active control question - by zsleo - 12-09-2019, 06:43 PM
RE: Active control question - by SorenJensen - 12-10-2019, 01:08 PM
RE: Active control question - by kudzu - 12-12-2019, 02:34 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)