question

TimKristof-5537 avatar image
0 Votes"
TimKristof-5537 asked TimKristof-5537 published

Why i need a second click on "OK" Button?

I wrote a script, wich works fine. But i have to klick twice on d´the OK Button and dont know why.
Here is the Code:
$CancelText = 'Cancel'
$OKButtonText = 'OK'
$SystemWindowsFormsTextBox = 'System.Windows.Forms.TextBox'
$SystemWindowsFormsLabel = 'System.Windows.Forms.Label'
$SystemWindowsFormsButton = 'System.Windows.Forms.Button'
$SystemDrawingSize = 'System.Drawing.Size'
$0Field = '{0}'

 function start-printercreation
 {
   <#
       .SYNOPSIS
       EIn Drucker wird auf dem Prinserver erstellt
    
       .DESCRIPTION
       Aus der Abfrage des Formulars mit den Parametern 
       Druckername
       Druckerport 
       Treibername
       wird ein Drucker installiert.
       Die Druckertreiber werden zuvor aus den vorhandenen verfügbaren Druckertreibern ausgelesen.
    
       .EXAMPLE
       start-printercreation
          
    
       .NOTES
       Dieses Skript ist nur für die direkte Ausführung auf dem Printserver gedacht. Da auf den anderen Servern der Spoolerdienst deaktiviert ist.
    
          
   #>
    
      
     
   Add-PrinterPort -Name ($0Field -f $Printerport) -PrinterHostAddress ($0Field -f $Printerport) -PortNumber 9100
   Add-Printer -Name ($0Field -f $Printername) -DriverName ($0Field -f $Drivername) -PortName ($0Field -f $printerport) -Shared -ShareName ($0Field -f $Printername)
    
    
 }
    
    
    
 Add-Type -AssemblyName System.Drawing
 Add-Type -AssemblyName System.Windows.Forms
    
 $objForm = New-Object -TypeName System.Windows.Forms.Form
 $objForm.Text = 'Dateneingabe'
 $objForm.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (400,300)
 $objForm.StartPosition = 'CenterScreen'
 $objForm.KeyPreview = $True
 $objForm.Add_KeyDown({if ($_.KeyCode -eq 'Enter') { $objForm.DialogResult='OK'
 $objForm.Close()} })
 $objForm.Add_KeyDown({if ($_.KeyCode -eq 'Escape') { $objForm.DialogResult='Cancel'
 $objForm.Close()} })
    
 $OKButton = New-Object -TypeName $SystemWindowsFormsButton
 $OKButton.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,180)
 $OKButton.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,23)
 $OKButton.Text = $OKButtonText
    
 $OKButton.DialogResult = $OKButtonText
 $OKButton.Add_Click({$objForm.Close()})
 $objForm.Controls.Add($OKButton)
    
 $CancelButton = New-Object -TypeName $SystemWindowsFormsButton
 $CancelButton.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (150,180)
 $CancelButton.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,23)
 $CancelButton.Text = $CancelText
 $CancelButton.DialogResult = $CancelText
 $CancelButton.Add_Click({$objForm.Close()})
 $objForm.Controls.Add($CancelButton)
    
 $objLabel = New-Object -TypeName $SystemWindowsFormsLabel
 $objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,20)
 $objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20)
 $objLabel.Text = 'Name des Druckers:'
 $objForm.Controls.Add($objLabel)
 $objTextBox = New-Object -TypeName $SystemWindowsFormsTextBox
 $objTextBox.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,40)
 $objTextBox.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20)
 $objTextBox.Text = 'Druckername'
 $objForm.Controls.Add($objTextBox)
    
 $objLabel = New-Object -TypeName $SystemWindowsFormsLabel
 $objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,70)
 $objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20)
 $objLabel.Text = 'IP-Adresse des Druckers:'
 $objForm.Controls.Add($objLabel)
 $objTextBox2 = New-Object -TypeName $SystemWindowsFormsTextBox
 $objTextBox2.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,90)
 $objTextBox2.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20)
 $objTextBox2.Text = '1.2.3.4'
 $objForm.Controls.Add($objTextBox2)
    
 $objLabel = New-Object -TypeName $SystemWindowsFormsLabel
 $objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,120) 
 $objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20) 
 $objLabel.Text = 'Treffen Sie bitte eine Auswahl:'
 $objForm.Controls.Add($objLabel) 
 $objCombobox = New-Object -TypeName System.Windows.Forms.Combobox 
 $objCombobox.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,140) 
 $objCombobox.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20) 
    
 $Printerdrivers = (Get-PrinterDriver).Name
 foreach ($printerdriver in $printerdrivers)
 { 
   $null =  $objCombobox.Items.Add(($0Field -f $Printerdriver))
 }
 $objCombobox.Height = 70
 $objForm.Controls.Add($objCombobox) 
 $objForm.Topmost = $True
 $objForm.Add_Shown({$objForm.Activate()})
 $null =  $objForm.ShowDialog()
 $Drivername = $objCombobox.SelectedItem
 $Printerport = $objTextBox2.Text
 $Printername = $objTextBox.Text
    
      
    
    
    
 $null =  $objForm.ShowDialog()
    
 If ($objForm.DialogResult -like $OKButton) {start-Printercreation} else {'Abbruch geklickt'}

Could you help me?

Greets form Hamburg

Tim

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Found the Bug.
in 114 second call vor object.dialog

0 Votes 0 ·

0 Answers