Winforms 鼠标悬停列表框项上的POWERSHELL工具提示
Winforms 鼠标悬停列表框项上的POWERSHELL工具提示,winforms,powershell,events,tooltip,Winforms,Powershell,Events,Tooltip,我使用Powershell GUI,我想在列表框上使用工具提示, 但我不熟悉事件和事件处理程序,我在Microsoft.com上找不到有关powershell/winform事件的帮助 我的列表框下面是$listbox\u groupe\u import #Infobulle au survol pour voir les tables d'un groupe de table $obj_infobulle = New-Object System.Windows.Forms.ToolTip $
我使用Powershell GUI,我想在列表框上使用工具提示, 但我不熟悉事件和事件处理程序,我在Microsoft.com上找不到有关powershell/winform事件的帮助
我的列表框下面是$listbox\u groupe\u 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, "")
}
}
您能告诉我如何通过鼠标悬停在我的列表框上执行此代码吗?或者用另一种方式为我的列表框的每个项目显示具有不同文本的工具提示?
谢谢你。特别是,看看底部的事件。有一个
MouseHover
事件需要添加:
$MyListBox.add_MouseHover({
# display your tooltip here
})
$MyListBox.add_MouseLeave({
# remove the tooltip now that user moved away
})
PowerShell GUI和事件处理程序没有很好的文档记录,因为您通常希望在C#中处理这类事情
你能告诉我如何用鼠标在我的列表框上执行这个代码吗
要在悬停事件中查找鼠标位置,首先可以使用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\u 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]))
}
})
谢谢,但这是所有listbox的事件,这不是我需要的:我需要此listbox中每个项目的事件,$MyListBox.Items.Items(1)。add_MouseHover{}不需要work@Kishiro然后查看
ListBoxItem
的文档,找到合适的事件(可能是相同的),并将我的答案应用于该情况。@Kishiro。该事件名为MouseEnter
和MouseLeave
或PreviewMouseMove
谢谢,所以我尝试了$listBox_-groupe.Items.Item(1).PreviewMouseMove({})和$listBox_-groupe.Items.Item(1).添加_-PreviewMouseMove({}),但它是由系统编写的。string没有这些方法…非常感谢,这正是我想要的!