I got a Listview control in a window form running from PowerShell that is being populated dynamically as an activity log and I basically want it to auto scroll so the last entry always is in focus.
I can get this to work with a ListBox by just setting the Item to selected but this doesn't seem to work for a ListView. I can set it to both Selected and Focused but only when the actual form control is targeted and even then it doesn't cause the scrollbar to move.
It would be fine if I just could get it to scroll to the bottom of the page on every update but would prefer to actually focus on a specific item since I got two groups and it would be good if an item from the top group could be focused as well.
Here's parts of the code
$listView1.View = 'Details'
$ListView1.Anchor = 'Top, Bottom, Left, Right'
$ListView1.Location = '0, 399'
$ListView1.Name = "ListView1"
$ListView1.Size = '683, 200'
$ListView1.TabIndex = 10
$listView1.FullRowSelect = $true
$listView1.MultiSelect = $False
$listColumnTime.Text = "Time"
$listColumnTime.Width = 90
$listColumnAction.Text = "Action"
$listColumnAction.Width = 100
$listColumnStatus.Text = "Status"
$listColumnStatus.Width = -2
$ListGroupUpdate.Header = "Updates"
$ListGroupUpdate.Tag = 0
$ListGroupAction.Header = "Actions"
$ListGroupAction.Tag = 1
$listView1.Columns.Add($listColumnTime)|Out-Null
$listView1.Columns.Add($listColumnAction)|Out-Null
$listView1.Columns.Add($listColumnStatus)|Out-Null
$listView1.Groups.Add($ListGroupUpdate)|Out-Null
$listView1.Groups.Add($ListGroupAction)|Out-Null
And the Add function
function Add-ListViewItem
{
Param(
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.ListView]$ListView,
[Parameter(Mandatory=$true)]
[string[]]$Items,
[Parameter(Mandatory=$true)]
[int]$Group,
[Parameter(Mandatory=$false)]
$timestamp = (get-date -Format "MM/dd HH:mm:ss"),
[switch]$Clear)
if($Clear)
{
$ListView1.Items.Clear();
}
$listitem = New-Object 'System.Windows.Forms.ListViewItem'
$listitem.text = $Timestamp
$listitem.Group = $listview.Groups[$group]
$listitem.SubItems.AddRange($Items)
$listitem.Selected = $true
$ListView.Items.Add($listitem)
$ListView.FocusedItem = $listitem
}
Would be very grateful for any help! BR Erik
Aucun commentaire:
Enregistrer un commentaire