I would like to ask how to refresh and changed the jpg below GUI from "Offline" to "Online" based on the live monitoring of specific folder and filenames. For example, I am monitoring a folder for any changes, if in the folder a filename exists with specific string, then it will shows in GUI as "Online" and when the folder doesn't contain the file with specific string, then it will shows as "Offline" in the GUI.
Below is the code that I have.
####################################### 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]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$TestStatus16 = Test-Path -Path C:\Desktop\Newfolder\P16*
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
if($TestStatus16 -eq $True){$StatusPhoto16.Image=$Online}else{$StatusPhoto16.Image=$Offline}
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({Status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
Kindly help improving the above code.
CodePudding user response:
You're missing is what I'm assuming it to be the function call for your Test-Path
.
What's needed is for it to be defined, the object ($StatusPhoto16
) be passed to it, and the update of the actual image based on your Test-Path
.
####################################### 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]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
function status16 ($StatusPhoto16) {
if (Test-Path -Path 'C:\Desktop\Newfolder\P16*')
{
$StatusPhoto16.Image = $Online
}
else
{
$StatusPhoto16.Image = $Offline
}
}
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()