passing variable to another event

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 4 years and 3 days 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
ausman
Posts: 7
Last visit: Fri Jan 21, 2022 6:14 am

passing variable to another event

Post by ausman »

Folks:
New to PS studio Gui. I am trying to pass variable\parameter to another event. For example i have Two button, Hello and World with label. Now when i click on Hello button, its shows Hello in label (as attach shows). Now i want to take Hello variable to world event and when click world button, should shows Hello world. I am not able to pass variable hello to world event. just need you folks help to understand...thanks for your help in advance.
Attachments
hello.png
hello.png (44.1 KiB) Viewed 7178 times
User avatar
ausman
Posts: 7
Last visit: Fri Jan 21, 2022 6:14 am

Re: passing variable to another event

Post by ausman »

$form1_Load={
#TODO: Initialize Form Controls here

}

$buttonHello_Click={
#TODO: Place custom script here
$hello = "Saying Hello "
$lbl.Text= $hello
}

$lbl_Click={
#TODO: Place custom script here

}

$buttonWorld_Click={
#TODO: Place custom script here
$world = "World"
$lbl.Text = $hello
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: passing variable to another event

Post by jvierra »

Simple - just add then up.

Code: Select all

$buttonWorld_Click={
    #TODO: Place custom script here
    $world = "World"
    $lbl.Text += $world
}
User avatar
ausman
Posts: 7
Last visit: Fri Jan 21, 2022 6:14 am

Re: passing variable to another event

Post by ausman »

thx for your quick respond.
My goal here is how to pass variable from one event to different, showing in txtbox or label is to just show...
I am creating Azure admin tool, so if admin will click verify button, it does some stuff and create variable. This variable i want to be available to different events to call if another button press.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: passing variable to another event

Post by jvierra »

You cannot pass variables between events. The system owns the events and handles all parameters.
To easily share data in a form you can save the data in a control or save it is a script scope variable.
User avatar
ausman
Posts: 7
Last visit: Fri Jan 21, 2022 6:14 am

Re: passing variable to another event

Post by ausman »

"To easily share data in a form you can save the data in a control or save it is a script scope variable"

any example you could share?

down below i would like to pass $ObjectId variable from $buttonHello to $buttonWorld and run $Results command
===============================================
$buttonHello_Click={
#TODO: Place custom script here
$ObjectId = $(Get-AzureADUser -Filter "UserPrincipalName eq '$EmpsUPN'").ObjectId
$textbox1.Text= $ObjectId
}



$buttonWorld_Click={
#TODO: Place custom script here
$world = "World"

$Results= get-AzureADUserManager -ObjectId $ObjectId -RefObjectId "test1"

$textbox1.Text += $world
}
===========================================
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: passing variable to another event

Post by jvierra »

Here is the correct way to code something like this in Forms.

Code: Select all

$buttonHello_Click={
    $textbox1.Text = (Get-AzureADUser -Filter "UserPrincipalName eq '$EmpsUPN'").ObjectId
}

$buttonWorld_Click={
    $results = get-AzureADUserManager -ObjectId $textbox1.Text -RefObjectId test1
}
Avoid over-quoting things and avoid using syntactic constructs without understanding what they do.
User avatar
ausman
Posts: 7
Last visit: Fri Jan 21, 2022 6:14 am

Re: passing variable to another event

Post by ausman »

those are are good points but i am just testing variables with events.

So in my real gui what i am trying to build when button verification click, it runs some command in Azure and shows in label. these commands get three values in three different variables. Now, if user click another button to execute, three values variables used in this event to execute some other commands.
So, questions would be whatif more then one variables needs to pass to events?
really appreciate your help..thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: passing variable to another event

Post by jvierra »

I have absolutely no idea what you are trying to ask. You need to learn enough PowerShell to be able to ask a basic question.
To persist variables in an event use scoping to set the scope to "script" or "global" and the variables will be available in any other event.

help about_scopes
This topic is 4 years and 3 days 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