954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with listview

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

public void CopySelectedItems(ListView _lstviewSource)

        {
            foreach (ListViewItem item in _lstviewSource.Items)
            {
                if (item.Checked)
                {
                  lstviewTarget.Items.Add((ListViewItem)item.Clone());
                }
            }
        }

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


method on form_source

private void btnAddImages_Click(object sender, EventArgs e)

        {
            form_target myform = new form_target();
            myform.CopySelectedItems(lstviewTarget);
            this.Close();
        }

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

rico078
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Use item.Selected in place of item.Checked on "form_target"

public void CopySelectedItems(ListView _lstviewSource)

        {
            foreach (ListViewItem item in _lstviewSource.Items)
            {
                if (item.Selected )
                {
                  lstviewTarget.Items.Add((ListViewItem)item.Clone());
                }
            }
        }


And on source_form i think you miss a linemyform.show();

private void btnAddImages_Click(object sender, EventArgs e)

        {
            form_target myform = new form_target();
            myform.Show();
            myform.CopySelectedItems(lstviewTarget);
            this.Close();
        }

It will work
Best Of Luck.

prvnkmr449
Junior Poster
106 posts since Sep 2010
Reputation Points: 3
Solved Threads: 16
 

thanks for the reply , i have tried what you have suggested and it still does not work

rico078
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
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
 

sorry , it is a typo , the code is "myform.CopySelectedItems(lstviewSource);"

rico078
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
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
 

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.

prvnkmr449
Junior Poster
106 posts since Sep 2010
Reputation Points: 3
Solved Threads: 16
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: