How to execute code when form is loaded/shown?

Probably a newbie question :slight_smile: :

I want to execute specific code when a form is loaded/shown

The expected onload/onshow events don’t appear to be triggered.

Example

In the below example

Form1

 Button1.onclick=function(){
    NSB.Print("Button1.onclick") ;
    Form1.hide();
    Form2.show();
 }

Form1.onload=function(){
   NSB.Print("Form1.onload");
}

Form1.onshow=function(){
  NSB.Print("Form1.onshow");
}

Form2

Form2.onload = function() {
  NSB.Print("Form2.onload");
}

Form2.onshow = function() {
  NSB.Print("Form2.onshow");
}

Button2.onclick = function() {
  NSB.Print("Button2.onclick");
  Form2.hide();
  Form1.show();

}

Generates:

When I click on button1 and button2 sequence several times

<image001.png>

Note Form1.onshow shows when sample is started but any subsequent onload/onshow event doesn’t seem to be triggered.

What is the recommended way of achieving initialization each time a form is loaded / shown??

Any suggestion or pointer to Wiki/Reference docs would be appreciated (i.e checked but couldn’t find an answer other than the caveat that

The load event can’t be used for individual forms or controls….
which probably explains why the onload isn’t triggered.


Related Topic: [On form loaded event]

Try using ChangeForm() to do this. That’s what calls the onshow() function.

https://wiki.nsbasic.com/ChangeForm

Thanks… works like a charm.:smiley: