任何人都可以告诉我如何使用复选框构建字符串。什么是最好的方法。

例如我有4个复选框,每个复选框都有自己的值(valueA,valueB,valueC,valueD) 我希望将每个结果显示在不同的行中。

结果如果B&选择C:

valueB
valueC

如果我将其保存到数据库中,我将如何再次显示?

有帮助吗?

解决方案

使用StringBuilder构建字符串,并在每次追加时附加Environment.NewLine:

StringBuilder builder = new StringBuilder();
foreach (CheckBox cb in checkboxes)
{
    if (cb.Checked)
    {
        builder.AppendLine(cb.Text); // Or whatever

        // Alternatively:
        // builder.Append(cb.Text);
        // builder.Append(Environment.NewLine); // Or a different line ending
    }
}
// Call Trim if you want to remove the trailing newline
string result = builder.ToString();

要再次显示它,您必须将字符串拆分为行,并选中每个复选框以查看其值是否在集合中。

其他提示

伪代码:

For each checkbox in the target list of controls
    append value and a newline character to a temporary string variable
output temporary string 
"if I saved this into a database" ? 

如果你真的想在这里得到任何帮助,你需要对你的家庭作业更具体一点......

编辑好吧,可能不是作业,但它肯定是这样读的 - 毕竟,操纵GUI来生成用户选择的视图是Interfaces 101 - 即使不是一个可怕的问题,如果没有足够的细节,任何机会得到一个体面的答案。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top