Home > Software design >  How to Stop Window Error Sound When Typing 'Enter' or 'CTRL A' in PowerShell W
How to Stop Window Error Sound When Typing 'Enter' or 'CTRL A' in PowerShell W

Time:12-22

Every time when I press the key to activate the button, I hear the system sound "deeng".

How can I turn off this sound in the program?

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


# form specs
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Text = "Check Network Status"
$Form1.Size = New-Object System.Drawing.Size(300,170)
$Form1.StartPosition = "CenterScreen"
$Form1.KeyPreview = $True
$Form1.MaximumSize = $Form1.Size
$Form1.MinimumSize = $Form1.Size
 
# form icon

$Form1.Icon = New-Object system.drawing.icon ("C:\Scripts\ps\ADDONS\ico1.ico")
 
$BackgorundImage = [system.drawing.image]::FromFile("C:\Scripts\ps\ADDONS\jpg1.jpg")
$Form1.BackgroundImage = $BackgorundImage
$Form1.BackgroundImageLayout = "None"
# None, Tile, Center, Stretch, Zoom
 
$Form1.Width = $BackgorundImage.Width
$Form1.Height = $BackgorundImage.Height
 
 
# label
$Label1 = New-Object System.Windows.Forms.label
$Label1.Location = New-Object System.Drawing.Size(7,10)
$Label1.Size = New-Object System.Drawing.Size(130,15)
$Label1.BackColor = "Transparent"
$Label1.ForeColor = "yellow"
$Label1.Text = "Enter Computer Name"
$Form1.Controls.Add($Label1)
 
# input box
$objTextbox = New-Object System.Windows.Forms.TextBox
$objTextbox.Location = New-Object System.Drawing.Size(10,45)
$objTextbox.Size = New-Object System.Drawing.Size(120,20)
$Form1.Controls.Add($objTextbox)
 
# ok button
$objButton = New-Object System.Windows.Forms.Button
$objButton.Location = New-Object System.Drawing.Size(140,44)
$objButton.Size = New-Object System.Drawing.Size(75,23)
$objButton.Text = "OK"
$objButton.Add_Click($button_click)
$Form1.Controls.Add($objButton)
 
# return status
$returnStatus = New-Object System.Windows.Forms.label
$returnStatus.Location = New-Object System.Drawing.Size(8,70)
$returnStatus.Size = New-Object System.Drawing.Size(130,30)
$returnStatus.BackColor = "Transparent"
$returnStatus.Text = ""
$Form1.Controls.Add($returnStatus)
 
# action item here - you could add your own actions
$button_click =
{
  


$returnStatus.Text = ""  

$computerName = $objTextbox.Text
 
# output - online  
if (Test-Connection $computerName -quiet -Count 1){
Write-Host -ForegroundColor Green "$computerName is online"
$returnStatus.BackColor = "Transparent"
$returnStatus.ForeColor = "lime"
$returnStatus.Text = "Status: Online"
}
Else{
# output - offline
Write-Host -ForegroundColor Red "$computerName is offline"
$returnStatus.ForeColor= "Red"
$returnStatus.Text = "Status: Offline"
}
}  
 

   
$Form1.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $button_click}})
$Form1.Add_KeyDown({if (($_.Control) -and ($_.KeyCode -eq 'A')){$objTextbox.SelectAll()}})
$Form1.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form1.Close()}})
   
# modal
$Form1.Topmost = $True
$Form1.Add_Shown({$Form1.Activate()})
[void] $Form1.ShowDialog()

I tried it:

$_.Handled = $true
$_.SuppressKeyPress = $true

Yes, it helps to remove sounds, but if these commands are present in the code, then I cannot write anything in the TextBox.

And yet, if someone knows how to make it so that when I press "CTRL A", not only the TextBox to which the "SelectAll" command is attached, but the TextBox in which I am now is selected.

I would be very grateful for the help! Best regards, Ar1kato

CodePudding user response:

You need to switch $_.SuppressKeyPress, e.g as follows:

$Form1.Add_KeyDown({
    $_.SuppressKeyPress = $True
    if     ($_.KeyCode -eq "Enter"){& $button_click}
    elseif (($_.Control) -and ($_.KeyCode -eq 'A')){$objTextbox.SelectAll()}
    elseif ($_.KeyCode -eq "Escape"){$Form1.Close()}
    else {$_.SuppressKeyPress = $False}
    })

Note that I define the only Add_KeyDown event listener…

  • Related