Winforms 在Powershell中聚焦/选择ListView项

Winforms 在Powershell中聚焦/选择ListView项,winforms,listview,powershell,scroll,selected,Winforms,Listview,Powershell,Scroll,Selected,我从PowerShell获得了一个窗口形式的Listview控件,该控件作为活动日志动态填充,我基本上希望它自动滚动,以便最后一个条目始终处于焦点位置 我可以通过将项目设置为selected来使用ListBox,但这对于ListView似乎不起作用。我可以将其设置为Selected和Focused,但仅当实际的表单控件是目标控件时,即使这样,它也不会导致滚动条移动 如果每次更新时我都能让它滚动到页面底部,那就好了,但我更愿意关注一个特定的项目,因为我有两个组,如果顶部组中的一个项目也能被关注就好

我从PowerShell获得了一个窗口形式的Listview控件,该控件作为活动日志动态填充,我基本上希望它自动滚动,以便最后一个条目始终处于焦点位置

我可以通过将项目设置为selected来使用ListBox,但这对于ListView似乎不起作用。我可以将其设置为Selected和Focused,但仅当实际的表单控件是目标控件时,即使这样,它也不会导致滚动条移动

如果每次更新时我都能让它滚动到页面底部,那就好了,但我更愿意关注一个特定的项目,因为我有两个组,如果顶部组中的一个项目也能被关注就好了

下面是部分代码

$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
和Add函数

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
}
非常感谢您的帮助!
BR Erik

我会这样做

#Dll Import
Function ScrollbarImport
{
  Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public static class Scrollbar {
  [DllImport("user32.dll")]
  public static extern int GetScrollPos(IntPtr hWnd, int nBar);
  [DllImport("user32.dll")]
  public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
  [DllImport("user32.dll")]
  public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr     wParam, IntPtr lParam);
      }
"@ 
}
保存滚动条的位置

$Script:ScrollbarX = [Scrollbar]::GetScrollPos($SQLProcessesListview.handle, 0)
$Script:ScrollbarY = [Scrollbar]::GetScrollPos($SQLProcessesListview.handle, 1)
设置保存的位置

[Scrollbar]::SendMessage($SQLProcessesListview.Handle, 4116 ,(0 - [Int]$Script:ScrollbarX),(0 - [Int]$Script:ScrollbarY))
[Scrollbar]::SendMessage($SQLProcessesListview.Handle, 4116 , [Int]$Script:ScrollbarX,([Int]$Script:ScrollbarY * 17))
你好,詹尼克