Gui, tabs and its handler, how to do it correctly?


I’m fiarly new to Powershell and totaly new to GUI building in PS.
I have a Winform created and i have 3 tabs.
Now each tab has it’s own little program. Each tab connects to a DB or to AD.
So for startup performance, i only load these functions when the tab is selected.
But i’m doing it wrong (although it works). I’m writing this post to ask to you guys: how should it be done?

         # Create tabControl and Tabs
         $TabControl = New-object System.Windows.Forms.TabControl
         $ADTab = New-Object System.Windows.Forms.TabPage
         $KayakoTab = New-Object System.Windows.Forms.TabPage
        $WieIsWieTab = New-Object System.Windows.Forms.TabPage
        #Tab Control 
        $tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
        $tabControl.Location = $System_Drawing_Point
        $tabControl.Name = "tabControl"
When the program is loaded, i do the following:
        $KayakoTabWasLoaded = 0
        # I set the counter to 0 as the tab is not yet been selected
                $handler_tabpage_SelectedIndexChanged_KayakoTab = {
        	            if($KayakoTabWasLoaded -lt 1){ # if the tab has not yet been selected, include the file with the needed functions
        		        if ($TabControl.SelectedTab.Name -eq "KayakoUser")	{
        			        Write-Host "You have selected the tab KayakoTab for the first time, file got included."
        			        . .\include\kayako_tab.ps1				
        	        }else{ Write-Host "File kayako_tab.ps1 already included."} # the tab was selected before so do nothing

But by creating these handlers, the include is done each time an other tab is selected.
Therefore i created the $KayakoTabWasLoaded variable.

But i feel like this can’t be the correct way of doing this.
If tried to create a handler one level higher (so on the Tabcontrol in stead of the tab) but that didn’t work (might have done it wrong).
Can someone tell me the correct way ?

Kind regards

Hey cm it,

When you try and define it at the higher level, are you also including the actual code for the event as well? Otherwise you would be setting the event handler to $null.


$handler_tabpage_SelectedIndexChanged_KayakoTab = {bla bla bla}