OnPaint script not working


Recommended Posts

I want to set a variable whenever a certain element is drawn (at least every time its page is loaded). I've tried putting it in the OnLoad and OnPaint of the element but it never seems to run. I've tried switching to other pages and back, shutting down and restarting DAQ Factory, but I can't seem to trigger the events. The same assignment works fine if I put it in the action of the element and click on it.

Could you go into some detail on when OnLoad, OnPaint and so on are executed?

The User Guide description is pretty brief, plus I think I'm misunderstanding something it says because it seems like my script should be executing, the way I'm interpreting what it says.

Link to comment
Share on other sites

OnLoad only gets executed when the component first gets instantiated. It will only get executed once. If you change the code in it, you'll either need to save and reload the document, or call the component's RedoLoadEvent() function which will retrigger the load event (and is designed so you don't have to reload the whole document just to reinit the component). The OnPaint is called whenever the control gets painted.

I've attached a dead simple sample. In OnLoad, it initializes a global variable (I suppose I could have made it local to the component), then prints OnLoad to the command/alert. OnPaint increments that variable each iteration, and prints OnPaint: and the current count. Try switching to other pages and you'll see the OnPaint stop.

events.ctl

Link to comment
Share on other sites

Actually you don't need to name the component. I did it just so I could call the RedoLoadEvent(). It worked fine even before I did that.

As for an event for the screen, the easiest thing to do is simply create a sequence, call it maybe: "switchToPageX" with script like this:

page.strCurrentPage = "pageX"

// put event code here

Then just call the sequence as a function to switch to that page. There are slicker ways of doing it if you've got lots of pages.

Link to comment
Share on other sites

"Actually you don't need to name the component. I did it just so I could call the RedoLoadEvent(). It worked fine even before I did that."

Strange. Mine started working and I had changed nothing but named the component. [shrug]

"As for an event for the screen, the easiest thing to do is simply create a sequence, call it maybe: "switchToPageX" with script like this:"

I had thought of that, but it felt a little awkward so I didn't want to do it. What are the "slicker ways" you mentioned?

Link to comment
Share on other sites

Use a class for each page, and a class to hold all these page objects. These are essentially wrapper classes for the internal page objects but have events as well. Something like this:

class CPage0

local string pageName = "page_0"

function OnDisplay()

? "displaying " + pageName

endfunction

endclass

class CPage1
   local string pageName = "page_1"
   function OnDisplay()
	 ? "displaying " + pageName
   endfunction
endclass

class CPages
   local pageList

   function init()
	  pageList.append(new(CPage0))
	  pageList.append(new(CPage1))
   endfunction

   function changePage(string newPage)
	  private index = search(compareNoCase(pageList.pageName, newPage) == 0)
	  if (index >= 0)
		 pageList[index].OnDisplay()
	  endif
	  page.strCurrentPage = newPage
   endfunction
endclass

Just instantiate CPages in startup and call its init():

global pages = new(CPages)

pages.init()

Whenever you want to change pages, call changePage:

pages.changePage("Page_0")

Note that if you call this function with a page that doesn't have a corresponding class, it will still change the page, it just won't try and call the event. Alas, the way its written it won't work with overlaid pages (or at least the event part won't). To fix that, you'd have to modify the search.

Finally, you can expand on this to add more functionality. For example, you could make it easy to do a back button. In this case, add a variable to CPages to store a stack with the last pages changed. Then push to the stack whenever changePage() is called, and create another function to go back:

class CPage0
   local string pageName = "page_0"
   function OnDisplay()
	 ? "displaying " + pageName
   endfunction
endclass

class CPage1
   local string pageName = "page_1"
   function OnDisplay()
	 ? "displaying " + pageName
   endfunction
endclass

class CPages
   local pageList
   local string pageStack

   function init()
	  pageList.append(new(CPage0))
	  pageList.append(new(CPage1))
   endfunction

   function changePage(string newPage)
	  callOnDisplay(newPage)
	  pageStack.push(page.strCurrentPage)
	  page.strCurrentPage = newPage
   endfunction

   function callOnDisplay(string newPage)
	  private index = search(compareNoCase(pageList.pageName, newPage) == 0)
	  if (index >= 0)
		 pageList[index].OnDisplay()
	  endif
   endfunction

   function goBack()
	  private string lastPage = pageStack.pop()
	  if (!isempty(lastPage))
		 callOnDisplay(lastPage)
		 page.strCurrentPage = lastPage
	  endif
   endfunction
endclass

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.