2013-08-27 26 views
1

我有這個自定義類的列表。如何跨Winforms訪問列表集合

public class Item 
{ 
    public string @Url; 
    public string Name; 
    public double Price; 

    public Item(string @url, string name, double price) 
    { 
     this.Url = url; 
     this.Name = name; 
     this.Price = price; 
    } 


    public void setPrice(Button button) 
    { 
     this.Price = Convert.ToDouble(button.Text); 
    } 
} 

現在我的主要的WinForm這個聲明

List<Item> items = new List<Item>(); 

的字段通過添加按鈕如下他們從3個文本框信息,並將其存儲在一個新的項目設置。

Regex url = new Regex(@"^[a-zA-Z0-9\-\.]+\.(com|org|net|ca|mil|edu|COM|ORG|NET|CA|MIL|EDU)$"); 
Regex name = new Regex(@"^[0-9, a-z, A-Z, \-]+$"); 
Regex price = new Regex(@"^[0-9, \.]+$"); 

if (url.IsMatch(urlText.Text) && name.IsMatch(nameText.Text) && price.IsMatch(priceText.Text)) 
{ 
    itemListBox.Items.Add(nameText.Text); 
    double item_Price = Convert.ToDouble(priceText.Text); 
    items.Add(new Item(@itemURL.Text, itemName.Text, item_Price)); 
    nameText.Clear(); 
    priceText.Clear(); 
    urlText.Clear(); 
} 
else 
{ 
    match(url, urlText, urlLabel); 
    match(price, priceText, priceLabel); 
    match(name, nameText, nameLabel); 
} 

正如您在上面的代碼中看到的那樣,它還會將項目的名稱添加到項目列表框中。現在我有另一個窗體,當點擊編輯按鈕時彈出窗口。我怎樣才能使編輯窗體中的項目列表框顯示完全像主窗體窗體中的項目列表框?

在一個基本的問題中,我如何將項目列表傳輸到編輯窗體。我試過把它傳遞給一個構造函數,因爲我希望無論winform如何,編輯後的信息都保持不變。構造函數被宣佈在編輯表單:

public Edit(List<Item> i) 
{ 
    itemList = i; 
    InitializeComponent(); 
} 

當我裝了列表框中

foreach (Item i in itemList) 
{ 
    itemListBox.Items.Add(i.Name); 
} 

列表框中顯示的,而不是名稱

更新1的實際值名稱:No work

更新2:

我的主winform co德http://pastebin.com/mENGKdnJ

編輯WinForm的代碼http://pastebin.com/tvp95jQW

不注意的打開文件對話框我不知道如何編寫它咫尺天涯編碼這個節目教了我很多,因爲我學習,因爲我走。

+1

好的,所以你嘗試了一些東西('ref'根本就不需要)。發生了什麼? – Jon

+0

在_itemListBox.Items.Add(i.Name)上放置斷點; _確認.Name包含「Name」。 –

+0

提示:如果你想成爲阻塞mainform直到它關閉的編輯表單,使用ShowDialog()而不是Show() –

回答

2

您不需要發送一個列表作爲參考,只有在更改(指針)實例本身時才需要參考。添加/刪除項目不會影響列表實例。

更新:

列表框使用ToString()呈現一些描述的項目。

public class Item 
{ 
    public string @Url; 
    public string Name; 
    public double Price; 

    public Item(string @url, string name, double price) 
    { 
     this.Url = url; 
     this.Name = name; 
     this.Price = price; 
    } 


    public void setPrice(Button button) 
    { 
     this.Price = Convert.ToDouble(button.Text); 
    } 

    public override string ToString() 
    { 
     // example: return string.Format("{0} -> {1}", this.Name, this.Price); 
     return this.Price; 
    } 

}

所以,你不應該添加的名稱,但對象

foreach (Item i in itemList) 
{ 
    itemListBox.Items.Add(i); 
} 

成癮(小提示)

public class Item 
{ 
    public string @Url {get; set;} 
    public string Name {get; set;} 
    public double Price {get; set;} 

    public Item(string @url, string name, double price) 
    { 
     this.Url = url; 
     this.Name = name; 
     this.Price = price; 
    } 
} 

的SetPrice(轉換加倍不應該放在這裏)

更新_ __ _ __ _ __ _ __ _ __ _ __

我想我看到它:

if (url.IsMatch(urlText.Text) && name.IsMatch(nameText.Text) && price.IsMatch(priceText.Text)) 
{ 
    itemListBox.Items.Add(nameText.Text); 
    double item_Price = Convert.ToDouble(priceText.Text); 
    items.Add(new Item(@itemURL.Text, itemName.Text, item_Price)); 
    nameText.Clear(); 
    priceText.Clear(); 
    urlText.Clear(); 
} 

你加nameText.Text到列表框,但是您將itemName .Text傳遞給該項目的構造函數。

同樣的,urlText。文本的正則表達式匹配,但itemURL。文本傳遞給構造函數。

+0

ref被刪除並更新問題 – MikaAK

+0

作出了所有的變化建議,但不幸的是我仍然堅持與同樣的問題檢查主要帖子的圖片顯示了。 – MikaAK

+0

您是否檢查清單上的內容?在itemListBox.Items.Add(i.Name)上放置一個斷點;確認i.Name包含「Name」,我認爲一個空列表傳遞給你編輯表單。並且名稱是默認的內容列表框 –