谁能告诉我如何使用复选框构建字符串。最好的方法是什么。
例如,我有 4 个复选框,每个复选框都有自己的值(valueA、valueB、valueC、valueD),我想在不同的行中显示每个结果。
选择 B & C 的结果:
值
B 值C
如果我将它保存到数据库中,我将如何再次显示它?
谁能告诉我如何使用复选框构建字符串。最好的方法是什么。
例如,我有 4 个复选框,每个复选框都有自己的值(valueA、valueB、valueC、valueD),我想在不同的行中显示每个结果。
选择 B & C 的结果:
值
B 值C
如果我将它保存到数据库中,我将如何再次显示它?
使用 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——即使它不是一个没有足够细节的可怕问题任何机会得到一个体面的答案。