如何在GUI运行时更新GUI

时间:2017-02-01 10:48:14

标签: winforms powershell user-interface

我有一个运行Windows窗体和文本框的脚本。

我的问题是:当GUI运行并呈现给用户时,我更改了文本框的内容,如何刷新GUI并查看更改?

$formText = New-Object 'System.Windows.Forms.Form'
$richtextbox1 = New-Object 'System.Windows.Forms.RichTextBox'
$buttonFind = New-Object 'System.Windows.Forms.Button'
$textboxFind = New-Object 'System.Windows.Forms.TextBox'
$buttonCopy = New-Object 'System.Windows.Forms.Button'
$buttonExit = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#region FindFunction

function FindText
{
    if ($textboxFind.Text.Length -eq 0)
    {
        return
    }
    $index = $richtextbox1.Find($textboxFind.Text,$richtextbox1.SelectionStart+ $richtextbox1.SelectedText.Length,[System.Windows.Forms.RichTextBoxFinds]::None)
    if ($index -ge 0)
    {
        $richtextbox1.Select($index,$textboxFind.Text.Length)
        $richtextbox1.ScrollToCaret()
        #$richtextbox1.Focus()
    }
    else
    {
        $index = $richtextbox1.Find($textboxFind.Text,0,$richtextbox1.SelectionStart,[System.Windows.Forms.RichTextBoxFinds]::None)
        #
        if ($index -ge 0)
        {
            $richtextbox1.Select($index,$textboxFind.Text.Length)
            $richtextbox1.ScrollToCaret()
            #$richtextbox1.Focus()
        }
        else
        {
           $richtextbox1.SelectionStart = 0
        }
    }
}
#endregion
$formText_Load = {
    #TODO: Initialize Form Controls here
}
$buttonExit_Click = {
    #TODO: Place custom script here
    $formText.Close()
}
$buttonLoad_Click = {
    Load-Text
}
$buttonCopy_Click = {
#The following requires STA mode
#    if ($textbox1.Text.Length -gt 0)
#    {
#        [System.Windows.Forms.Clipboard]::SetText($textbox1.Text)
#    }
    #Alternative - Does not require STA
    $richtextbox1.SelectAll() #Select all the text
    $richtextbox1.Copy()      #Copy selected text to clipboard
    $richtextbox1.Select(0,0); #Unselect all the text
}
$textboxFind_TextChanged = {
    $buttonFind.Enabled = $textboxFind.Text.Length -gt 0
}
$buttonFind_Click = {
    #TODO: Place custom script here
    FindText
}
#################################################
# Customize LoadText Function
#################################################
$Form_StateCorrection_Load = {
    #Correct the initial state of the form to prevent the .Net maximized form issue 
    $formText.WindowState = $InitialFormWindowState
}
$Form_StoreValues_Closing = {
    #Store the control values
    $script:MainForm_richtextbox1 = $richtextbox1.Text
    $script:MainForm_textboxFind = $textboxFind.Text
}
$Form_Cleanup_FormClosed = {
    #Remove all event handlers from the controls
    try
    {
        $buttonFind.remove_Click($buttonFind_Click)
        $textboxFind.remove_TextChanged($textboxFind_TextChanged)
        $buttonCopy.remove_Click($buttonCopy_Click)
        $buttonExit.remove_Click($buttonExit_Click)
        $formText.remove_Load($formText_Load)
        $formText.remove_Load($Form_StateCorrection_Load)
        $formText.remove_Closing($Form_StoreValues_Closing)
        $formText.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
$formText.Controls.Add($richtextbox1)
$formText.Controls.Add($buttonFind)
$formText.Controls.Add($textboxFind)
$formText.Controls.Add($buttonCopy)
$formText.Controls.Add($buttonExit)
$formText.AcceptButton = $buttonFind
$formText.AutoScaleDimensions = '10, 20'
$formText.AutoScaleMode = 'Font'
$formText.ClientSize = '828, 594'
$formText.Margin = '8, 8, 8, 8'
$formText.Name = 'formText'
$formText.StartPosition = 'CenterScreen'
$formText.Text = 'Wake On Lan'
$formText.add_Load($formText_Load)
$formtext.TopMost = $True

$richtextbox1.Anchor = 'Top, Bottom, Left, Right'
$richtextbox1.BackColor = 'Window'
$richtextbox1.Font = 'Courier New, 12pt'
$richtextbox1.HideSelection = $False
$richtextbox1.Location = '20, 55'
$richtextbox1.Margin = '5, 5, 5, 5'
$richtextbox1.Name = 'richtextbox1'
$richtextbox1.ReadOnly = $True
$richtextbox1.Size = '784, 467'
$richtextbox1.TabIndex = 6
$richtextbox1.WordWrap = $False
#
# buttonFind
#
$buttonFind.Anchor = 'Top, Right'
$buttonFind.Enabled = $False
$buttonFind.Location = '748, 1'
$buttonFind.Margin = '5, 5, 5, 5'
$buttonFind.Name = 'buttonFind'
$buttonFind.Size = '60, 35'
$buttonFind.TabIndex = 5
$buttonFind.Text = '&Find'
$buttonFind.UseVisualStyleBackColor = $True
$buttonFind.add_Click($buttonFind_Click)
#
# textboxFind
#
$textboxFind.Anchor = 'Top, Right'
$textboxFind.Location = '420, 15'
$textboxFind.Margin = '8, 8, 8, 8'
$textboxFind.Name = 'textboxFind'
$textboxFind.Size = '316, 26'
$textboxFind.TabIndex = 4
$textboxFind.add_TextChanged($textboxFind_TextChanged)
#
# buttonCopy
#
$buttonCopy.Anchor = 'Bottom'
$buttonCopy.Location = '20, 540'
$buttonCopy.Margin = '5, 5, 5, 5'
$buttonCopy.Name = 'buttonCopy'
$buttonCopy.Size = '125, 35'
$buttonCopy.TabIndex = 3
$buttonCopy.Text = '&Copy'
$buttonCopy.UseVisualStyleBackColor = $True
$buttonCopy.add_Click($buttonCopy_Click)
#
# buttonExit
#
$buttonExit.Anchor = 'Bottom, Right'
$buttonExit.Location = '683, 540'
$buttonExit.Margin = '5, 5, 5, 5'
$buttonExit.Name = 'buttonExit'
$buttonExit.Size = '125, 35'
$buttonExit.TabIndex = 2
$buttonExit.Text = 'E&xit'
$buttonExit.UseVisualStyleBackColor = $True
$buttonExit.add_Click($buttonExit_Click)
$formText.ResumeLayout()
#endregion Generated Form Code
$Results = "1","2"
#---------------------------------------------
$richtextbox1.Text = foreach ($Result in $Results) {
    ($Result+[Environment]::NewLine)
}

#Save the initial state of the form
$InitialFormWindowState = $formText.WindowState
#Init the OnLoad event to correct the initial state of the form
$formText.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$formText.add_FormClosed($Form_Cleanup_FormClosed)
#Store the control values when form is closing
$formText.add_Closing($Form_StoreValues_Closing)
#Show the Form

$formText.ShowDialog()

0 个答案:

没有答案