Creating controls on the fly

It’s possible to create controls at runtime. This can be useful if you want to display some information temporarily without affecting the current form. The control gets created completely through code, which is certainly not as nice as having the IDE do it for you, especially with more complex objects.

In this example, we’ll create a div object. Div is one of the simplest elements that lies underneath NS Basic/App Studio objects. In fact, the HTMLView control is actually just a simple div.

Here’s the code:

  Dim newdiv
  newdiv = document.createElement("div")
  newdiv.setAttribute("id", "myNewControl")
  newdiv.style.position = "absolute"
  newdiv.style.width = "200px"
  newdiv.style.height = "40px"
  newdiv.style.left = "55px"
  newdiv.style.top = "200px"
  newdiv.style.background = "white" 
  newdiv.style.border = "1px solid #000";
  document.body.appendChild(newdiv)

Now we can set its value just like we would an HTMLview:

  myNewControl.innerHTML="My new control"

To get rid of the control, do the following:

  myNewControl.parentNode.removeChild(myNewControl)

The CreateDiv sample is included starting with NS Basic/App Studio 1.1.3.

Edited 3/28/16 – fixed some errors in code