鼠标悬停列表框项目上的 POWERSHELL 工具提示

POWERSHELL tooltip on mousehover listbox item


我做一个 Powershell GUI,我想在列表框上使用工具提示, 但我不熟悉事件和事件处理程序,我没有在 Microsoft.com
上找到 powershell/winform 事件的帮助 在我的列表框下面是 $listbox_groupe_import

#Infobulle au survol pour voir les tables d'un groupe de table
$obj_infobulle = New-Object System.Windows.Forms.ToolTip 
$obj_infobulle.InitialDelay = 100     
$obj_infobulle.ReshowDelay = 100 

#Sélectionne tous les groupes de tables dans la base de données et les met dans une liste déroulante 
$listbox_groupe_import = Get-ListboxGroup
#Création d'une info bulle pour la Listbox.
$obj_infobulle.SetToolTip($listBox_groupe_import, "tooltip sur listbox")

我想在鼠标悬停时设置工具提示
我找到了这个,但我不知道如何执行它:

$listboxGroupe_MouseMove = [System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    #TODO: Place custom script here

    #index vaut ce qu'on pointe avec la souris au dessus de listbox1
    $index = $listBox_groupe.IndexFromPoint($_.Location)     #$_ => this (listbox.location) je crois
    ##"index ="+$index
    ##$tooltip1.SetToolTip($listBox_groupe, "index ="+$index)

    if ($index -ne -1){ 
        #Tooltype sur listbox1 = valeur de l'item pointé
        $tooltip1.SetToolTip($listBox_groupe, $listBox_groupe.Items[$index].ToString()) 
    }
    else{ 
        #on n'est pas au dessus de listBox_groupe
        $tooltip1.SetToolTip($listBox_groupe, "") 
    }
}

你能告诉我如何通过鼠标悬停在我的列表框上来执行这段代码吗?
或者用另一种方式为我的列表框的每一项显示不同文本的工具提示?
谢谢

Here's the documentation。尤其要注意底部的事件。您要添加一个 MouseHover 事件:

$MyListBox.add_MouseHover({
    # display your tooltip here
})
$MyListBox.add_MouseLeave({
    # remove the tooltip now that user moved away
})

PowerShell GUI 和事件处理程序没有很好的文档记录,因为您通常希望在 C# 中处理此类事情。

Can you tell me how to execute this code by mousehover on my listbox?

要在悬停事件中找到鼠标位置,首先可以使用Control.MousePosition 找到鼠标屏幕位置,然后使用ListBox.PointToClient,将其转换为控件上的鼠标位置。那么剩下的逻辑和你已有的类似:

$point = $listBox.PointToClient([System.Windows.Forms.Control]::MousePosition)
$index = $listBox.IndexFromPoint($point)
if($index -ge 0) {
    $toolTip.SetToolTip($listBox, $listBox.GetItemText($listBox.Items[$index]))
}
else {
    $toolTip.SetToolTip($listBox, "")
}

只是为了让它更好一点,我使用了 ListBox.GetItemTextToString 方法更好的项目方法。如果您将复杂对象设置为列表框的数据源并设置显示成员 属性,它会根据显示名称提取项目文本,否则它会 returns ToString项目。

另外别忘了,要处理MouseHover事件,你需要使用Add_MouseHover.

解决方案:

#Au survol
$listBox_groupe.add_MouseEnter({

    #récupérer la position de la souris
    $point = $listBox_groupe.PointToClient([System.Windows.Forms.Control]::MousePosition)

    #récupérer l'indice de l'item sur lequel on pointe
    $index = $listBox_groupe.IndexFromPoint($point)

    if($index -ge 0) {
        #l'infobulle est au dessus de listBox_groupe et elle a pour texte le texte de l'item
        $tooltip1.SetToolTip($listBox_groupe, $listBox_groupe.GetItemText($listBox_groupe.Items[$index]))
    }
})