Tuesday, 27 August 2013

How to access a List collection across winforms

How to access a List collection across winforms

I have a list of this custom class.
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);
}
}
Now in my main winform this is declared
List<Item> items = new List<Item>();
the fields are set through an add button as following they take
information from 3 text boxes and store it in a new item.
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);
}
As you can see in the above code it also adds the name of the item to an
item list box. Now I have another windows form that pop's up when the edit
button is clicked. How can i make the item list box in the edit form show
exactly like the item list box from the main windows form?
In a basic question how can i transfer the list of items to the edit form.
I've tried passing it through a constructor because I want the edited
information to remain constant no matter what winform. the constructor was
declared in the Edit Form:
public Edit(ref List<Item> i)
{
itemList = i;
InitializeComponent();
}

No comments:

Post a Comment