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();
        }

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

Recommended Answers

All 7 Replies

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 line
myform.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.

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

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.

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.