首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

listbox 有关问题

2012-01-19 
listbox 问题请问listbox的item上有没有DoubleClick和Click事件?我要实现双击listbox的item输出该item是否

listbox 问题
请问listbox的item上有没有DoubleClick和Click事件?

我要实现双击listbox的item输出该item
是否能用SelectedItems

  Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
  If ListBox1.Items.Count = 0 Then
  Return
  End If
  MsgBox(ListBox1.SelectedItem)
  End Sub

但是当双击下边空白处时 
我希望输出 MsgBox(“none”)

请问ListBox有没有item的DoubleClick和Click事件?
或者其他实现的方法



[解决办法]
ListBox有DoubleClick事件和Click事件,但没有到应到具体的Item上,你需要在控件的默认的DoubleClick及Click事件里来处理,可以使用SelectedIndex或SelectedItem来获取当前选中的Item。
[解决办法]
不知道有没有解决你的问题。。。

VB.NET code
 Private MsePoint As Point    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick        If ListBox1.Items.Count = 0 Then            Return        End If                If ListBox1.IndexFromPoint(MsePoint.X, MsePoint.Y) = -1 Then            MsgBox("NONE")        Else            MsgBox(ListBox1.SelectedItem)        End If    End Sub    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove        MsePoint = New Point(e.X, e.Y)    End Sub
[解决办法]
稍微改改:
VB.NET code
Private MsePoint As Point    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick        If ListBox1.Items.Count = 0 Then            Return        End If                If ListBox1.IndexFromPoint(MsePoint.X, MsePoint.Y) = -1 Then            MsgBox("NONE")        Else            MsgBox(ListBox1.SelectedItem)        End If    End Sub    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown        MsePoint = New Point(e.X, e.Y)        If e.Button = MouseButtons.Right Then            Dim sindex As Integer = ListBox1.IndexFromPoint(MsePoint.X, MsePoint.Y)            If sindex <> -1 Then                ListBox1.SelectedIndex = sindex            End If        End If    End Sub 

热点排行