Powershell .Net Form auto Scrolldown
Moin
Folgendes Szenario: eine kleine Form mit einer multiline Textbox. In dem vereinfachten Beispiel wird jedes Mal eine Zeile hinzugefügt. Das Ziel wäre jetzt, dass das Fenster automatisch runterscrollt, wenn der Text länger wird. Hat jemand eine Idee, wie ich das machen kann?
Vielen Dank
Gruess
Arduino
Folgendes Szenario: eine kleine Form mit einer multiline Textbox. In dem vereinfachten Beispiel wird jedes Mal eine Zeile hinzugefügt. Das Ziel wäre jetzt, dass das Fenster automatisch runterscrollt, wenn der Text länger wird. Hat jemand eine Idee, wie ich das machen kann?
Vielen Dank
Gruess
Arduino
Function Do-Something
{
$ObjTextBox.Text = $ObjTextBox.Text + "`r`nnoch mehr intelligenter Text"
}
[Void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[Void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$objForm = New-Object System.Windows.Forms.Form
$ObjForm.Add_Shown({$ObjForm.Activate()})
$ObjForm.Controlbox = $False
$ObjForm.FormBorderStyle = 'Fixed3D'
$objForm.MinimizeBox = $False
$objForm.MaximizeBox = $False
$objForm.Size = New-Object System.Drawing.Size(390,330)
$objForm.StartPosition = “CenterScreen”
$objForm.Text = “Text coming soon”
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({If ($_.KeyCode -eq “Enter”) { Do-Something } })
$objForm.Add_KeyDown({If ($_.KeyCode -eq “Escape”) { $objForm.Close()} })
$ObjButton = New-Object Windows.Forms.Button
$ObjButton.Add_Click({ Do-Something })
$ObjButton.Text = "do something"
$ObjButton.Location = New-Object Drawing.Point(10,10)
$ObjForm.Controls.Add($ObjButton)
$ObjTextBox = New-Object System.Windows.Forms.TextBox
$ObjTextBox.Location = New-Object System.Drawing.Size(10,40)
$ObjTextBox.Size = New-Object System.Drawing.Size(360,250)
$ObjTextBox.Multiline = $True
$ObjTextBox.ScrollBars = 'Both'
$ObjTextBox.Text = "Hier kommt noch ein intelligenter Text rein"
$ObjTextBox.Readonly = $True
$ObjForm.Controls.Add($ObjTextBox)
[Void]$objForm.ShowDialog()
Please also mark the comments that contributed to the solution of the article
Content-Key: 296557
Url: https://administrator.de/contentid/296557
Printed on: April 18, 2024 at 05:04 o'clock
4 Comments
Latest comment
Hallo @arduino,
kein Problem hier eine Demo:
(Das essentielle zum Scrollen ans Ende einer MultiLine-Textbox steht in Zeile 23 und 25)
Grüße Uwe
p.s. Es wäre von Vorteil wenn du zumindest das Wort "Powershell" in deinem Titel erwähnen könntest, so dass auch andere den Thread finden können und direkt wissen worum es geht. Danke.
kein Problem hier eine Demo:
(Das essentielle zum Scrollen ans Ende einer MultiLine-Textbox steht in Zeile 23 und 25)
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
$form1 = New-Object System.Windows.Forms.Form
$btnAddLine = New-Object System.Windows.Forms.Button
$txt2 = New-Object System.Windows.Forms.TextBox
$txt1 = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$handler_btnAddLine_Click=
{
# Zeile zur Multiline.Textbox hinzufügen
$txt1.AppendText($txt2.Text + "`r`n")
}
$handler_txt1_TextChanged=
{
# Cursor bei Änderung in der Textbox an Ende setzen
$txt1.SelectionStart = $txt1.Text.Length
# scrolle zur Cursorposition in der Textbox die sich ja jetzt am Ende befindet
$txt1.ScrollToCaret()
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 221
$System_Drawing_Size.Width = 250
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Scrolling Textbox"
$btnAddLine.Anchor = 10
$btnAddLine.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 183
$System_Drawing_Point.Y = 178
$btnAddLine.Location = $System_Drawing_Point
$btnAddLine.Name = "btnAddLine"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 55
$btnAddLine.Size = $System_Drawing_Size
$btnAddLine.TabIndex = 1
$btnAddLine.Text = "Add line"
$btnAddLine.UseVisualStyleBackColor = $True
$btnAddLine.add_Click($handler_btnAddLine_Click)
$form1.Controls.Add($btnAddLine)
$txt2.Anchor = 14
$txt2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 180
$txt2.Location = $System_Drawing_Point
$txt2.Name = "txt2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 165
$txt2.Size = $System_Drawing_Size
$txt2.TabIndex = 0
$form1.Controls.Add($txt2)
$txt1.Anchor = 15
$txt1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$txt1.Location = $System_Drawing_Point
$txt1.Multiline = $True
$txt1.Name = "txt1"
$txt1.ScrollBars = 2
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 151
$System_Drawing_Size.Width = 226
$txt1.Size = $System_Drawing_Size
$txt1.TabIndex = 2
$txt1.ReadOnly = $true
$txt1.add_TextChanged($handler_txt1_TextChanged)
$form1.Controls.Add($txt1)
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
GenerateForm
p.s. Es wäre von Vorteil wenn du zumindest das Wort "Powershell" in deinem Titel erwähnen könntest, so dass auch andere den Thread finden können und direkt wissen worum es geht. Danke.
Keine Ursache. Freut mich wenn ich helfen konnte
Ist angepasst, ich war wohl etwas schnell mit klicken
Merci.