WinForm 第一次没有加载

WinForm not loading the first time

我正在使用 PowerShell WinForms 编写程序。 例如,程序的“登录”部分。

#CREDENTIAL CREATE - LOGIN AND PASSWORD INPUT
############################################


#APP FOUNDATION
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


#Login_Form
$Login_Form                                         = New-Object -TypeName System.Windows.Forms.Form
$Login_Form.ClientSize                              = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]340,[System.Int32]110))
$Login_Form.Text                                    = [System.String]'Login_Form'
$Login_Form.add_Load($Login_Form_Load)
$Login_Form.ResumeLayout($false)
$Login_Form.PerformLayout()
$Login_Form.SuspendLayout()
$Login_Form.FormBorderStyle                         = "FixedDialog"

#Login_Label_User
$Login_Label_User                                   = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_User.Location                          = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]9))
$Login_Label_User.Name                              = [System.String]'Login_Label_User'
$Login_Label_User.Size                              = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]100,[System.Int32]18))
$Login_Label_User.TabIndex                          = [System.Int32]3
$Login_Label_User.Text                              = [System.String]'Login:'
$Login_Label_User.UseCompatibleTextRendering        = $true

#Login_TextBox_User
$Login_TextBox_User                                 = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_User.Location                        = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]30))
$Login_TextBox_User.Name                            = [System.String]'Login_TextBox_User'
$Login_TextBox_User.Size                            = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]224,[System.Int32]21))
$Login_TextBox_User.TabIndex                        = [System.Int32]0
$Login_TextBox_User.Text                            = ((Get-ADDomain).name + "\")
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)

#Login_TextBox_Password
$Login_TextBox_Password                             = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_Password.Location                    = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]74))
$Login_TextBox_Password.Name                        = [System.String]'Login_TextBox_Password'
$Login_TextBox_Password.Size                        = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]187,[System.Int32]21))
$Login_TextBox_Password.TabIndex                    = [System.Int32]1
$Login_TextBox_Password.Text                        = [System.String]''
$Login_TextBox_Password.PasswordChar                = '*'

#Login_Button_Enter
$Login_Button_Enter                                 = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_Enter.Location                        = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]251,[System.Int32]74))
$Login_Button_Enter.Name                            = [System.String]'Login_Button_Enter'
$Login_Button_Enter.Size                            = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]21))
$Login_Button_Enter.TabIndex                        = [System.Int32]2
$Login_Button_Enter.Text                            = [System.String]'ENTER'
$Login_Button_Enter.UseCompatibleTextRendering      = $true
$Login_Button_Enter.UseVisualStyleBackColor         = $true
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)

#Login_Button_ShowHide
$Login_Button_ShowHide                                 = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location                        = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name                            = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size                            = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex                        = [System.Int32]2
$Login_Button_ShowHide.Text                            = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering      = $true
$Login_Button_ShowHide.UseVisualStyleBackColor         = $true
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)

#Login_Label_Password
$Login_Label_Password                               = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_Password.Location                      = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]54))
$Login_Label_Password.Name                          = [System.String]'Login_Label_Password'
$Login_Label_Password.Size                          = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]100,[System.Int32]17))
$Login_Label_Password.TabIndex                      = [System.Int32]4
$Login_Label_Password.Text                          = [System.String]'Password:'
$Login_Label_Password.UseCompatibleTextRendering    = $true
$Login_Label_Password.add_Click($Label2_Click)

#Form.Controls
$Login_Form.controls.AddRange(@($Login_Label_Password,$Login_Label_User,$Login_Button_Enter,$Login_Button_ShowHide,$Login_TextBox_Password,$Login_TextBox_User))

#Login_Button_Enter Click logic    
$Login_Button_Enter_Click = {
    
    $Script:username                                = $Login_TextBox_User.Text
    $Script:password                                = $Login_TextBox_Password.Text
    $Script:secstr                                  = New-Object -TypeName System.Security.SecureString
    $Script:password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
    $Script:cred                                    = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
    $Login_Form.Close()
}

#Login_Button_ShowHide Click logic 
$Login_Button_ShowHide_Click = {

    if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
    
        $Script:Login_TextBox_Password.PasswordChar            = 0
        $Login_Button_ShowHide.Text                            = [System.String]'SHOW'
    
    }
    
    elseif($Login_TextBox_Password.PasswordChar -eq 0) {
        $Login_Button_ShowHide.Text                            = [System.String]'HIDE' 
        $Script:Login_TextBox_Password.PasswordChar            = "*"
    
    
    }

}




#START APPLICATION

[void]$Login_Form.ShowDialog()

当我第一次 运行 脚本时,window 和其他所有内容都正确显示,但按钮和逻辑不起作用。 为了一切正常,我需要 运行 第二次。

我如何在第一次启动时使一切正常? 我已经尝试了所有可能的方法。我不明白哪里错了。

PowerShell 在很大程度上是一种解释型 语言 - 语句按行顺序执行。

因此您需要定义事件动作脚本块,然后您可以将它们分配给相应的事件处理程序,否则您只是将$null分配给事件处理程序,这就是 none 个按钮有效的原因。

# define the scriptblock that's supposed to handle the event action...
$Login_Button_ShowHide_Click = {
    if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
        $Script:Login_TextBox_Password.PasswordChar            = 0
        $Login_Button_ShowHide.Text                            = [System.String]'SHOW'
    }
    elseif($Login_TextBox_Password.PasswordChar -eq 0) {
        $Login_Button_ShowHide.Text                            = [System.String]'HIDE' 
        $Script:Login_TextBox_Password.PasswordChar            = "*"
    }
}

#Login_Button_ShowHide
$Login_Button_ShowHide                                 = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location                        = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name                            = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size                            = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex                        = [System.Int32]2
$Login_Button_ShowHide.Text                            = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering      = $true
$Login_Button_ShowHide.UseVisualStyleBackColor         = $true

# ... _before_ assigning it to handlethe event
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)

对分配为事件处理程序的所有脚本块重复 - 将它们全部移至脚本顶部,,将所有 .add_<EventName>() 调用移至底部,就在启动 GUI 之前:

# Rest of script goes here ...

# register all event handlers
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)

# start app
[void]$Login_Form.ShowDialog()