Question

can anybody show me how to build a string using checkbox. what would be the best way to do this.

for example i have 4 checkbox's each with its own value (valueA, valueB, valueC, valueD) the thing is i want to display each result in different lines.

result if B & C is selected :

valueB
valueC

and how would i display this again if i saved this into a database?

Was it helpful?

Solution

Use a StringBuilder to build the string, and append Environment.NewLine each time you append:

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();

To display it again, you'd have to split the string into lines, and check each checkbox to see whether its value is in the collection.

OTHER TIPS

Pseudo-code:

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" ? 

You'll need to be a bit more specific with your homework assignments if you're actually going to receive any help here ...

Edit: ok, it might not be homework but it certainly read like it - after all, manipulating a GUI to generate a view of the user's choices is Interfaces 101 - and even it wasn't it was a terrible question without enough detail to have any chance of getting a decent answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top