I have 2 windows forms (form_source and form_target) with listviews
(lstviewSource and lstviewTarget) respectively. When the items from
the "listviewSource" is selected it needs to be displayed on
"lstviewTarget".I am cloning the items from the listview in
"form_source" but it is not displaying on the listview on "form_target".
You said that form_source has a listview called lstviewSource,
yet in your code for form_source you reference lstviewTarget. myform.CopySelectedItems(lstviewTarget);
Is this where your problem is or is this just a typo.
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
private void btnAddImages_Click(object sender, EventArgs e) {
form_target myform = new form_target();
myform.Show();
myform.CopySelectedItems(lstviewTarget);
this.Close();
}
It will work
It will work if your goal is to briefly show the form then close it.myform is a local variable and thus will go out of scope as soon as this method ends. This will cause myform to immediately close.
You need to rethink your form handling.
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
It will work if your goal is to briefly show the form then close it. myform is a local variable and thus will go out of scope as soon as this method ends. This will cause myform to immediately close.
You need to rethink your form handling.
You would think that would be the case...and i believe it should be... but unless this.close is closing the main application form, the newly opened form will remain open.
When you call myform.Show() it is added to the Application.OpenForms collection. Thus when the method level variable that references the form goes out of scope it doesnt close because there is still a valid reference to it in the application. It will only be disposed and garbage collected when no references to it remain.
I still prefer not to call forms this way as it feels untidy to me to have forms floating around with no "anchoring" reference, but thats my personal feelings on the matter. I always use this.Hide when i want to move from one form to the next as this allows me the option to return and works if the form to be "hidden" is the application entry point.
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246