Wie im Titel oben ich etwas Hilfe für ein gutes „Feedback“ auf einer Benutzereingabe benötigen ...Powershell machen ein guten Eingang Feedback
Wie, wenn er 4 Dinge in ein Eingabefenster eingeben und einer von denen, Dinge nur aus Zahlen, die zweite wie die vier Eingabefelder sind nicht null sein sollten und vieles mehr ...
bestehen sollte:
- ParID (nur Zahlen sein und ich brauche den Benutzer genau 6 eingeben
- Forschungsgruppe (3 Buchstaben u ein Dropdown singen und diese sollten nur Buchstaben)
- Kunde (Buchstaben, Zahlen alles erlaubt sein, aber ich darf nicht leer sein!)
- Projektnamen (gleicher wie Kundeneingang)
Das Problem ist, dass mein Code funktioniert nicht, weil es hält mir das gleiche Feedback zum Beispiel sagen:
Eingang: "123456" Fehler: "Ist das nicht eine Zahl"
Mein Code:
$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 = ""
EDIT: WIndow-Code
$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()
Warum nicht eine Regex verwenden, um die Eingabe zu validieren? Zum Beispiel würde '^ \ d {6} $' genau sechs Ziffern entsprechen. – vonPryz
wäre eine Idee, aber das Problem ist immer noch da, weil meine Fehlerausgaben zufällig ausgesendet werden (zumindest scheinen sie zufällig zu sein). Ich weiß nicht, was mit meinem Code falsch ist ^^ –
Und ich weiß nicht wirklich, wie man Regex verwendet, weil ich Powershell sehr neu bin ^^ –