我做一个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, "")
}
}
您能告诉我如何通过鼠标悬停在我的列表框上执行此代码吗? 或者为列表框的每个项目显示不同文本的工具提示的另一种方法是什么? 谢谢
答案 0 :(得分:1)
您能告诉我如何通过鼠标悬停在我的列表框上执行此代码吗?
要在悬停事件中查找鼠标位置,首先您可以使用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.GetItemText
方法,该方法优于ToString
项方法。如果您将复杂对象设置为列表框的数据源并设置显示成员属性,它将根据显示名称提取项目文本,否则返回项目的ToString
。
另请不要忘记,要处理MouseHover
事件,您需要使用Add_MouseHover
。
答案 1 :(得分:0)
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#中处理这类事情。
答案 2 :(得分:0)
解决方案:
#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]))
}
})