Hello,

Please help...

I have a small application. When I click on a ListView row to Edit the details populated from the MS Access DB, I have the Else statement being executed all the time. Initially the code is attached behind a button named Edit. I have a Refresh button also which is clicked before the Edit button is clicked. Thus I just don't want to click on the OK button on the message box generated from the Else part. I just want to switch the Edit option between the ListView data when the ListView is item is clicked. Please help edit my code below and provide a solution. :confused:

The code is as shown below:

Thanking you in advance.

Private Sub lvData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvData.SelectedIndexChanged
        Me.dtmDateOfBirth.Visible = False
        Me.txtDateOfBirth.Visible = True
        'Check if there is a row selected on the listview
        If lvData.SelectedIndices.Count > 0 Then
            'Grab the selected index

            Dim ItemNo As Integer = lvData.SelectedIndices(0)

            EDITMODE = True
            NEWMODE = False

            lblExecutiveID.Text = lvData.Items(ItemNo).SubItems(0).Text()
            txtGivenName.Text = lvData.Items(ItemNo).SubItems(1).Text()
            txtFamilyName.Text = lvData.Items(ItemNo).SubItems(2).Text()
            cmbGender.Text = lvData.Items(ItemNo).SubItems(3).Text()
            txtATSAPosition.Text = lvData.Items(ItemNo).SubItems(4).Text()
            txtInstitution.Text = lvData.Items(ItemNo).SubItems(5).Text()
            txtCourseOfStudy.Text = lvData.Items(ItemNo).SubItems(6).Text()
            cmbYearOfStudy.Text = lvData.Items(ItemNo).SubItems(7).Text()
            'If edit then enter the data on the textbox called txtDateOfBirth
            txtDateOfBirth.Text = lvData.Items(ItemNo).SubItems(8).Text()
            txtHomeVillage.Text = lvData.Items(ItemNo).SubItems(9).Text()
            txtHomeDistrict.Text = lvData.Items(ItemNo).SubItems(10).Text()
            txtHomeProvince.Text = lvData.Items(ItemNo).SubItems(11).Text()
            txtHomeAddress.Text = lvData.Items(ItemNo).SubItems(12).Text()
            txtEmail.Text = lvData.Items(ItemNo).SubItems(13).Text()
            txtHomePhone.Text = lvData.Items(ItemNo).SubItems(14).Text()
            cmbBaptismStatus.Text = lvData.Items(ItemNo).SubItems(15).Text()
            txtChurchMembership.Text = lvData.Items(ItemNo).SubItems(16).Text()
            txtOtherNotes.Text = lvData.Items(ItemNo).SubItems(17).Text()
            txtPhoto.Text = lvData.Items(ItemNo).SubItems(18).Text()

            'Display Full name of Staff
            lblFullName.Text = txtGivenName.Text & " " & txtFamilyName.Text
            'Load the photo when calling this
            loadPhoto(lblExecutiveID.Text)

            'To stop the message from popping up.
        Else
            If MessageBox.Show("Please click Refresh and then select the data from the list above to edit", "Select Data", MessageBoxButtons.OK) = Windows.Forms.DialogResult.OK Then
            End If
            ClearTextBoxes()
        End If

    End Sub
Member Avatar for Unhnd_Exception

I don't quite understand enough to fix your problem.

I can tell you your problem. When the list view's selected index is changed that event is raised 2 times. It raises when the current item is unselected and then raises again with the newly selected items. So on every change you will have the event raised with no selected items and another with a selected item.

Thats the reason your getting the message box firing everytime.

see if you can posts some more and your problem will get solved.

Thank you very much for the reply, Unhan_Exception,

Yes, I got your reply. The above is the code behind the Listview select row. What I want is, a code that will display the items for the first row (Listview item 0) when I click on it to do editing. This part is working fine if this is the first click on the listview items. When the second/next listview item is clicked, it should replace the textboxes now occupied with the ListView items of the first one (Selected Index 0) and display the data for editing. However, this is not happening, what I'm getting is the else part of the code above. So pleas help to modify my code above. I desperately need this one resolved to get the application working.

Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.