peterz
Goto Top

Powershell Maus in Eingabefeld

Hallo,
ich habe ein kleines Powershell Script gebastelt, in dem eine Inputbox mit zwei Eingabefelder aufgerufen wird.
Nun wäre es schön, wenn nach dem Aufruf die Maus automatisch im ersten Eingabefeld landen würde und die Vorgabe "Name_Test1" zum überschreiben markiert ist.
Wie kann ich das hinbekommen?
Anbei das relevante Codeschnipsel.

Vielen dank schon mal.
Peter

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = „Test1“
$objForm.Size = New-Object System.Drawing.Size(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 System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,230)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = „OK“
$OKButton.DialogResult = „OK“
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)

$AbbruchButton = New-Object System.Windows.Forms.Button
$AbbruchButton.Location = New-Object System.Drawing.Size(100,230)
$AbbruchButton.Size = New-Object System.Drawing.Size(75,23)
$AbbruchButton.Text = „Abbruch“
$AbbruchButton.DialogResult = „Cancel“
$AbbruchButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($AbbruchButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = „Bitte alle Felder ausfüllen:“
$objForm.Controls.Add($objLabel)
$objVornameBox = New-Object System.Windows.Forms.TextBox 
$objVornameBox.Location = New-Object System.Drawing.Size(10,40) 
$objVornameBox.Size = New-Object System.Drawing.Size(260,20)
$objVornameBox.Text = „Name_Test1“

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,60) 
$objLabel1.Size = New-Object System.Drawing.Size(280,20) 
$objNachnameBox = New-Object System.Windows.Forms.TextBox 
$objNachnameBox.Location = New-Object System.Drawing.Size(10,80) 
$objNachnameBox.Size = New-Object System.Drawing.Size(260,20)
$objNachnameBox.Text = „Name_Test2“ 

Content-ID: 355103

Url: https://administrator.de/forum/powershell-maus-in-eingabefeld-355103.html

Ausgedruckt am: 10.04.2025 um 20:04 Uhr

colinardo
colinardo 16.11.2017, aktualisiert am 17.11.2017 um 10:36:16 Uhr
Goto Top
Servus Peter,
ganz einfach du musst nur den Focus mit der Methode Focus() des Textfeldes setzen, und anschließend noch die SelectAll() Methode des Feldes aufrufen face-wink.
# Load Event der Form definieren
$objForm.add_Load({
    # Focus auf das Feld setzen
    $objVornameBox.Focus()
    # Alles im Feld markieren
    $objVornameBox.SelectAll()
})
Grüße Uwe
Peterz
Peterz 17.11.2017 um 10:02:47 Uhr
Goto Top
Hallo Uwe,

vielen Dank für die Hilfe.
Leider habe ich es nicht hinbekommen, diese Passage (ich habe natürlich aus $objVorname.Focus() $objVornameBox.Focus() gemacht) in mein Script erfolgreich einzubauen.

Kannst du mir noch mal helfen.
colinardo
Lösung colinardo 17.11.2017 aktualisiert um 12:31:24 Uhr
Goto Top
Zitat von @Peterz:
Leider habe ich es nicht hinbekommen, diese Passage (ich habe natürlich aus $objVorname.Focus() $objVornameBox.Focus() gemacht) in mein Script erfolgreich einzubauen.
Dann hast du das Event nicht oder an der falschen Stelle eingebaut. Der Code muss vor dem Aufrufen von ShowDialog() stehen da es ein Event-Handler ist der anspringt wenn die Form geladen wird!

Dein Skript oben ist nicht vollständig!

Hier ein genz einfaches Beispiel:

#Generated Form Function
function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

#region Generated Form Objects
$objForm = New-Object System.Windows.Forms.Form
$label2 = New-Object System.Windows.Forms.Label
$label1 = New-Object System.Windows.Forms.Label
$objNachnameBox = New-Object System.Windows.Forms.TextBox
$objVornameBox = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$objForm.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 85
$System_Drawing_Size.Width = 224
$objForm.ClientSize = $System_Drawing_Size
$objForm.DataBindings.DefaultDataSourceUpdateMode = 0
$objForm.Name = "objForm"  
$objForm.Text = "Demo"  
$objForm.add_Load({
    $objVornameBox.Focus()
    $objVornameBox.SelectAll()
})

$label2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 3
$System_Drawing_Point.Y = 38
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 72
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 3
$label2.Text = "Nachname"  

$objForm.Controls.Add($label2)

$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 3
$System_Drawing_Point.Y = 15
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 52
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 2
$label1.Text = "Vorname"  

$objForm.Controls.Add($label1)

$objNachnameBox.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 81
$System_Drawing_Point.Y = 38
$objNachnameBox.Location = $System_Drawing_Point
$objNachnameBox.Name = "objNachnameBox"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$objNachnameBox.Size = $System_Drawing_Size
$objNachnameBox.TabIndex = 1

$objForm.Controls.Add($objNachnameBox)

$objVornameBox.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 81
$System_Drawing_Point.Y = 12
$objVornameBox.Location = $System_Drawing_Point
$objVornameBox.Name = "objVornameBox"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$objVornameBox.Size = $System_Drawing_Size
$objVornameBox.TabIndex = 0
$objVornameBox.Text = "Max"  

$objForm.Controls.Add($objVornameBox)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $objForm.WindowState
#Init the OnLoad event to correct the initial state of the form
$objForm.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$objForm.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
Peterz
Peterz 17.11.2017 um 12:21:35 Uhr
Goto Top
Danke, jetzt hat es funktioniert face-smile