末增加我想從Form3
,Form4
,Form5
,Form6
,Form7
和Form8
創建窗體2與數據的列表_Buffer
。我做了工作,但只有1點的形式,如果我嘗試從Form4
例如創建另一個列表中添加其他元素,而我已經從Form3
添加...在Form2
會告訴我只能從Form4
元素,而不從Form3
我添加的元素先前。下面是我如何做到這一點:通過列表形式之間,並在它
代碼Form2
:
ListArticle _Buffer = new ListArticle();
public void SetData(ListArticle article)
{
_Buffer = article;
}
代碼Form3
:
public ListArticle _articles = new ListArticle();
public ListArticle Articles
{
get
{
return _articles;
}
set
{
_articles = value;
}
}
foreach (Color color in dominantColours)
{
MessageBox.Show(closestColor2(clist, color));
tshirt_number++;
_articles.Clothes.Add("T-shirt " + tshirt_number.ToString());
_articles.Colors.Add(closestColor2(clist, color));
Console.WriteLine("K: {0} (#{1:x2}{2:x2}{3:x2})", color, color.R, color.G, color.B);
string hex = color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
}
注:closestColor2
返回string
;
,這裏是我如何將它們添加到列表中Form2
:
Form2 frm = new Form2();
frm.Show();
Articles = _articles;
frm.SetData(Articles);
this.Hide();
Form4
代碼非常相似,從Form3代碼..只是另一個列表。
這裏是ListArticle
類:
public class ListArticle
{
public List<string> Clothes { get; private set; }
public List<string> Colors { get; private set; }
public ListArticle()
{
Clothes = new List<string>();
Colors = new List<string>();
}
}
所以基本上我想添加的元素我Form4
在我Form3
添加元素的末尾添加。
在'Form3,Form4,Form5 ...'你創建'Form2'的新實例?如果這樣做是錯誤的。您需要有一個Form2實例,並且所有其他表單必須訪問Form2s文章列表。 – Reniuz
@Reniuz我做這樣的事情:'Form2 frm = new Form2(); frm.Show();'。我如何創建一個Form2實例? –