942,967 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 120
  • C# RSS
Sep 8th, 2010
0

problem with listview

Expand Post »
Good Day ,
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".

below is the code.... thanks in advance.

*****************************************************

method on form_target



C# Syntax (Toggle Plain Text)
  1. public void CopySelectedItems(ListView _lstviewSource)
  2.  
  3. {
  4. foreach (ListViewItem item in _lstviewSource.Items)
  5. {
  6. if (item.Checked)
  7. {
  8. lstviewTarget.Items.Add((ListViewItem)item.Clone());
  9. }
  10. }
  11. }
******************************************************


method on form_source

C# Syntax (Toggle Plain Text)
  1. private void btnAddImages_Click(object sender, EventArgs e)
  2.  
  3. {
  4. form_target myform = new form_target();
  5. myform.CopySelectedItems(lstviewTarget);
  6. this.Close();
  7. }
******************************************************
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rico078 is offline Offline
5 posts
since Aug 2010
Sep 8th, 2010
0
Re: problem with listview
Use item.Selected in place of item.Checked on "form_target"
C# Syntax (Toggle Plain Text)
  1. public void CopySelectedItems(ListView _lstviewSource)
  2.  
  3. {
  4. foreach (ListViewItem item in _lstviewSource.Items)
  5. {
  6. if (item.Selected )
  7. {
  8. lstviewTarget.Items.Add((ListViewItem)item.Clone());
  9. }
  10. }
  11. }

And on source_form i think you miss a line
myform.show();
C# Syntax (Toggle Plain Text)
  1.  
  2. private void btnAddImages_Click(object sender, EventArgs e)
  3.  
  4. {
  5. form_target myform = new form_target();
  6. myform.Show();
  7. myform.CopySelectedItems(lstviewTarget);
  8. this.Close();
  9. }
It will work
Best Of Luck.
Reputation Points: 3
Solved Threads: 16
Junior Poster
prvnkmr449 is offline Offline
106 posts
since Sep 2010
Sep 9th, 2010
0
Re: problem with listview
thanks for the reply , i have tried what you have suggested and it still does not work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rico078 is offline Offline
5 posts
since Aug 2010
Sep 9th, 2010
0
Re: problem with listview
Quote originally posted by rico078 ...
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.
Reputation Points: 375
Solved Threads: 186
Veteran Poster
nick.crane is offline Offline
1,194 posts
since Feb 2010
Sep 9th, 2010
0
Re: problem with listview
sorry , it is a typo , the code is "myform.CopySelectedItems(lstviewSource);"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rico078 is offline Offline
5 posts
since Aug 2010
Sep 9th, 2010
0
Re: problem with listview
Click to Expand / Collapse  Quote originally posted by prvnkmr449 ...
C# Syntax (Toggle Plain Text)
  1. private void btnAddImages_Click(object sender, EventArgs e) {
  2. form_target myform = new form_target();
  3. myform.Show();
  4. myform.CopySelectedItems(lstviewTarget);
  5. this.Close();
  6. }
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.
Featured Poster
Reputation Points: 1143
Solved Threads: 514
Posting Sensei
Momerath is online now Online
3,142 posts
since Aug 2010
Sep 9th, 2010
0
Re: problem with listview
I think your program is close and not give any output use this.hide() in place of this.close()
region is that when ever when ever program encounter this.close() it close hole program rather then closing a single may it because of it is your main form.............
Best Of Luck.
Last edited by prvnkmr449; Sep 9th, 2010 at 4:41 pm.
Reputation Points: 3
Solved Threads: 16
Junior Poster
prvnkmr449 is offline Offline
106 posts
since Sep 2010
Sep 10th, 2010
0
Re: problem with listview
Click to Expand / Collapse  Quote originally posted by Momerath ...
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.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Making setup
Next Thread in C# Forum Timeline: Code for start and stop time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC