Powershell聊天程序刷新问题

时间:2021-03-04 13:47:42

标签: powershell chat

我是堆栈溢出的新成员,虽然它已经对我有帮助多年了

我尝试编写的 Power Shell 程序有问题。 我在带有 GUI 的 Power Shell 中构建了一个简单的聊天程序。它将文本写入 网络上的文本文件,这可以通过其他聊天程序获取。 我希望它每秒自动刷新 $outputBox.text 但直到现在它已经 没有成功。 但是我成功添加了一个手动按钮来刷新。

我的问题:如何让它自动刷新 $outputbox.text ?

希望有人能帮助我。

代码:

    $date = Get-Date -Format g
    echo "$date" | Out-File -filepath \\net\swap$\chat.txt  -Append
    echo "Chat with IT initiated" | Out-File -filepath \\net\swap$\chat.txt  -Append
    $user = whoami


    function Sendinput{

        $text=$Userinput.text;
        
        echo "$user : $text" | Out-File -filepath \\net\swap$\chat.txt  -Append
        $outputBox.Text = [IO.File]::ReadAllText("\\net\swap$\chat.txt ")
        $Userinput.Text = ""
    }

    function Refresh{

        $outputBox.Text = [IO.File]::ReadAllText("\\net\swap$\chat.txt ")
    }


#### Form settings #################################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Chat with IT"    
$Form.Size = New-Object System.Drawing.Size(800,500)  
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
$Form.Refresh()

#### Title - Powershell GUI Tool ###################################################
$Label = New-Object System.Windows.Forms.Label
$LabelFont = New-Object System.Drawing.Font("Calibri",18,[System.Drawing.FontStyle]::Bold)
$Label.Font = $LabelFont
$Label.Text = "Chat with IT"
$Label.AutoSize = $True
$Label.Location = New-Object System.Drawing.Size(315,20) 
$Form.Controls.Add($Label)

#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(20,70) 
$outputBox.Size = New-Object System.Drawing.Size(740,275)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ReadOnly = $True
$outputBox.Refresh()
$outputBox.Update();
$outputBox.Cursor = [System.Windows.Forms.Cursors]::arrow
$outputBox.ScrollBars = "Vertical"
$outputBox.Text = [IO.File]::ReadAllText("\\ld1\swap$\chat.txt ")
$Form.Controls.Add($outputBox)

#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,380) 
$groupBox.size = New-Object System.Drawing.Size(760,60)
$groupBox.text = "Send responds:" 

$Form.Controls.Add($groupBox) 

#### User input #####################################################################
$Userinput = New-Object System.Windows.Forms.TextBox 
$Userinput.Location = New-Object System.Drawing.Size(10,20)
$Userinput.Size = New-Object System.Drawing.Size(540,20) 
$Userinput.Controls.Add($InputBox)
$Userinput.Controls.Add($Label2)
$groupBox.Controls.Add($Userinput)

#### Send inuput ####################################################################
$Sendinput = New-Object System.Windows.Forms.Button
$Sendinput.Location = New-Object System.Drawing.Size(660,20)
$Sendinput.Size = New-Object System.Drawing.Size(80,20)
$Sendinput.Text = "Send"
$Sendinput.Add_Click({Sendinput})
$Sendinput.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Sendinput)

#### Refresh ########################################################################
$Refresh = New-Object System.Windows.Forms.Button
$Refresh.Location = New-Object System.Drawing.Size(560,20)
$Refresh.Size = New-Object System.Drawing.Size(80,20)
$Refresh.Text = "Refresh"
$Refresh.Add_Click({Refresh})
$Refresh.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Refresh)

#####################################################################################

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()   

0 个答案:

没有答案
相关问题