WinForm的全局变量

时间:2018-05-14 12:46:25

标签: function powershell user-interface global-variables

我目前正在尝试使用一个函数中的日期/时间选择器变量的值,并在另一个函数中多次使用它。日期/时间选择器位于表单上,因此当用户设置开始日期和结束日期时,这些值将分配给要在另一个函数中使用的变量,该变量应至少调用这两个变量两次。在尝试从第一个函数调用该变量时,它包含一个空值,即使它已分配给变量。我试图保持在一个函数中创建的值多次在另一个函数中使用。谢谢。

 Set-strictMode -off
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
    [System.Windows.Forms.Application]::EnableVisualStyles()
    Add-Type -AssemblyName System.Web
    [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

   $global:startDate=$null
$global:endDate=$null

function MakeForm{

#region begin GUI
$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '396,180'
$Form.text                       = "Rexpii Integrations Version (Beta)"
$Form.BackColor                  = "#ffffff"
$Form.TopMost                    = $False
$Form.Icon= [System.Drawing.Icon]::ExtractAssociatedIcon('C:\files\scripts\tpgicon.ico')

$btnSubmit                         = New-Object system.Windows.Forms.Button
$btnSubmit.text                    = "Submit"
$btnSubmit.width                   = 60
$btnSubmit.height                  = 30
$btnSubmit.location                = New-Object System.Drawing.Point(220,116)
$btnSubmit.Font                    = 'Microsoft Sans Serif,10'
$btnSubmit.Add_Click({
    $global:startDate= $Global:txtStartDate.Value.ToString("yyyy-MM-dd")
                  $global:endDate= $Global:txtEndDate.Value.ToString("yyyy-MM-dd")

             GetData
             $Form.Close()})

$btnCancel                         = New-Object system.Windows.Forms.Button
$btnCancel.text                    = "Cancel"
$btnCancel.width                   = 60
$btnCancel.height                  = 30
$btnCancel.location                = New-Object System.Drawing.Point(288,116)
$btnCancel.Font                    = 'Microsoft Sans Serif,10'
$btnCancel.Add_Click({
            $Form.Close()
                })
$Global:txtStartDate                       = New-Object system.Windows.Forms.DateTimePicker
$Global:txtStartDate.width                 = 150
$Global:txtStartDate.location              = New-Object System.Drawing.Point(208,40)
$Global:txtStartDate.Format = "Custom"
$Global:txtstartDate.CustomFormat = "yyyy-MM-dd"


$Global:txtEndDate                         = New-Object system.Windows.Forms.DateTimePicker
$Global:txtEndDate.width                   = 150
$Global:txtEndDate.location                = New-Object System.Drawing.Point(208,80)
$Global:txtEndDate.Format="Custom"
$Global:txtEndDate.CustomFormat = "yyyy-MM-dd"
$Form.controls.AddRange(@($btnSubmit,$btnCancel,$PictureBox1,$PictureBox2,$StartDate,$EndDate,$lblStartDate,$lblEndDate,$txtStartDate, $txtEndDate))

[void]$Form.ShowDialog()

}

MakeForm

function GetData{ 
#my scripts number one
Write-Output "This is the first $global:startDate to $global:endDate example"

#my scripts number two
Write-Output "This is the second $global:startDate to $global:endDate example"

}

1 个答案:

答案 0 :(得分:0)

首先,您需要先定义函数,然后才能使用它们。您正在尝试在定义之前调用GetData。将调用移至MakeForm(也显示表单)到GetData的函数定义之后。

其次,应更改GetData以调用Write-Host而不是Write-Output,以便您可以看到事件处理程序的输出。如果您在显示表单后调用GetData,您将看到全局变量设置正确。

因此,脚本的最后一部分应如下所示:

function GetData{ 
    #my scripts number one
    Write-Host "This is the first $global:startDate to $global:endDate example"

    #my scripts number two
    Write-Host "This is the second $global:startDate to $global:endDate example"
}

MakeForm

# Call GetData again to verify that the globals are set.
GetData