Powershell提供了良好的输入反馈

时间:2016-07-21 06:34:54

标签: powershell powershell-v2.0

如上所述,我需要一些帮助,以便对用户输入提供良好的“反馈”......

就像他在输入窗口输入4个东西,其中一个东西应该只包含数字,第二个不应该是空的,而且更多......

与4个输入字段类似:

  • ParID(应该只是数字,我需要用户输入6
  • 研究组(3个字母使用下拉列表,那些应该只是字母)
  • 客户(信件,号码允许一切,但我不应该是空的!)
  • 项目名称(与客户输入相同)

问题是我的代码不起作用,因为它一直告诉我相同的反馈,例如:

输入:“123456”错误:“不是数字”

我的代码:

    $error_message = "Please fill out all of the form fields correctly!`n" #text which will be displayed
    $error_sign = "Exclamation" #sign veriable -> for a specific sign (picture) which will be displayed 

    if( #the program is going trough all the different types of wrong inputs that could happen and throws an error message!
      ($ParIDInbox.Text.Length -lt 1) -and
      ($ResearchGroupInbox.Text.Length -lt 1) -and
      ($CustomerInbox.Text.Length -lt 1) -and
      ($ProjectnameInbox.Text.Length -lt 1)) {
       $error_output = "All fields must be filled out!"
    } else {
        Write-Host -f Green "Everything seems right!"
    }

    #if($ParIDInbox.Text.Length
    if([string]::IsNullOrEmpty($ParID)) {
        $error_output = "ParID must be filled out!"
    } else {  
        if(($ParID -gt 0) -and ($ParID -lt 6)) { 
            $error_output = "ParID is too short!"
        } else {  
            $Sort = [int[]][char[]]$ParID #sorts the input ParID into an int and a char array
                foreach($Value in $ParID) { #goes trough every letter and checks if the ASCII Code for 0-9 is correct!
                     if (!(($Value -ge 48) -and ($Value -le 57))) {
                        $error_output = "ParID is not a number!"
                     } else {
                        #nothing else matters ...
                     }
                }
            }
        }       
    }     
    $error_message = $error_message + "$($error_output)"

    [System.Windows.Forms.MessageBox]::Show($error_message, "Warning", #messagebox text -> text which will be displayed in the error message box
    [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::$error_sign) #picture -> picture that will be shown in the window ($error_sign)!
    $error_output = ""

编辑:WIndow代码

$objForm = New-Object System.Windows.Forms.Form #init -> initializing window form
$objForm.Text = "Input Window v0.5" #name -> the name which will be display in the top border of the window
$objForm.Size = New-Object System.Drawing.Size(300,290) #size -> set the size of the window
$objForm.StartPosition = "CenterScreen" #location -> where the window will appear on the screen
$objForm.FormBorderStyle = 'Fixed3D' #sizing -> fixes the size of the window so you cannot make it bigger       
$OKButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button
$OKButton.Location = New-Object System.Drawing.Size(75,190) #Location -> where the button is located in the window
$OKButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button
$OKButton.Text = "OK" #value -> sets the value of the button to 'OK'
$objForm.Controls.Add($OKButton) #adding -> adds the button to the window
$OKButton.Add_Click($OKButton_OnClick)   
$CancelButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button
$CancelButton.Location = New-Object System.Drawing.Size(150,190) #Location -> where the button is located in the window 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button
$CancelButton.Text = "Cancel" #value -> sets the value of the button to 'Cancel'
$CancelButton.Add_Click($CancelButton_Onclick) #closing -> closes the window after clicked
$objForm.Controls.Add($CancelButton) #adding -> adds the button to the window         
$Info_Label = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$Info_Label.Location = New-Object System.Drawing.Size(10,220) #Location -> where the label is located in the window
$Info_Label.Size = New-Object System.Drawing.Size(280,50) #Size -> defines the size of the label
$Info_Label.Text = "If a browser window does not open up in 30 seconds please restart this script program!" #Info Text
$objForm.Controls.Add($Info_Label) #adding -> adds the label to the window    $ParIDLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label      
$ParIDLabel.Location = New-Object System.Drawing.Size(10,10) #Location -> where the label is located in the window     
$ParIDLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ParIDLabel.Text = "Par ID (6 numbers!)" #value -> sets the value of the Label to 'Par ID (6 numbers)'
$objForm.Controls.Add($ParIDLabel) #adding -> adds the label to the window        $ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window
$ParIDInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$ParIDInbox.MaxLength = 6 #sets max. length of the input box to 6 
$objForm.Controls.Add($ParIDInbox) #adding -> adds the input box to the window
$ResearchGroupLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$ResearchGroupLabel.Location = New-Object System.Drawing.Size(10,55)     $ResearchGroupLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ResearchGroupLabel.Text = "Research Group (3 letters!)" #value -> sets the value of the Label to 'Research Group (3 letters!)'
$objForm.Controls.Add($ResearchGroupLabel) #adding -> adds the label to the window
$ResearchGroupInbox = New-Object System.Windows.Forms.ComboBox #initialization -> initializes the combobox (dropdown)
$ResearchGroupInbox.Location = New-Object System.Drawing.Size(10, 75) 
$ResearchGroupInbox.Size = New-Object System.Drawing.Size(130,28) #Size -> defines the size of the box
$ResearchGroupInbox.MaxLength = 3 #sets max. length of the input box to 3
ForEach ($Item in $ResearchGroupShortcuts) { #loop -> put all variables from '$DropDownArray' into '$Item'
    $ResearchGroupInbox.Items.Add($Item) #adder -> add '$Item' to the dropdown
}
$objForm.Controls.Add($ResearchGroupInbox) #adding -> add the dropdown to the window
$CustomerLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$CustomerLabel.Location = New-Object System.Drawing.Size(10,100) #Location -> where the label is located in the window
$CustomerLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$CustomerLabel.Text = "Customer:" #value -> sets the value of the Label to 'Customer'
$objForm.Controls.Add($CustomerLabel) #adding -> adds the label to the window

$CustomerInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$CustomerInbox.Location = New-Object System.Drawing.Size(10,120) #Location -> where the label is located in the window
$CustomerInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$CustomerInbox.MaxLength = 50 #sets max. length of the input box to 50 
$objForm.Controls.Add($CustomerInbox) #adding -> adds the input box to the window    
$ProjectnameLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$ProjectnameLabel.Location = New-Object System.Drawing.Size(10,140)     $ProjectnameLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ProjectnameLabel.Text = "Projectname:"  #value -> sets the value of the Label to 'Projectname'
$objForm.Controls.Add($ProjectnameLabel) #adding -> adds the label to the window
$ProjectnameInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ProjectnameInbox.Location = New-Object System.Drawing.Size(10,160)     $ProjectnameInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox 
$ProjectnameInbox.MaxLength = 50 #sets max. length of the input box to 50 
$objForm.Controls.Add($ProjectnameInbox) #adding -> adds the input box to the window 
$objForm.Topmost = $True #topmost -> A topmost form is a form that overlaps all the other (non-topmost!) forms!

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

1 个答案:

答案 0 :(得分:1)

就我之前的问题记住您的表单时,我建议您将TextChanged事件添加到ypour $ParIDInbox中:

#ParID Input Box -> Input box for the Par ID input
$ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window
$ParIDInbox.Size = New-Object System.Drawing.Size(60,20) #Size -> defines the size of the inputbox
$ParIDInbox.MaxLength = 6

$ParIDInbox_OnTextEnter = {
    if ($ParIDInbox.Text -notmatch '^\d{1,6}$') {
        $ParIDInbox.Text = $ParIDInbox.Text -replace '\D'
    }
}

$ParIDInbox.add_TextChanged($ParIDInbox_OnTextEnter)
$objForm.Controls.Add($ParIDInbox)

此代码删除除数字以外的任何输入符号,并将ParIDInbox MaxLength参数设置为6.因此,您只需要在单击“确定”按钮时检查最小长度。我通常独立地检查每个字段而不是一个if语句中的所有字段 - 它帮助我调试错误抛出的地方:

if ($ParIDInbox.Text.Length -lt 6) { # or whatever length is ok
   Show-MessageBox -Type Error -Message "Par ID field should contain 6 digits!"
}
elseif ($NextInputBox.Text ...) { ... }# etc
else {
    # Your OK button actions here
}

应该在代码的开头定义Show-MessageBox函数:

function Show-MessageBox {
    param(
        [parameter(Mandatory=$true)]
        [string]$Message,
        [Validateset('Error', 'Warning', 'Information')]
        [string]$Type = 'Information'
    )

    [System.Windows.Forms.MessageBox]::Show($Message, $Type, `
        [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::$Type) 
}