如何在C#中将字符串值列表从一个表单传递到另一个表单?
问题描述:
I have list of string values pass form one to another form the same list of string values.
答
请参阅此链接 http://stackoverflow.com/questions/12768674/send-data-from-one-page-to-another-in-c-sharp -asp-net [ ^ ]
参考这些链接。
在Windows窗体之间传递数据 [ ^ ]
http://stackoverflow.com/questions/7886544/从一种形式传递到另一种形式的价值 [ ^ ]
Refer these links.
Passing Data between Windows Forms[^]
http://stackoverflow.com/questions/7886544/passing-a-value-from-one-form-to-another-form[^]
根据您的场景,我创建了一个示例,让您了解我们如何将字符串集合从一个表单传递到另一个表单: -
让我说有两种形式如下: -
ContainForm:保存需要发送的数据。
GetForm:从ContainForm获取数据的表格。
现在我们可以在GettingForm中拥有属性来设置stringcollection,然后再从第一个表单打开表单。
GettingForm.cs
--------------
As per your scenario i have created an example to get you understand how we can pass string collection from one form to another :-
Let say i have two forms as below :-
ContainForm : Holds data which needs to be send.
GettingForm : Form which gets data from ContainForm.
Now we can have property in GettingForm to set the stringcollection before opening the form from first form.
GettingForm.cs
--------------
public partial class GettingForm : Form
{
public StringCollection DummyList { get; set; }
public GettingForm()
{
InitializeComponent();
}
private void GettingForm_Load(object sender, System.EventArgs e)
{
foreach (string str in this.DummyList)
{
MessageBox.Show(str);
}
}
}
ContainForm.cs
- --------------
ContainForm.cs
---------------
public partial class ContainForm : Form
{
public StringCollection DummyList { get; set; }
public ContainForm()
{
InitializeComponent();
}
private void ContainForm_Load(object sender, System.EventArgs e)
{
DummyList = new StringCollection();
DummyList.Add("Item 1");
DummyList.Add("Item 2");
DummyList.Add("Item 3");
DummyList.Add("Item 4");
}
private void button1_Click(object sender, System.EventArgs e)
{
GettingForm gtForm = new GettingForm(); // create instance of GettingForm
gtForm.DummyList = this.DummyList; // Assign value to the string collection propery of GettingForm
gtForm.Show(); // Display the form
}
}
希望这对你有所帮助。
那里您也可以获得更多替代方案此处 [ ^ ]。