none
Powershell - Help with returning dynamic variable values RRS feed

  • Question

  • Add-Type -AssemblyName System.Windows.Forms
    $frm1 = New-Object System.Windows.Forms.form
    $frm1.Name = "Hey"
    
    $flw1 = New-Object System.Windows.Forms.FlowLayoutPanel
    $flw1.flowdirection = 'TopDown'
    
    $frm1.controls.add($flw1)
    
    $m = "apple","orange","banana"
    
    for($i=0;$i -le $m.Length-1;$i++){
    
    $lb = New-Object System.Windows.Forms.linkLabel
    $lb.text = $m[$i]
    $m[$i] = $null
    $flw1.Controls.Add($lb)
    $lb.add_Click({Write-Host $lb.text})
    }
    
    $frm1.showdialog()
    Could someone please help me with a method to return the value of the label that was clicked? Or a means to identify which linklabel was clicked. Thank you.
    Monday, May 12, 2014 1:43 PM

Answers

  • Place the same click event on the label.  The event will tell you which one was clicked.


    ¯\_(ツ)_/¯

    • Marked as answer by L.T.S Monday, May 12, 2014 2:11 PM
    Monday, May 12, 2014 1:52 PM
  • You cannot return anything from an open dialog.  The dialog must be closed and the calling script must extract the value.

    Assumimg you knew about scripting in PowerShell I gave you the answer.  THis thread is really about a different topic.  Please start a new topic with your full question aand I wil try to point you at how to work with PowerShell and forms.


    ¯\_(ツ)_/¯

    • Marked as answer by L.T.S Tuesday, May 13, 2014 1:45 PM
    Monday, May 12, 2014 8:46 PM

All replies

  • Place the same click event on the label.  The event will tell you which one was clicked.


    ¯\_(ツ)_/¯

    • Marked as answer by L.T.S Monday, May 12, 2014 2:11 PM
    Monday, May 12, 2014 1:52 PM
  • Sorry - you have to do it this way:

    $lb.add_Click({Write-Host $this.text})

    Notice we use the $this object to reference the eventing control.


    ¯\_(ツ)_/¯

    Monday, May 12, 2014 1:54 PM
  • Thank you so very much, jrv.

    Monday, May 12, 2014 2:12 PM
  • You are welcome. That is not well documented so try it when you think it will work.


    ¯\_(ツ)_/¯

    Monday, May 12, 2014 2:31 PM
  • Hi, jrv-

    Awfully sorry to trouble you. Could you please assist with the solution to this script or a better way to approach this scenario? I need to return the current value of label ($lb) from the form ($frm1), when $btnfrm2 on form ($frm2) is clicked. Thank you.

    Add-Type -AssemblyName System.Windows.Forms
    $frm1 = New-Object System.Windows.Forms.form
    $frm1.Name = "Hey"
    
    $flw1 = New-Object System.Windows.Forms.FlowLayoutPanel
    $flw1.flowdirection = 'TopDown'
    
    $frm1.controls.add($flw1)
    
    $m = "apple","orange","banana"
    
    function frmDisplay{
    
    $frm2 = New-Object System.Windows.Forms.Form
    $btnfrm2 = New-Object System.Windows.Forms.Button
    #$btnfrm2.Add_Click()
    $btnfrm2.text = 'Click'
    $frm2.Controls.Add($btnfrm2)
    
    $frm2.Add_Shown({$frm2.Activate()})
    	[void] $frm2.ShowDialog()
    }
    
    for($i=0;$i -le $m.Length-1;$i++){
    
    $lb = New-Object System.Windows.Forms.linkLabel
    $lb.text = $m[$i]
    $m[$i] = $null
    $flw1.Controls.Add($lb)
    $lb.add_Click({Write-Host $this.text; (frmDisplay)})
    }
    
    $frm1.showdialog()
    


    • Edited by L.T.S Monday, May 12, 2014 8:01 PM Clarity
    Monday, May 12, 2014 8:00 PM
  • Set a hidden control with the value and retrieve it after the from closes.

    if($frm2.ShowDialog() -eq 'OK'){
         $result=$frm2Controls['hidctl'].Text
    }


    ¯\_(ツ)_/¯

    Monday, May 12, 2014 8:16 PM
  • Thank you for the reply. I receive an error when I try to run it. Could you show me with the script I posted? I would like to have $frm2 remain open after $btnfrm2 is clicked and it returns the current value of the label $lb from form $frm1.

    Owe you lunch if you are ever in the Tulsa area.

    Monday, May 12, 2014 8:36 PM
  • You cannot return anything from an open dialog.  The dialog must be closed and the calling script must extract the value.

    Assumimg you knew about scripting in PowerShell I gave you the answer.  THis thread is really about a different topic.  Please start a new topic with your full question aand I wil try to point you at how to work with PowerShell and forms.


    ¯\_(ツ)_/¯

    • Marked as answer by L.T.S Tuesday, May 13, 2014 1:45 PM
    Monday, May 12, 2014 8:46 PM
  • Once again, thank you. I got the Powershell script to do what I needed it to do - returning the desired value with the form open.  I appreciate your help. Have a good day.
    Tuesday, May 13, 2014 1:45 PM
  • Once again, thank you. I got the Powershell script to do what I needed it to do - returning the desired value with the form open.  I appreciate your help. Have a good day.

    I am sure I don't know  what you are asking or are trying to do.  I suspect it is the wording "return a variable".

    We can output with Write-Host but that is not returning a variable and what is output cannot be used.  It can only be displayed.

    Is this what you are doing?


    ¯\_(ツ)_/¯

    Tuesday, May 13, 2014 3:05 PM
  • To clarify:

    $x=$frm.ShowDialog()

    The contents of $s will be Null, OK or Cancel.  They will not be available until the form closes.  Anything else output by the form will only be text displayed on the console screen which is not "returning" anything it is just simple text output.


    ¯\_(ツ)_/¯

    Tuesday, May 13, 2014 3:08 PM