2017-06-06 96 views
-2

我需要發送listView.SelectedindexChanged事件的一些變量到外部對象,就像我可以做一個listBox。以下是我的listView代碼。在此之後,我將上傳外部對象代碼和工作列表框代碼。c#linq listView - selecteditem to object

private void listViewProducts_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     ProductList_Variables selected = (ProductList_Variables)listViewProducts.SelectedItems[0]; 

     textBoxProduct.Text = selected.Product; 
     comboBoxCategory.SelectedItem = selected.Category; 
     textBoxSize.Text = selected.Size.ToString(); 
     comboBoxMarket.SelectedItem = selected.Market; 
     comboBoxContainer.SelectedItem = comboBoxContainer.Items.OfType<ProductList_Variables>().SingleOrDefault(x => x.Container == selected.Container); 
     textBoxPrice1.Text = selected.Price.ToString(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

class ProductList_Variables 
{ 
    public int Id { get; set; } 
    public string Product { get; set; } 
    public string Category { get; set; } 
    public string Size { get; set; } 
    public string Market { get; set; } 
    public string ProductName { get { return Product + " - " + Category + " - Size: " + Size +", Market: "+ Market; } } 
    public string Flavour { get; set; } 
    public decimal Price { get; set; } 
    public string Container { get; set; } 
    public int IdContainer { get; set; } 
} 

    private void listBoxProducts_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      ProductList_Variables selected = (ProductList_Variables)listBoxProducts.SelectedItem; 

      //textBoxProduct.Text = selected.Product; 
      //comboBoxCategory.SelectedItem = selected.Category; 
      //comboBoxMarket.SelectedItem = selected.Market; 
      //comboBoxContainer.SelectedValue = selected.IdContainer; 
      //textBoxPrice1.Text = selected.Price.ToString(); 

      textBoxProduct.Text = selected.Product; 
      comboBoxCategory.SelectedItem = selected.Category; 
      textBoxSize.Text = selected.Size.ToString(); 
      comboBoxMarket.SelectedItem = selected.Market; 
      comboBoxContainer.SelectedItem = comboBoxContainer.Items.OfType<ProductList_Variables>().SingleOrDefault(x => x.Container == selected.Container); 
      textBoxPrice1.Text = selected.Price.ToString(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
+0

問題是什麼? – itsme86

回答

0

如果你想發送的額外變量的SelectedIndexChanged事件,你可能要考慮這些要求寫自己的事件。

您可以通過this user找到一個很好的解釋。