我特意将项目添加到另一个表单的listview中 它是form1中的方法(列表视图中的一个)
private void AfterAdd_DoWork(object sender, DoWorkEventArgs e)
{
int[] sizes = SellBL.Product.GetProdStockByID(prodid);
ListViewItem Item = new ListViewItem(prodid.ToString());
Item.SubItems.Add(prodname);
Item.SubItems.Add(ebayid);
Item.SubItems.Add("$" + sellprice);
if (!isnosize)
{
for (int i = 0; i < sizes.Length; ++i)
{
if (sizes[i] == 1)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("In Stock");
subItem.ForeColor = Color.Green;
}
else if (sizes[i] == 0)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Out Of Stock");
subItem.ForeColor = Color.Red;
}
else if (sizes[i] == 2)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Less Than 3");
subItem.ForeColor = Color.Orange;
}
else if (sizes[i] == 5)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Less Than 5");
subItem.ForeColor = Color.Orange;
}
else if (sizes[i] == 10)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Less Than 10");
subItem.ForeColor = Color.Orange;
}
}
}
else
{
if (sizes[0] == 1)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("In Stock");
subItem.ForeColor = Color.Green;
}
else if (sizes[0] == 0)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Out Of Stock");
subItem.ForeColor = Color.Red;
}
else if (sizes[0] == 10)
{
Item.UseItemStyleForSubItems = false;
var subItem = Item.SubItems.Add("Few Left");
subItem.ForeColor = Color.Orange;
}
}
ProductListView.Invoke(new MethodInvoker(delegate () { ProductListView.Items.AddRange(new ListViewItem[] { Item }); }));
}
&#13;
并且它是form2(从那里我不会添加产品)
private void backgroundWorkerAddProd_DoWork(object sender, DoWorkEventArgs e)
{
string a = Addprodnametxt.Text.ToString();
string b = Addprodebayidtxt.Text.ToString();
string c = addprodsellpricetxt.Text.ToString();
f1.AfterProductAdded(1, a, b, c, false);
}
&#13;
我尝试了这个方法:ProductListView.items.add(item); 但它不起作用 当我尝试这个方法时:ProductListView.Invoke(new MethodInvoker(delegate(){ProductListView.Items.AddRange(new ListViewItem [] {Item});})); 我收到了这个错误: System.InvalidOperationException:&#39;在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。&#39;
我该如何解决?
感谢
答案 0 :(得分:0)
试试这个
if (System.Windows.Forms.Application.OpenForms["yourForm"] != null)
{
(System.Windows.Forms.Application.OpenForms["yourForm"] as Form1).AfterAdd_DoWork();
}