首先请大家看一下这段小程序,功能就是在LISTBOX中按上、下键来显示当前项的索引,按回车键把LISTBOX中项目反选后下移。-->请记住:这个LISTBOX的MultiSelect=Simple(VB.NET的是:SelectionMode=MultiSimple)
Private Sub Form_Load()
Dim i As Integer
While i < 50
    List1.AddItem ("Test" & Str(i))
i = i + 1
Wend
End SubPrivate Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
    Case vbKeyDown, vbKeyUp
            Text1.Text = Str(List1.ListIndex)
    Case vbKeyReturn
            List1.Selected(List1.ListIndex) = Not List1.Selected(List1.ListIndex)
            If List1.ListIndex < List1.ListCount - 1 Then
                List1.ListIndex = List1.ListIndex + 1
                Text1.Text = Str(List1.ListIndex)
            End If
            
End Select
End Sub
在VB.NET中,如果我把LISTBOX的SelectionMode=MultiSimple(请记住,是SelectionMode=MultiSimple),那么要实现以上的功能怎么实现呢?
我想过可以用一个全局变量来记录按键盘的上、下键,这就相对于VB6中的ListIndex,但还有一个是VB6中的List1.Selected(List1.ListIndex) = Not List1.Selected(List1.ListIndex)这一句,在VB.NET中怎么实现???
那位能把以上的VB6的代码改为VB.NET啊?(但记住,SelectionMode=MultiSimple)

解决方案 »

  1.   

    没想到.Net下ListBox没有ListIndex还挺费劲的啊!:)    Dim m_CurrentIndex As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer
            While i < 50
                ListBox1.Items.Add("Test" & Str(i))
                i = i + 1
            End While        m_CurrentIndex = 0    End Sub
        Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown        Select Case e.KeyCode            Case Keys.Down
                    If m_CurrentIndex = ListBox1.Items.Count - 1 Then Exit Sub
                    m_CurrentIndex = m_CurrentIndex + 1
                    TextBox1.Text = CStr(m_CurrentIndex)
                Case Keys.Up
                    If m_CurrentIndex = 0 Then Exit Sub
                    m_CurrentIndex = m_CurrentIndex - 1
                    TextBox1.Text = CStr(m_CurrentIndex)
                Case Keys.Enter
                    ListBox1.SetSelected(m_CurrentIndex, Not ListBox1.GetSelected(m_CurrentIndex))
                    SendKeys.Send("{DOWN}")
            End Select
        End Sub
        Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            Dim index As Integer
            index = ListBox1.IndexFromPoint(e.X, e.Y)
            If index <> -1 Then m_CurrentIndex = index
        End Sub
      

  2.   

    TO :benyfeifei(狒狒) 
    请问如果在LISTBOX中到了最后一项,我想它返回到第一项,象VB6中的LIST1.LISTINDEX=0如何实现??
    谢谢!!
      

  3.   

    哦,知道了,用SendKeys.Send("{HOME}")来实现
    好,结贴咯!!!