internet2107
Goto Top

Powershell Listbox Items verschieben

Hallo.

Ist es möglich, dass man Items in einer Listbox mit einer Art Button verschiebt?
Ich lade die Einträge der Listbox mit einem Array.
Alles klappt auch einwandfrei.

Nur möchte ich gerne die angezeigten Einträge in der Liste nach Möglichkeit, nach Bedarf verschieben.

Beispiel:
IST-Zustand
Eintrag 1
Eintrag 2
Eintrag 3
Eintrag 4
...usw


SOLL-Zustand
nach drücken eines Buttons soll man die Liste entsprechend verändern...hoch ...runter

Eintrag 1
Eintrag 4
Eintrag 2
Eintrag 3
...usw

$listbox1.SelectedItems 
----> move up
----> move down

Content-Key: 328239

Url: https://administrator.de/contentid/328239

Ausgedruckt am: 28.03.2024 um 19:03 Uhr

Mitglied: 132272
Lösung 132272 02.02.2017 aktualisiert um 12:59:38 Uhr
Goto Top
Hier stehts
Regeln in Outlook 2013 verwalten: Suchliste sortieren?

function FormEntryEditor {

#region Generated Form Objects
$formEntryEditor = New-Object System.Windows.Forms.Form
$btnSort = New-Object System.Windows.Forms.Button
$btnToBottom = New-Object System.Windows.Forms.Button
$btnToTop = New-Object System.Windows.Forms.Button
$btnRemove = New-Object System.Windows.Forms.Button
$btnAdd = New-Object System.Windows.Forms.Button
$txtAdd = New-Object System.Windows.Forms.TextBox
$btnCancel = New-Object System.Windows.Forms.Button
$btnSave = New-Object System.Windows.Forms.Button
$btnDown = New-Object System.Windows.Forms.Button
$btnUp = New-Object System.Windows.Forms.Button
$lbItems = New-Object System.Windows.Forms.ListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

$handler_btnCancel_Click= 
{
    $Global:result = $false
}

$handler_btnDown_Click= 
{
    $lbItems.Sorted = $false
    $intSelIndex = $lbItems.SelectedIndex
    if ($intSelIndex -lt $lbItems.Items.Count -1 -and $intSelIndex -ne -1){
        $val = $lbItems.SelectedItem
        $lbItems.Items.RemoveAt($intSelIndex)
        $lbItems.Items.Insert($intSelIndex +1,$val)
        $lbItems.SelectedIndex = $intSelIndex +1
    }
}

$handler_btnUp_Click= 
{
    $lbItems.Sorted = $false
    $intSelIndex = $lbItems.SelectedIndex
    if ($intSelIndex -gt 0){
        $val = $lbItems.SelectedItem
        $lbItems.Items.RemoveAt($intSelIndex)
        $lbItems.Items.Insert($intSelIndex -1,$val)
        $lbItems.SelectedIndex = $intSelIndex -1
    }

}

$handler_formEntryEditor_Load= 
{

}

$handler_btnRemove_Click= 
{
    . $removeListBoxEntry
}

$handler_btnAdd_Click= 
{
    if ($txtAdd.Text -ne ""){  
        $lbItems.Sorted = $false
        $intSelIndex = $lbItems.SelectedIndex
        if ($intSelIndex -eq -1){
            $lbItems.Items.Add($txtAdd.Text)
        }else{
            $lbItems.Items.Insert($intSelIndex,$txtAdd.Text)
        }
        $txtAdd.Text = ""  
    }
}

$handler_formEntryEditor_FormClosed= 
{

}

$handler_btnSave_Click= 
{
    $arrNewOrder = [string[]]$lbItems.Items
    $Script:result = $arrNewOrder
}


$btnToBottom_OnClick= 
{
    $lbItems.Sorted = $false
    $intSelIndex = $lbItems.SelectedIndex
    if ($intSelIndex -ne -1){
        $val = $lbItems.SelectedItem
        $lbItems.Items.RemoveAt($intSelIndex)
        $lbItems.Items.Insert($lbItems.Items.Count,$val)
        $lbItems.SelectedIndex = $lbItems.Items.Count - 1
    }

}

$btnToTop_OnClick= 
{
    $lbItems.Sorted = $false
    $intSelIndex = $lbItems.SelectedIndex
    if ($intSelIndex -ne -1){
        $val = $lbItems.SelectedItem
        $lbItems.Items.RemoveAt($intSelIndex)
        $lbItems.Items.Insert(0,$val)
        $lbItems.SelectedIndex = 0
    }
}

$handler_lbItems_SelectedIndexChanged= 
{

}

$handler_btnSort_Click= 
{
    $lbItems.Sorted = $true
}

$handler_lbItems_KeyUp= 
{
    switch ($_.KeyCode){
        "Delete" {  
            . $removeListBoxEntry
        }
    }
}

$handler_txtAdd_KeyDown = {
    if ($_.KeyCode -eq "Enter"){. $handler_btnAdd_Click}  
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$formEntryEditor.WindowState = $InitialFormWindowState
}

$removeListBoxEntry = {
    if ($lbItems.SelectedIndex -ne -1){
        $oldIndex = $lbItems.SelectedIndex
        $lbItems.Items.RemoveAt($lbItems.SelectedIndex)
        if ($lbItems.Items.Count -gt $oldIndex){$lbItems.SelectedIndex = $oldIndex}
    }

}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 300
$System_Drawing_Size.Width = 337
$formEntryEditor.ClientSize = $System_Drawing_Size
$formEntryEditor.DataBindings.DefaultDataSourceUpdateMode = 0
$formEntryEditor.FormBorderStyle = 6
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 326
$System_Drawing_Size.Width = 345
$formEntryEditor.MinimumSize = $System_Drawing_Size
$formEntryEditor.Name = "formEntryEditor"  
$formEntryEditor.StartPosition = 4
$formEntryEditor.ShowInTaskbar = $False
$formEntryEditor.Text = "Einträge bearbeiten"  
$formEntryEditor.add_Load($handler_formEntryEditor_Load)
$formEntryEditor.add_FormClosed($handler_formEntryEditor_FormClosed)

$btnSort.Anchor = 9

$btnSort.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 227
$btnSort.Location = $System_Drawing_Point
$btnSort.Name = "btnSort"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnSort.Size = $System_Drawing_Size
$btnSort.TabIndex = 11
$btnSort.Text = "A-Z"  
$btnSort.UseVisualStyleBackColor = $True
$btnSort.add_Click($handler_btnSort_Click)

$formEntryEditor.Controls.Add($btnSort)

$btnToBottom.Anchor = 9

$btnToBottom.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 175
$btnToBottom.Location = $System_Drawing_Point
$btnToBottom.Name = "btnToBottom"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnToBottom.Size = $System_Drawing_Size
$btnToBottom.TabIndex = 10
$btnToBottom.Text = "Nach unten"  
$btnToBottom.UseVisualStyleBackColor = $True
$btnToBottom.add_Click($btnToBottom_OnClick)

$formEntryEditor.Controls.Add($btnToBottom)

$btnToTop.Anchor = 9

$btnToTop.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 88
$btnToTop.Location = $System_Drawing_Point
$btnToTop.Name = "btnToTop"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnToTop.Size = $System_Drawing_Size
$btnToTop.TabIndex = 9
$btnToTop.Text = "Nach oben"  
$btnToTop.UseVisualStyleBackColor = $True
$btnToTop.add_Click($btnToTop_OnClick)

$formEntryEditor.Controls.Add($btnToTop)

$btnRemove.Anchor = 9

$btnRemove.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 39
$btnRemove.Location = $System_Drawing_Point
$btnRemove.Name = "btnRemove"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnRemove.Size = $System_Drawing_Size
$btnRemove.TabIndex = 8
$btnRemove.Text = "Entfernen"  
$btnRemove.UseVisualStyleBackColor = $True
$btnRemove.add_Click($handler_btnRemove_Click)

$formEntryEditor.Controls.Add($btnRemove)

$btnAdd.Anchor = 9

$btnAdd.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 10
$btnAdd.Location = $System_Drawing_Point
$btnAdd.Name = "btnAdd"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnAdd.Size = $System_Drawing_Size
$btnAdd.TabIndex = 7
$btnAdd.Text = "Hinzufügen"  
$btnAdd.UseVisualStyleBackColor = $True
$btnAdd.add_Click($handler_btnAdd_Click)

$formEntryEditor.Controls.Add($btnAdd)

$txtAdd.Anchor = 13
$txtAdd.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$txtAdd.Location = $System_Drawing_Point
$txtAdd.Name = "txtAdd"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 226
$txtAdd.Size = $System_Drawing_Size
$txtAdd.TabIndex = 6
$txtAdd.add_KeyDown($handler_txtAdd_KeyDown)

$formEntryEditor.Controls.Add($txtAdd)

$btnCancel.Anchor = 10

$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0
$btnCancel.DialogResult = 2

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 210
$System_Drawing_Point.Y = 265
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 109
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 4
$btnCancel.Text = "Abbrechen"  
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)

