Question Second form revisited

mstolley

New member
Joined
Apr 29, 2016
Messages
4
Programming Experience
5-10
I have a program that has two forms. The first form is just a listview and a series of buttons making a keyboard.
The second form displays the selected item details from the first form.
The relevant code for the first form is :
Public Class frmMain
   Public PersonID As String


    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
.
.
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        PersonID = ListView1.SelectedItems.Item(0).SubItems(0).Text
        MsgBox("PersonID = " & PersonID)
        Me.Hide()
        frmPersonData.ShowDialog()
End Sub

and for the second form is :

Private Sub frmPersonData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myDataSet As New DataSet()
        Dim constring As String
        Dim conn As SqlConnection
        Dim PersonID1 As String
        PersonID1 = frmMain.PersonID
        MsgBox("PersonID1 = " & PersonID1)
.
.
End Sub

Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
        Dim frm As New frmMain
        Me.Close()
        frmMain.Close() ' Closes the Form
        'THEN! Reload it.
        frm = New frmMain
        frm.Show() 'Shows a new,reloaded "frmMain"


    End Sub

My problem is that the code works well the first time round, shown by the msgboxes added to see what's being transferred.
But when another personID is chosen the PersonID in the frmMain shows the correct value but fails to transfer and the message
box in the frmPersonData sub shows PersonID =
Any ideas why this is happening.
Thanks.
 
This is almost certainly caused by a common issue with the ListView and one that has been written about often in many places, so I can't help thinking that you didn't look to hard to find a solution. Anyway, when an item is selected in a ListView and then you select another item, the SelectedIndexChanged event will be raised twice. The first event is for the deselection of the previous item and the second is for the selection of the next item. That means that you should ALWAYS first test that an item is selected before trying to use the selected item. If there is no item selected then just ignore the event and you'll get what you want on the next one.
 
Thanks for the reply, but as I said the PersonID value from the Listview is correct immediately before calling the 2nd form. It's when it gets to the 2nd form that it disappears.
i.e. for example
1st name selection gives PersonID = 1 in form fmrMain and PersonID = 1 in fmrPersonID
then the btnReturn_Click sub, then
2nd name selection gives PersonID = 8 in form fmrMain and PersonID = in frmPersonID.

I think the Listview events are cleared by closing then opening the main form frmMain in btnReturn_Click.

I hope you will forgive any apparent stupidity, but I'm just a septuagenarian trying to keep his mind active.
 
Ok - sorted it. I put the variable PersonID into a module and declared it Public.
Then referenced it as Module1.PersonID.
 
Ok - sorted it. I put the variable PersonID into a module and declared it Public.
Then referenced it as Module1.PersonID.

The more correct solution would have been to add a public property to the second form and then set that from the first form before displaying the second form. The second form should have to know where to pull that data from, it should be pushed in from outside.
 
Back
Top