【发布时间】:2025-12-07 18:20:05
【问题描述】:
我已参考此链接 (thread 1),但出现错误。对于我的示例,我在一个班级中只有一个列表。类名是Savestate。我有 2 个表格。
Form1 包含一个文本框,当我按下 button1 时,我保存在其中的字符串将被传输到列表中。 Button1 也会打开
Form2,其中有一个标签应该反映文本框中的字符串。但是,标签将反映 systems.collection...
下面是我的代码。
保存状态:类名
public static List<string> number = new List<string>();
Form1
private void button1_click(object sender, System.EventArgs e)
{
Savestate.number.Add(textbox1.Text);
Formscollection.Form1.Hide(); //Form 1 and Form 2 saved in another class called formscollection
Formscollection.Form2.Show();
}
Form2(显示 systems.collections..)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = Savestate. number.ToString();
}
我尝试了基于其他论坛的另一个代码,但收到了错误
Form2(出现错误:无法将类型 void 隐式转换为字符串)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text = Console.WriteLine(item)
}
}
希望得到帮助。谢谢。
【问题讨论】:
-
Savestate. number是一个列表,这就是为什么当你调用ToString()时它会显示Systems.Collections.GenericList。您需要获取列表中的元素以打印实际的字符串