Listview

timed

Newcomer
Joined
Jan 9, 2003
Messages
8
Location
Sioux Falls, South Dakota
I am new to Vb.net. And I thank all who help me out. I am tring to get the index of a item in a listview, so I can populate the database with that item. I am having trouble do this. I do have a ItemCheck method and it is here where i am tring to get the index. Here is the code I am tring to do this in. Like I sain I am new to this and any coments that will help me improve will be great. Thanks again for the help.

Visual Basic:
Public Sub LoadListView()

        Dim cmdAppt As SqlCommand
        Dim frmMain As frmSearch


        frmMain = New frmSearch

        frmMain.Connect()

        cmdAppt = New SqlCommand
        With cmdAppt
            .Connection = frmMain.mconMain
            .CommandType = CommandType.StoredProcedure
            .CommandText = "sGetAllAppointments"
        End With
        Try
            'frmMain.mconMain.Open()
            drAppt = cmdAppt.ExecuteReader
        Catch connectionError As System.Data.SqlClient.SqlException
            MessageBox.Show(connectionError.ToString)
        End Try

        'Set the List to Detail View
        lvwAppt.View = View.Details
        lvwAppt.CheckBoxes = True
        lvwAppt.MultiSelect = False

        'To Activate an Item you must doubleclick the item
        'This will fire the 
        lvwAppt.Activation = ItemActivation.Standard

        'Add Columns 
        'Lets Get the Column names from the DataSet and use Them
        lvwAppt.Columns.Add("ID", 50, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Date", 150, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Location", 175, _
             HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Room", 100, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Address", 200, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("City", 75, _
             HorizontalAlignment.Center)

        lvwAppt.Columns.Add("State", 30, _
             HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Phone", 100, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Fax", 100, _
            HorizontalAlignment.Center)

        lvwAppt.Columns.Add("Start Time", 100, _
             HorizontalAlignment.Center)

        lvwAppt.Columns.Add("End Time", 100, _
             HorizontalAlignment.Center)

        Do While drAppt.Read
            'Create ListViewItem
            Dim itmp As New Windows.Forms.ListViewItem

            itmp.Text = drAppt("iAppointmentID") & ""

            'Modify Some of the Items Properties
            itmp.BackColor = System.Drawing.Color.Aquamarine
            itmp.ForeColor = System.Drawing.Color.Navy
            itmp.Checked = False

            'Create SubItem 1
            Dim itms1 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cDateTime") & "")

            'Create SubItem 2
            Dim itms2 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cLocation") & "")

            'Create SubItem 3
            Dim itms3 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cRoom") & "")

            'Create SubItem 4
            Dim itms4 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cAddress") & "")

            'Create SubItem 5
            Dim itms5 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cCity") & "")

            'Create SubItem 6
            Dim itms6 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cState") & "")

            'Create SubItem 7
            Dim itms7 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cPhone") & "")

            'Create SubItem 8
            Dim itms8 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cFax") & "")

            'Create SubItem 9
            Dim itms9 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cStartTime") & "")

            'Create SubItem 10
            Dim itms10 As New Windows.Forms.ListViewItem.ListViewSubItem _
                (itmp, drAppt("cEndTime") & "")

            'Add SubItems to parent Item
            itmp.SubItems.Add(itms1)
            itmp.SubItems.Add(itms2)
            itmp.SubItems.Add(itms3)
            itmp.SubItems.Add(itms4)
            itmp.SubItems.Add(itms5)
            itmp.SubItems.Add(itms6)
            itmp.SubItems.Add(itms7)
            itmp.SubItems.Add(itms8)
            itmp.SubItems.Add(itms9)
            itmp.SubItems.Add(itms10)

            'Add Parent Item to ListView Control
            lvwAppt.Items.Add(itmp)

        Loop

    End Sub

    Private Sub lvwAppt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwAppt.SelectedIndexChanged
        Dim selectedItem As ListViewItem

        selectedItem = lvwAppt.SelectedItems(iIndex)

        ' Do something with selectedItem
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchedule.Click
        If lvwAppt.SelectedItems.Count > 2 Then
            MessageBox.Show("You can only select one location.")
        Else
            'Do something here
        End If
    End Sub

    Private Sub lvwAppt_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvwAppt.ItemCheck
'***********************************
'[B]This is where i cant figure out the index[/B] 
    End Sub
 
Last edited by a moderator:
The ItemCheckEventArgs passed to the event handler has an
Index property which conatains the Index of the item.

Visual Basic:
Private Sub lvwAppt_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvwAppt.ItemCheck

Dim itemIndex As Integer = e.Index

' itemIndex is the index of the item being changed
End Sub
 
Back
Top