in Education by
I'm writing a script for a user that concatenates several PDF files and appends tabular data as a text file. Now the problem is that the user can manually name the text files so that the script has to verify which PDF file belongs to the data. Usually it would do so by name, but since the user can (and will obviously) change the name of the PDF, the script shall ask the user to pick the correct PDF to merge. I've written a function in my script, that uses Winforms to display a Listbox with the available PDF files and the user should pick one. function Select-Rechnung { param ( [string] $Rechnung, [string[]] $PdfFiles ) $form = New-Object System.Windows.Forms.Form $form.Text = "Rechnung wählen" $form.Size = New-Object System.Drawing.Size(640,320) $form.StartPosition = "CenterScreen" $form.KeyPreview = $true $form.Add_KeyDown({if($_.KeyCode -eq "Enter") { $x = $PdfFiles[$ListBox.SelectedIndex]; $form.Close() }}) $form.Add_KeyDown({if($_.KeyCode -eq "Escape") { $form.Close() }}) $OkButton = New-Object System.Windows.Forms.Button $OkButton.Location = New-Object System.Drawing.Size(240,240) $OkButton.Size = New-Object System.Drawing.Size(75,23) $OkButton.Text = "OK" $OkButton.Add_Click({ $x = $PdfFiles[$ListBox.SelectedIndex]; $form.Close() }) $form.Controls.Add($OkButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(325,240) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Abbrechen" $CancelButton.Add_Click({$form.Close()}) $form.Controls.Add($CancelButton) $Label = New-Object System.Windows.Forms.Label $Label.Location = New-Object System.Drawing.Size(10,20) $Label.Size = New-Object System.Drawing.Size(600,20) $Label.Text = [string]::Format("Für die Rechnung {0} wurden mehrere mögliche Dateien gefunden. Bitte auswählen:", $Rechnung) $form.Controls.Add($Label) $ListBox = New-Object System.Windows.Forms.ListBox $ListBox.Location = New-Object System.Drawing.Size(10,40) $ListBox.Size = New-Object System.Drawing.Size(600, 20) $ListBox.Height = 200 foreach($pdfFile in $PdfFiles) { [void] $ListBox.Items.Add($pdfFile) } $form.Controls.Add($ListBox) $form.TopMost = $true $form.Add_Shown({$form.Activate()}) [void] $form.ShowDialog() $x } Now within the KeyDown handler or the Click handler, the function should assign the selected PDF file to the variable $x. I've checked that the $PdfFiles are correctly handed to the function and that during the handlers execution, the $PdfFiles[$ListBox.SelectedIndex] actually has the correct string value. However, when I access $x after the forms ShowDialog has been processed, it is empty and thus the function's return value is empty. Why won't it assign the value (that it correctly evaluates during the handler) to my variable and return it? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try it by introducing a reference type like a hashtable and use it to modify and return the value like so function Select-Rechnung { param ( [string] $Rechnung, [string[]] $PdfFiles ) $x = @{ Value = '' } ... $OkButton.Add_Click({ $x.Value = $PdfFiles[$ListBox.SelectedIndex]; ... $x.Value } You could also achieve your goal by accessing the $x variable of the outer scope with Set-Variable -Name x -Scope 1 $PdfFiles[$ListBox.SelectedIndex] or one of the other scope modifiers (global or script - see about scopes) if you want to avoid the extra hash table.

Related questions

0 votes
    I am writing down a simple schedule planner app using WinForms in Visual Basic .NET, and I have this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a problem where a Web Application needs to (after interaction from the user via Javascript) 1) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    How do I implement a Copy menu item in a Windows application written in C#/.NET 2.0? I want to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    If a user hits enter in a windows forms textbox with a KeyUp Event, windows sounds a beep or ding ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III 3. I, II, III, IV 4. I, II, IV...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III C) I, II, III, IV D) I, II, IV...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    I am using module jsonwebtoken 8.4.0 in a nodejs 10.2.0 app. A JWT token is generated on https: ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    The driving point impedance of an LC network is given by Z(s)=(2s^5+12s^3+16s)/(s^4+4s^2+3). ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    The driving point impedance of an LC network is given by Z(s)=(2s^5+12s^3+16s)/(s^4+4s^2+3). ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    The driving point impedance of an LC network is given by Z(s)=(2s^5+12s^3+16s)/(s^4+4s^2+3). ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    The driving point impedance of an LC network is given by Z(s)=(2s^5+12s^3+16s)/(s^4+4s^2+3). ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    The driving point impedance of an LC network is given by Z(s)=(2s^5+12s^3+16s)/(s^4+4s^2+3). ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
...