繁体   English   中英

如何获取要在现有文本文件中列出的单选按钮的名称/文本?

[英]How do I get the name/text of a radio button to be listed in an existing text file?

(文本文件以单独的形式创建)

这是我到目前为止所拥有的:

foreach (RadioButton rb in this.Controls)
            if (rb.Checked)
            {
                StreamWriter details;
                details = File.AppendText("User Details.txt");
                details.WriteLine("Preferences:" + rb.Text);
                
                 }

When ran, it says: System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.RadioButton'.'

您可以使用OfType<..>指定所需的控件类型。 这将返回转换为指定类型的已过滤控件集合。

foreach (RadioButton rb in this.Controls.OfType<RadioButton>())

在这里找到了类似的问题

您需要导入 Linq 才能使用此扩展方法。

using System.Linq;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM