Dies ist, wie ich den Artikel der List-Box bin und füge hinzu:Wie kann ich Objekt-Array von Elementen zu ListView hinzufügen, genauso wie ich sie zu ListBox hinzufüge?
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
Und das ist, wie ich versuche, sie zu dem Listview hinzuzufügen:
ListViewCostumControl.lvnf.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
Dies ist der Code Listview-Steuerelement :
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)
{
}
}
}
die Fehler, die ich bekommen habe, wenn die Elemente in der Listenansicht hinzuzufügen versuchen, sind:
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'
Dies ist die GetAllWindows Methode:
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);
}
Und das ist die WindowSnapCollection:
public WindowSnapCollection(WindowSnap[] items, bool asReadOnly)
{
base.AddRange(items);
base.TrimExcess();
this.asReadonly = asReadOnly;
}
WindowSnap ist der Name der Klasse mit dem GetAllWindow Methode os.
Ist dieses Beispiel, das ich jetzt gemacht habe, eine gute Lösung? Ich habe es zu dem Listview-Steuercode an der Spitze:
Object myObj = new Object();
ListViewItem[] items = new ListViewItem[8];
Dann im Konstruktor:
items[0].Text = myObj.ToString();
items[0].Tag = myObj;
lvnf.Items.AddRange(items);
Benötige ich den Tag? Wie verwende ich es anstatt eine Instanz von 8 Elementen zu erstellen?
Try ToList statt ToArray – Viru
Es gibt keine eine Eigenschaft ToList nur ToArray und ToString –