Powershell 错误验证
Powershell Error Validation
Function Button_Click()
{
Param([Parameter(Mandatory=$True)]
$telephone,
$calledtoSee,
$wantstoSeeyou,
$pleaseCall,
$willcallAgain,
$returningYourCall,
$ToSelection,
$from,
$telephoneNumber,
$message)
[boolean]$okayContinue=$true
[string]$radioSelectedText
[string]$comboBoxSectionText
[string]$persontoEmail
$callType
$messageCount
$comboBoxSectionText = $ToSelection.GetItemText($ToSelection.SelectedItem)
if($okayContinue){
if($comboBoxSectionText -eq "Please Select Contact"){
[System.Windows.Forms.MessageBox]::Show("Please Select Recipient")
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($from.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Who The Message is From")
$from.focus()
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($telephoneNumber.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Telephone Number")
$telephoneNumber.focus()
$okayContinue=$false
}
}
#######################################################################################################################################################
if($okayContinue){
if($telephone.Checked){
$callType = $telephone.Text
}
elseif($calledtoSee.Checked){
$callType = $calledtoSee.Text
}
elseif($wantstoSeeyou.Checked){
$callType = $wantstoSeeyou.Text
}
elseif($pleaseCall.Checked){
$callType= $pleaseCall.Text
}
elseif($willcallAgain.Checked){
$callType = $willcallAgain.Text
}
elseif($returningYourCall.Checked){
$callType = $returningYourCall.Text
}
else{
[System.Windows.Forms.MessageBox]::Show("Please Select Call Type")
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($message.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Message")
$okayContinue=$false
}
}
if($okayContinue){
$buildPerson=$comboBoxSectionText.Split(',')
$personObject = [pscustomobject]@{
FirstName = $buildPerson[0]
LastName = $buildPerson[1]
Email = $buildPerson[2]
}
$messageObject = [pscustomobject]@{
Message = $message.Text
MessageFor = $personObject
From = $from.Text
CallType = $callType
Telephone = $telephoneNumber.Text
}
}
我有一个包含 6 个单选按钮、2 个文本框和一个组合框的表单。现在,在错误验证方面,我决定使用一个布尔值并检查文本框是否已正确填写以及是否已选择收件人。填写完所有内容后,将创建一个对象。
我在错误验证方面是否走在正确的轨道上?我可以更好地处理它吗?
如果您想要对验证进行完全编程控制,或者需要执行复杂的验证检查,您应该使用大多数 Windows 表单控件中内置的验证事件。每个接受自由格式用户输入的控件都有一个 Validating 事件,该事件将在控件需要数据验证时发生。在验证事件处理方法中,您可以通过多种方式验证用户输入。看看 Event-Driven Validation. And More explanations here : Extending Windows Forms with a Custom Validation.
Function Button_Click()
{
Param([Parameter(Mandatory=$True)]
$telephone,
$calledtoSee,
$wantstoSeeyou,
$pleaseCall,
$willcallAgain,
$returningYourCall,
$ToSelection,
$from,
$telephoneNumber,
$message)
[boolean]$okayContinue=$true
[string]$radioSelectedText
[string]$comboBoxSectionText
[string]$persontoEmail
$callType
$messageCount
$comboBoxSectionText = $ToSelection.GetItemText($ToSelection.SelectedItem)
if($okayContinue){
if($comboBoxSectionText -eq "Please Select Contact"){
[System.Windows.Forms.MessageBox]::Show("Please Select Recipient")
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($from.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Who The Message is From")
$from.focus()
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($telephoneNumber.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Telephone Number")
$telephoneNumber.focus()
$okayContinue=$false
}
}
#######################################################################################################################################################
if($okayContinue){
if($telephone.Checked){
$callType = $telephone.Text
}
elseif($calledtoSee.Checked){
$callType = $calledtoSee.Text
}
elseif($wantstoSeeyou.Checked){
$callType = $wantstoSeeyou.Text
}
elseif($pleaseCall.Checked){
$callType= $pleaseCall.Text
}
elseif($willcallAgain.Checked){
$callType = $willcallAgain.Text
}
elseif($returningYourCall.Checked){
$callType = $returningYourCall.Text
}
else{
[System.Windows.Forms.MessageBox]::Show("Please Select Call Type")
$okayContinue=$false
}
}
if($okayContinue){
if([string]::IsNullOrWhiteSpace($message.Text)){
[System.Windows.Forms.MessageBox]::Show("Please Enter Message")
$okayContinue=$false
}
}
if($okayContinue){
$buildPerson=$comboBoxSectionText.Split(',')
$personObject = [pscustomobject]@{
FirstName = $buildPerson[0]
LastName = $buildPerson[1]
Email = $buildPerson[2]
}
$messageObject = [pscustomobject]@{
Message = $message.Text
MessageFor = $personObject
From = $from.Text
CallType = $callType
Telephone = $telephoneNumber.Text
}
}
我有一个包含 6 个单选按钮、2 个文本框和一个组合框的表单。现在,在错误验证方面,我决定使用一个布尔值并检查文本框是否已正确填写以及是否已选择收件人。填写完所有内容后,将创建一个对象。
我在错误验证方面是否走在正确的轨道上?我可以更好地处理它吗?
如果您想要对验证进行完全编程控制,或者需要执行复杂的验证检查,您应该使用大多数 Windows 表单控件中内置的验证事件。每个接受自由格式用户输入的控件都有一个 Validating 事件,该事件将在控件需要数据验证时发生。在验证事件处理方法中,您可以通过多种方式验证用户输入。看看 Event-Driven Validation. And More explanations here : Extending Windows Forms with a Custom Validation.