issue with get-variable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

issue with get-variable

Post by Domtar »

hi all,

i have a project with multiple files and hundreds of lines of code so i cannot paste everything here but I'll try to describe my issue as best I can to allow someone to help.

the GUI I'm building has a new tabpage each time you connect to a computer in the field. the tab is correctly creates and works great.

when the tab is created, I create a new variable that is used to indicate if that tab already exists, to make sure the user does not connect to the same computer twice. when he/she clicks connect, a new tab appears, where I'll build all the logic and controls i need.

anyway, in this function I'm pasting here, you can see the process, if the variable does not exists, i create a tabpage, set the variable, and if it exists, i set the focus to the existing tabpage.

my issue is that, when i need to close the tabpage, i need to remove the variable, but my function says it cannot find a variable with that name. i hope someone can understand this and helps!

thanks!

Code: Select all

function create-newTab {
	[CmdletBinding()]
	param (
		$pcname
	)
	
	$button_click = {
		$varName = ($this.Text -split ' ')[1]
		Get-Variable -name $varName | Remove-Variable -Force 
		$tabControl1.TabPages.Remove($tabControl1.SelectedTab)
	}
	
	new-variable -Name "$pcname" -Scope global
	$tabcontrol1.TabPages.Add($comboPCName.Text, $comboPCName.Text)
	
	$button = New-Object System.Windows.Forms.Button
	$button.Text = "Close $pcname"
	$button.Parent = $tabcontrol1.TabPages[$pcname]
	$button.Location = '794, 5'
	$button.Size = '42, 23'
	$button.add_Click($button_Click)
	$tabcontrol1.TabPages[$pcname].Controls.Add($thisbutton)
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: issue with get-variable

Post by jvierra »

You cannot remove a tab this way.

If the code is running from a control in the tab, then you know the tab as it is the "parent" of the control that is executing the event - the "$this" variable. "$this.Parent" is the tab. "$this.Parent.Parent" references the tab control. By doing it this way the code can still be used on any tab control without assuming any names or variables. The code is portable and easier to change, maintain and debug. This is a fundamental of all Windows objects anywhere in the Windows operating system.

Generally, it is not necessary to keep any global or script-level variables. The TabControl contains all objects, and they can be detected in any event. To match a PC name to a tab just make the tab name the PC name so you can request the tab from the tab collection by PC name.

Here is a quick and untested example.
  1. function create-newTab {
  2.     [CmdletBinding()]
  3.     param (
  4.         [System.Windows.Forms.TabControl]$TabCtrll,
  5.         [string]$pcSamAccountName
  6.     )
  7.    
  8.     # add button to TabPageCollection
  9.     $TabCtrll.TabPages.Add($pcSamAccountName, $pcSamAccountName)
  10.     $button = New-Object System.Windows.Forms.Button
  11.    
  12.     # configure button
  13.     $TabCtrll.TabPages[$pcSamAccountName].Controls.Add($button)
  14.     $button.Text = "Close $pcSamAccountName"
  15.     $button.Location = '794,5'
  16.     $button.Size = '42,23'
  17.    
  18.     # add button event to remove the current tabpage from the tab page collection
  19.     $button.add_Click({
  20.             $this.Parent.Parent.TabPages.Remove($this.parent)
  21.         }
  22.     )
  23. }
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: issue with get-variable

Post by Domtar »

you missed the point. i can remove the tab. it's the variable that i can't remove.

the variable exists only if a connection to a computer was done before.

get-variable -name x | remove-variable

this one gives me an error even though i can see the variable if i pipe it to the log status bar.

pseudo-code:

-user asks for a connection to a computer
-does the variable exists? yes, select the existing tab. no, create a new tab and create the variable
-user closes a tab
-script closes tab & remove variable

thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: issue with get-variable

Post by jvierra »

As I noted, you can't remove a variable that references active object from event code in the object. That is why I gave you an example of how to not create unnecessary variables. Control objects are collections of variables. There is no need to create an external variable.

Who does the user ask?
How does the computer know that the user has asked for a connection?

The process has to have some specific context before anything else can be decided.

Once you know the "SamAccountName" of the computer then you can check the tab page collection for the tab. Control collections are specifically designed to work this way. Really complex controls have a "Find" method of some kind. Keyed collections are easier when you have the key (SamAccountName) to index the collection - "TabPages[$SamAccountName]" will return the tab or null if the tab does not exist.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: issue with get-variable

Post by Domtar »

yeah it makes sense now. I'll give this a try.

thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: issue with get-variable

Post by jvierra »

I had some time, so I built a small demo of how to add and remove tabs. Note that there is only on temporary variable, and it create a new tab or display the tab associated with the name in the text box. Change the name and create a couple of new tabs and then type in the name of an existing tab and it will display the tab instead of creating a new tab. This only takes one line of code.

See the following PSF.
Attachments
Demo-TabAddRemove.psf
(18.92 KiB) Downloaded 34 times
This topic is 1 year and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked