0

This is how I'm adding the item to the listBox:

this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

And this is how I'm trying to add them to the listView:

ListViewCostumControl.lvnf.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

This is the listView control code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinimizeCapture
{
    public partial class ListViewCostumControl : UserControl
    {
        public static ListViewControl lvnf;

        public ListViewCostumControl()
        {
            InitializeComponent();

            lvnf = new ListViewControl();
            lvnf.Location = new Point(50, 50);
            lvnf.Size = new Size(50, 50);
            lvnf.View = View.SmallIcon;
            lvnf.Dock = DockStyle.Fill;
            lvnf.SuspendLayout();
            lvnf.LabelEdit = true;
            lvnf.Sorting = SortOrder.None;
            this.Controls.Add(lvnf);
            lvnf.ResumeLayout(false);
        }

        public class ListViewControl : System.Windows.Forms.ListView
        {
            public ListViewControl()
            {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.EnableNotifyMessage, true);
            }

            protected override void OnNotifyMessage(System.Windows.Forms.Message m)
            {
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }
            }
        }

        private void ListViewNFTest_Load(object sender, EventArgs e)
        {

        }
    }
}

The errors I'm getting when trying to add the items to the listView are:

Error   14  The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.AddRange(System.Windows.Forms.ListView.ListViewItemCollection)' has some invalid arguments

Error   15  Argument 1: cannot convert from 'MinimizeCapture.WindowSnap[]' to 'System.Windows.Forms.ListView.ListViewItemCollection'

This is the GetAllWindows method:

public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
        {
            windowSnaps = new WindowSnapCollection();
            countMinimizedWindows = minimized;//set minimized flag capture
            useSpecialCapturing = specialCapturring;//set specialcapturing flag
            EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
            EnumWindows(callback, IntPtr.Zero);
            return new WindowSnapCollection(windowSnaps.ToArray(), true);
        }

And this is the WindowSnapCollection:

public WindowSnapCollection(WindowSnap[] items, bool asReadOnly)
        {
            base.AddRange(items);
            base.TrimExcess();
            this.asReadonly = asReadOnly;
        }

WindowSnap is the name os the class with the GetAllWindow method.

Is this example I did now is a good solution? I added it to the listView control code at the top:

Object myObj = new Object();
ListViewItem[] items = new ListViewItem[8];

Then in constructor:

items[0].Text = myObj.ToString();
items[0].Tag = myObj;
lvnf.Items.AddRange(items);

Do I need the tag? How do I use it instead of making an instance of 8 items?

2
  • Try ToList instead of ToArray Commented May 3, 2016 at 8:04
  • There is no a property ToList only ToArray and ToString Commented May 3, 2016 at 8:10

1 Answer 1

3

The problem is what you are trying to add, that is what

WindowSnap.GetAllWindows(true, true).ToArray()

is returning as an argument.

The listbox AddRange parameter accepts two different arguments;

AddRange(Object[])
AddRange(ListBox.ObjectCollection)

So the first accepts any array of objects, and this is what you are using.

ListView however is not as flexible. It accepts;

AddRange(ListViewItem[])
AddRange(ListView.ListViewItemCollection)

Therefore, you cannot send an array of objects as you can with listbox. You have to be more specific in the type of data you pass to it.
I would suggest that you create some kind of wrapper around it to change the format of the data you want into the expected input format.

Sign up to request clarification or add additional context in comments.

2 Comments

probably write an extension method ToListViewItem which converts array of object to Array of ListViewItem
Can't figure out yet how to do it. Could someone please how how to do it ? Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.