【发布时间】:2022-01-13 21:20:55
【问题描述】:
(文本文件以单独的形式创建)
这是我目前所拥有的:
foreach (RadioButton rb in this.Controls)
if (rb.Checked)
{
StreamWriter details;
details = File.AppendText("User Details.txt");
details.WriteLine("Preferences:" + rb.Text);
}
运行时显示:System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.RadioButton'。'
【问题讨论】:
-
我相信您需要将 Controls 集合过滤为您期望的类型。使用 System.Linq; foreach(Controls.OfType 中的 RadioButton
()) -
不要推出自己的偏好系统;已经有一个非常好的内置在 winforms 中