Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing frmUserSelection.vb (5.63 KB)

Option Explicit On

Public Class frmUserSelection

    Private _objUserSelection As clsUserSelection = Nothing

    Public Sub New(ByVal objUserSelection As clsUserSelection, _
        Optional ByVal caption As String = Nothing)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _objUserSelection = objUserSelection

        ' get column and row counts
        Dim colCount As Integer = _objUserSelection.GetColumnCount()
        Dim rowCount As Integer = _objUserSelection.GetRowCount()

        ' build the listview columns
        For i As Integer = 1 To colCount

            With lvData.Columns.Add(_objUserSelection.GetColumn(i))
                .Width = CInt(Math.Round(lvData.ClientSize.Width / colCount) - (20 / colCount))
            End With

        Next

        ' build the listview rows
        For i As Integer = 1 To rowCount

            Dim thisRow() As String = _objUserSelection.GetRow(i)
            Dim thisItem As ListViewItem = lvData.Items.Add(thisRow(0))

            For x As Integer = 1 To thisRow.GetUpperBound(0)
                thisItem.SubItems.Add(thisRow(x))
            Next

        Next

        If Not (caption Is Nothing) Then Me.Text = caption

        If Not _objUserSelection.AllowCancel Then

            btnCancel.Visible = False
            btnChoose.Left = (Me.Width - btnChoose.Width) / 2

        End If

    End Sub

    Private Sub _showWaitCursor(ByVal bShow As Boolean)

        ' show/hide the hourglass
        If bShow Then
            Me.Cursor = Cursors.WaitCursor
            Me.UseWaitCursor = True
        Else
            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Arrow
        End If

    End Sub

    Private Sub lvData_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvData.KeyUp

        ' check for the find hotkey
        If e.KeyCode = Keys.F3 Then btnFind_Click(sender, Nothing)

    End Sub

    Private Sub lvData_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvData.MouseDoubleClick

        If lvData.SelectedItems.Count > 0 Then btnChoose.PerformClick()

    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click

        If lvData.SelectedItems.Count = 0 Then

            MessageBox.Show("Please make a selection.", _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            lvData.Focus()

        Else

            ' store the index of the selected item
            _objUserSelection.retIndex = lvData.SelectedItems(0).Index
            Me.Close()

        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        _objUserSelection.retIndex = -1
        Me.Close()

    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        Static strInput As String = ""
        Dim thisInput As String = ""

        If e Is Nothing And strInput <> "" Then
            thisInput = strInput
        Else
            thisInput = InputBox("Enter the text to find:", "Find", strInput)
        End If

        strInput = thisInput
        thisInput = thisInput.ToLower

        If thisInput = "" Then Exit Sub

        Dim thisItem As ListViewItem = Nothing
        Dim bFromBeginning As Boolean = False

        If lvData.SelectedItems.Count > 0 Then

            thisItem = lvData.SelectedItems(0)

            If thisItem.Index + 1 < lvData.Items.Count Then

                thisItem = lvData.Items(thisItem.Index + 1)

            ElseIf lvData.Items.Count > 0 Then

                thisItem = lvData.Items(0)
                bFromBeginning = True

            End If

        ElseIf lvData.Items.Count > 0 Then

            thisItem = lvData.Items(0)
            bFromBeginning = True

        End If

        _showWaitCursor(True)

        Do While Not (thisItem Is Nothing)

            If thisItem.Text.ToLower.Contains(thisInput) Then _
                Exit Do

            If thisItem.SubItems.Count > 0 Then

                For Each thisSubItem As ListViewItem.ListViewSubItem In thisItem.SubItems

                    If thisSubItem.Text.ToLower.Contains(thisInput) Then _
                        Exit Do

                Next

            End If

            If thisItem.Index + 1 < lvData.Items.Count Then

                thisItem = lvData.Items(thisItem.Index + 1)

            ElseIf Not bFromBeginning AndAlso _
                MessageBox.Show("Cannot find """ + strInput + """" + vbCrLf + vbCrLf + _
                "Would you like to search from the beginning?", _
                Application.ProductName, MessageBoxButtons.OKCancel, _
                MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then

                thisItem = lvData.Items(0)
                bFromBeginning = True

            Else

                thisItem = Nothing

            End If

        Loop

        _showWaitCursor(False)

        If Not (thisItem Is Nothing) Then

            thisItem.EnsureVisible()
            thisItem.Focused = True
            thisItem.Selected = True

        Else

            MessageBox.Show("Cannot find """ + strInput + """", _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub

End Class

Download frmUserSelection.vb

Back to file list


Back to project page