I create my listview in my design part, not programmatically.

Then I want to reach this listview's content when I pressed some button.

In my main form, I created a public listview to return listview2 as in my design.

public ListView lst
    {
        get { return listView2; }
        set { listView2 = value; }
    }

Then in my other form, I created an object of main form, and tried to reach listview2 which is in main form.

Mainform main = new Mainform();

for(int i=0;i<main.lst.Items.Count;i++)

But I see that items.count is 0. I want to send my listview content to other form when I pressed button5 for example.

Any idea ?

Recommended Answers

All 10 Replies

You have to populate listview object into a constructor of MainForm class.

You are creating another instance of the main form , you must pass the instance of the main form to the 2nd forms constructor to make the reference work EX

MainForm : Form
{
     ...

     public ListView mylistview
{
  get{return listview1;}
 set{listview1 = value;}
}

void openform2(object sender, eventhandler e)
{
   Form2 f2 = new form2(this);
    f2.show();
}
}  

and in form2

form2:form
{
mainform f;

  public form2(mainform f)
{
     this.f = f;
}

void buttonclick(object sender, eventargs e)
{
     f.mylistview.add("item1");
}

}

I know this code is hideous and don't copy and paste it as I am sure its not correctly cased. i typed it here in the quick reply box. but it should show you what you need to know.

Firstly, what are you trying to achieve? If you are adding the items to the first Listbox at design time, why dont you just paste them into the second listbox as well?

If you really want to do it in code you can do the following. But please note this will only copy over items that you added to the listbox at design time. If you add/remove items at runtime then what diamonddrake said will apply. The changes made at runtime can only be accessed by passing a handle to your current form. The items added at design time are accessible on a new instance of the form.
If you need me to explain anything in more detail just let me know.

//add this property to the form you wish to retrieve the listbox items from
        //it is read only as you will be getting the lsit from a fresh instance of the form and changes would be lost
        public object[] lstItems
        {
            get
            {
                object[] values = new object[listBox1.Items.Count];
                listBox1.Items.CopyTo(values, 0);
                return values;
            }
        }
            
        //in the form you wish to add the list items to place this code in a button click event
        private void button1_Click(object sender, EventArgs e)
        {
            //create a new instance of your other form
            Form1 frm = new Form1();
            //then add the items to the listbox on this form via the readonly property
            listBox2.Items.AddRange(frm.lstItems);
        }

I am populating my listview from database and I want to take a Microsoft report of it when I clicked some button. As I saw from http://www.aliaydin.com.tr/?p=3 I tried to create Microsoft report. But I need to carry my form's listview content to my report form. I'm asking these question because of this.

As I couldn't solve in my previous post, I thought I need to be more clear.

I have a listview that was populated from database. I want to create a microsoft report that shows me what is inside the listview when I clicked some "take a report" button.

I created my listview in design part and gave column names by hand. And filled the listview by using LINQ

>I am populating my listview from database and I want to take a Microsoft report of it when I clicked some button..

You can use same database content for Microsoft report but what happened to listview control.

As I couldn't solve in my previous post, I thought I need to be more clear.

I have a listview that was populated from database. I want to create a microsoft report that shows me what is inside the listview when I clicked some "take a report" button.

I created my listview in design part and gave column names by hand. And filled the listview by using LINQ

If you are filling the listview from a database then the code i gave wont work.
Can you clarify something for me; do you need to transfer ALL the items in the list or just the ones that are currently selected?

I want to transfer all the items

In that case, unless you are adding/changing items at runtime, you can just call the same query you used to populate the first list.
Or, using what diamonddrake showed you, you can pass a reference for either Form1 (or the listbox on Form1) to Form2 from which to access the list.

I am having a similar issue. I have 2 forms. One in which is a search form that brings up one to many items, then an edit form which edits the selected index item.

There are 2 listviews. One reads and lists the search from the database and the other one is to take one selected result and view it in another listview on another form.

I was thinking about just passing the search query but if there is more than one result that would not help.

Thanks for any advice

Nick

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.