$formEntryEditor.Controls.Add($btnCancel)

$btnSave.Anchor = 10

$btnSave.DataBindings.DefaultDataSourceUpdateMode = 0
$btnSave.DialogResult = 1

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 88
$System_Drawing_Point.Y = 265
$btnSave.Location = $System_Drawing_Point
$btnSave.Name = "btnSave"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 116
$btnSave.Size = $System_Drawing_Size
$btnSave.TabIndex = 3
$btnSave.Text = "Speichern"  
$btnSave.UseVisualStyleBackColor = $True
$btnSave.add_Click($handler_btnSave_Click)

$formEntryEditor.Controls.Add($btnSave)

$btnDown.Anchor = 9

$btnDown.DataBindings.DefaultDataSourceUpdateMode = 0
$btnDown.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,1)  

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 146
$btnDown.Location = $System_Drawing_Point
$btnDown.Name = "btnDown"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnDown.Size = $System_Drawing_Size
$btnDown.TabIndex = 2
$btnDown.Text = "˅"  
$btnDown.UseVisualStyleBackColor = $True
$btnDown.add_Click($handler_btnDown_Click)

$formEntryEditor.Controls.Add($btnDown)

$btnUp.Anchor = 9

$btnUp.DataBindings.DefaultDataSourceUpdateMode = 0
$btnUp.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,1)  

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 244
$System_Drawing_Point.Y = 117
$btnUp.Location = $System_Drawing_Point
$btnUp.Name = "btnUp"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnUp.Size = $System_Drawing_Size
$btnUp.TabIndex = 1
$btnUp.Text = "˄"  
$btnUp.UseVisualStyleBackColor = $True
$btnUp.add_Click($handler_btnUp_Click)

$formEntryEditor.Controls.Add($btnUp)

$lbItems.Anchor = 15
$lbItems.DataBindings.DefaultDataSourceUpdateMode = 0
$lbItems.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 38
$lbItems.Location = $System_Drawing_Point
$lbItems.Name = "lbItems"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 212
$System_Drawing_Size.Width = 226
$lbItems.Size = $System_Drawing_Size
$lbItems.TabIndex = 0
$lbItems.add_SelectedIndexChanged($handler_lbItems_SelectedIndexChanged)
$lbItems.add_KeyUp($handler_lbItems_KeyUp)

$formEntryEditor.Controls.Add($lbItems)

#endregion Generated Form Code

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

Gruß
Mitglied: 132272
Lösung 132272 03.02.2017 um 15:01:48 Uhr
Goto Top
Wenns das war Thread noch schließen.