来自 WinForm 的全局变量



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

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:filesscriptstpgicon.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"
}

首先,您需要先定义函数,然后才能使用它们。您正在尝试在定义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

相关内容

  • 没有找到相关文章

最新更新