CommandButton controls provide a DialogResult property. When you open the form using the ShowDialog method, and someone clicks a button with its DialogResult property set, the ShowDialog method returns the value of the clicked button's DialogResult property as its return value. You can add a command button to the form for each DIalogResult value you might want to return.
Then, you can have it set up so that in the SelectedIndexChanged (or Click, more likely) event of the ListView, you call the PerformClick method of the appropriate commandbutton. This sends back the DialogResult value associated with the button, and the ShowDialog method call that opened the form returns the value. You don't have to worry about closing the form, it'll take care of itself.
If you go that route, the code in the parent might look like this:
Public SomeProperty As String
' Then, later, when you want to display the child form:
Dim frm As New YourChildForm()
Select Case frm.ShowDialog()
Case DialogResult.OK
' Do whatever you need to do if they selected OK.
Case DialogResult.Cancel
' Do whatever you need to do if they selected cancel.
' And so on.
End Select
' You won't get here until they close the form, so you can
' use the value in SomeProperty.
' Do something with SomeProperty.
' Don't forget to dispose of the form when you're done:
frm.Dispose()
If you don't want to use that mechanism, then I believe you've got all the info you need, from other posts. You can call the ShowDialog method of the form to display it. You've seen how to either make a property of the parent form (I assume we're still on the MDI app) visible from the child. You know how to set that property from the child form. In the Click event of the ListVIew control, you can set the value of the property you need to set, then call the Close method of the form. This closes the form. Then, in the parent form, you can do something with the property once the form is closed.
Something like this, in the parent:
Public SomeProperty As String
' Then, later, when you want to display the child form:
Dim frm As New YourChildForm()
frm.ShowDialog()
' The code stops here, and waits for you to close the child form.
' Once you get back, you can do something with SomeProperty,
' which was set by the child form.
' Don't forget to dispose of the form when you're done:
frm.Dispose()
In the child form, you'd have to add code that handled the Click event of the ListVIew, and then called the Close method of the form.
in you parent form, a method to display the dialog, and also declare a local/ or class level variable to hold the data return by the dialog
create a public property for your dialog, it coulb anything, even the listVieweItem object, and also put a button which the dialog result set to DialogResult.OK, and may be set it as the default button for the dialog
public ListViewItem SelectedItem
{
get {
if( mylistview.SelectedItems.Count == 1
return mylistview.SelectedItems[ 0 ];
return null;
}
}
may be you should replace it with your custome business object, i.e. what the dialog is supposed to do.
public Person SelectedPerson
{
get {
// assuming you dispaly person collecton in the listview
if( mylistview.SelectedItems.Count == 1
return person[ mylistview.SelectedItems[ 0 ]];
return null;
}
}
while in your parent
//
object retVal;
// show dialog
using{ MyDialog dlg = new MyDialog())
{
if( dlg.ShowDialog() == DialogResult.OK)
retVal = dlg.RetVal;
}
Anyway, this is what I ended up with in the child: Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Me.Close()
End SubAnd the parent Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNew.Click
Dim myNewFile As New NewFileDialog()
myNewFile.ShowDialog()
MessageBox.Show(myNewFile.ListView1.SelectedItems(0).Index)
myNewFile.Dispose()
End SubThis will popup a message box with the index of the selected item. It works now. Thanks for both of your help!!