Construa uma string usando a caixa de seleção
-
04-07-2019 - |
Pergunta
Alguém pode me mostrar como construir uma string usando a caixa de seleção. Qual seria a melhor maneira de fazer isso.
Por exemplo, eu tenho 4 caixas de seleção, cada uma com seu próprio valor (valuea, valueb, valuec, valorizado). O problema é que eu quero exibir cada resultado em linhas diferentes.
Resultado Se B&C for selecionado:
valueb
Valuec
E como eu exibiria isso novamente se salvasse isso em um banco de dados?
Solução
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.
Outras dicas
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.