首页 > 解决方案 > 我们如何检查价值 $new_value1 = $inputpecentw1.Text。这个值可以吗?

问题描述

****我们如何更改代码行 $MaskedTextBox.PasswordChar = '*' 以检查数字值输入?我们如何限制powershell TextBox中减号和一个数字或一个数字的输入窗口大小?我们希望用户只能输入一个符号(减号)和一位数字或只能输入一位数字。为什么我们不能在 Windows 中使用退格键来删除旧值?

我们如何在窗口输入期间检查值 $new_value1 = $inputpecentw1.Text?这个值可以

只能是一个负数,例如 -5 且不超过 -9 或正数

数字例如 4 从 1 到 9 和 0

我生成的程序在 $Save_Click = { 之后不像设计那样工作

    Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form= New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '800,800'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$textnamef1 = New-Object 'System.Windows.Forms.Label'
$textPercent = New-Object 'System.Windows.Forms.Label'
    $inputpecentw1 = New-Object 'System.Windows.Forms.MaskedTextBox'

    $Read = New-Object 'System.Windows.Forms.Button'
    $Save = New-Object 'System.Windows.Forms.Button'
    $form_Load = {
         $inputpecentw1.Text = [xml](Get-Content 'c:\program\MIK_Autokontinent.xml') | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
    }    
    $Read_Click = {

      $inputpecentw1.Text = [xml](Get-Content 'c:\program\MIK_Autokontinent.xml') | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String

    }  
    $Save_Click = {






IF ($inputpecentw1.Text -eq '^(\d|-\d)$'){
$new_value1 = $inputpecentw1.Text
 [xml](Get-Content 'c:\program\MIK_Autokontinent.xml' -Encoding UTF8)  | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.SetAttribute("Percent", $new_value1) }; $_.Save('c:\program\MIK_Autokontinent.xml') }       
}
Else{ $value=$inputpecentw1.Text 
[System.Windows.MessageBox]::Show($value.ToString())
}        } 
     $form.Controls.Add($textnamef1)
     $form.Controls.Add($textPercent)
    $form.Controls.Add($inputpecentw1)
    $form.Controls.Add($Read)
    $form.Controls.Add($Save)
    $form.ClientSize = '800, 800'
    $form.Text = 'проценты'
    $form.add_Load($form_Load)
    $textnamef1.AutoSize = $True
    $textnamef1.Location = '80, 40'
    $textnamef1.Text = 'MIK_Autokontinent.xml'    
    $textPercent.AutoSize = $True
    $textPercent.Location = '120, 110'
    $textPercent.Text = 'Percent'
    $inputpecentw1.Location = '120, 140'
    $inputpecentw1.Size = '20, 20'
    $Read.Location = '20,740'
    $Read.Size = '100, 40'
    $Read.Text = 'Rread'
    $Read.add_Click($Read_Click)
    $Save.Location = '680,740'
    $Save.Size = '100, 40'
    $Save.Text = 'Save'
    $Save.add_Click($Save_Click)
    #Show the Form
    $form.ShowDialog()

标签: powershellpowershell-2.0powershell-3.0powershell-4.0

解决方案


您应该使用System.Windows.Forms.MaskedTextBox而不是常规的 TextBox,然后分配Mask属性。

这是可用掩码的文档


推荐阅读