多个复选框值插入并返回到c#windows应用程序中的复选框 [英] Multiple checkbox value insert and retrive back to checkbox in c# windows application
问题描述
嘿,请帮助我在我的Windows应用程序中发现这个问题。
im试图将数值插入数据库的工作但是在时间上它不再工作了。
我尝试过:
1)插入
string checkvalues =;
foreach(在..groupBox5.Controls中控制c)
{
if(c是CheckBox)
{
CheckBox b =(CheckBox)c;
if(b.Checked)
{
checkvalues = b.Text ++ checkvalues;
}
}
}
2)我已经删除了我的提取代码
Hey please help me to short out this .problem occured in my windows application,
i m trying to insert value into db its work but at fetching time its not work any more.
What I have tried:
1)To insert
string checkvalues = "";
foreach (Control c in this.groupBox5.Controls)
{
if (c is CheckBox)
{
CheckBox b = (CheckBox)c;
if (b.Checked)
{
checkvalues = b.Text + "" + checkvalues;
}
}
}
2)I have errased my code of fetching
推荐答案
首先使用唯一的分隔符,这样如果你有6个带有文本1的复选框,2 等等,您可以在恢复值时轻松分离它们。您正在寻找的是一个看起来像这样的结果字符串:
Start by using unique separator characters, so that if you have 6 checkboxes with the text "1", "2", and so forth, you can easily seperate them when you restore the values. What you are looking for is a result string that looks somethign like this:
string demo = "1=true;2=false;3=true;4=false;5=false;6=false";
然后你可以使用string.Split两次来打破你需要的值:
You can then use string.Split twice to break out the values you need:
string[] boxes = demo.ToLower().Split(';');
foreach (string box in boxes)
{
string parts = box.Split('=');
if (!parts.Length == 2) throw new ApplicationException("Bad checkbox data: " + box);
bool checked = parts[1] == "true";
switch(parts[0])
{
default: throw new ApplicationException("Unknown checkbox: " + box);
case "1": checkbox1.Check = checked; break;
case "2": checkbox2.Check = checked; break;
case "3": checkbox3.Check = checked; break;
case "4": checkbox4.Check = checked; break;
case "5": checkbox5.Check = checked; break;
case "6": checkbox6.Check = checked; break;
}
}
生成字符串是对现有代码的微不足道的更改:
To generate your string is trivial changes on your existing code:
string checkvalues = "";
string sep = "";
foreach (Control c in this.groupBox5.Controls)
{
if (c is CheckBox)
{
CheckBox b = (CheckBox)c;
checkvalues = string.Format("{0}{1}{2}={3}", checkvalues, sep, b.Text, b.Checked);
sep = ";";
}
}
这篇关于多个复选框值插入并返回到c#windows应用程序中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!