在c#中检查和分配复选框值给字符串
本文关键字:字符串 复选框 分配 检查和 | 更新日期: 2023-09-27 18:18:28
我很累,很困惑,所以想看看是否有人知道更好的方法:
我有4个复选框,用户可以选择none, one, or all, or any combo。然后在后面的代码中,我想检查所有的复选框,如果它被选中,然后取它的值(文本)并将其分配给1个字符串,并在必要时用逗号分隔它。
这样做效果很好,但是无论项目是否被选中,都会分配所有的。text。
if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked || CheckBox4.Checked)
{
ClientString = CheckBox1.Text + ", " + CheckBox2.Text + ", " + CheckBox3.Text + ", " + CheckBox4.Text;
}
我盯着它看,我知道解决办法很简单,但我现在太累了,无法清晰地思考,想找出我做错了什么。代码为c# (ASP.NET)。
我如何轮询4复选框,如果它被选中,然后分配它的值给字符串,忽略它,如果它没有被选中?
谢谢。
这样怎么样?(未测试)
For .NET 3.5
var checkboxList = new List<CheckBox>{CheckBox1,CheckBox2,CheckBox3,CheckBox4};
var messages = checkboxList
.Where(x => x.Checked)
.Select(x => x.Text)
.ToArray();
ClientString = string.Join(", ", messages);
For .NET 4.0
var checkboxList = new List<CheckBox>{CheckBox1,CheckBox2,CheckBox3,CheckBox4};
var messages = checkboxList
.Where(x => x.Checked)
.Select(x => x.Text);
ClientString = string.Join(", ", messages);
有几个选项,最粗暴的方法是使用一系列if语句。
// this runs the risk of a hanging chad
// i.e. ", value, value"
ClientString = String.Empty;
if (CheckBox1.Checked)
ClientString = CheckBox1.Text;
if (CheckBox2.Checked)
ClientString += ", " + CheckBox2.Text;
if (CheckBox3.Checked)
ClientString += ", " + CheckBox3.Text;
if (CheckBox4.Checked)
ClientString += ", " + CheckBox4.Text;
也就是说,你最好使用CheckBoxList和lambda表达式。
ClientString = checkboxBoxList.Items.Cast<ListItem>()
.Where(i => i.Selected)
.Join(", ", i => i.Text);
对于。net 2.0,这样的东西可能是最好的:
List<string> cbTexts = new List<string>();
if (CheckBox1.Checked)
cbText.Add(CheckBox1.Text);
if (CheckBox2.Checked)
cbText.Add(CheckBox2.Text);
if (CheckBox3.Checked)
cbText.Add(CheckBox3.Text);
if (CheckBox4.Checked)
cbText.Add(CheckBox4.Text);
string ClientString = String.Empty;
foreach (string cbText in cbTexts)
{
ClientString += cbText ", ";
}
ClientString.Remove(ClientString.Length - 2); // remove trailing comma
你可以这样做
//clear the string
String ClientString = String.Empty;
if (CheckBox1.Checked )
ClientString = CheckBox1.Text
if (CheckBox2.Checked )
ClientString += ", " + CheckBox2.Text;
if (CheckBox3.Checked )
ClientString += ", " + CheckBox3.Text;
if (CheckBox4.Checked )
ClientString += ", " + CheckBox4.Text;
下面的代码是if any checkbox is checked then combine text value of all the checkboxes
if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked || CheckBox4.Checked)
{
ClientString = CheckBox1.Text + ", " + CheckBox2.Text + ", " + CheckBox3.Text + ", " + CheckBox4.Text;
}
好的,我认为你的问题是询问所有复选框的值,如果它们都被选中了,基于此,我说:你用OR
写这个,而我认为你想用AND
if (CheckBox1.Checked && CheckBox2.Checked && CheckBox3.Checked && CheckBox4.Checked)
{
ClientString = String.Format("{0}, {1}, {2}, {3}", CheckBox1.Text, CheckBox2.Text, CheckBox3.Text, CheckBox4.Text);
}
然而,现在我意识到你真正想要的只是那些被选中的复选框的值,你想知道它们是否都被选中了。您可以通过连接一个列表来实现:
List<String> values = new List<String>();
if (CheckBox1.Checked) result.Add(CheckBox1.Text);
if (CheckBox2.Checked) result.Add(CheckBox2.Text);
if (CheckBox3.Checked) result.Add(CheckBox3.Text);
if (CheckBox4.Checked) result.Add(CheckBox4.Text);
String result = String.Join(", ", values);
用逗号分隔列表:
TextList textlist = new List<string>();
if (CheckBox1.Checked) textlist.Add(CheckBox1.Text);
if (CheckBox2.Checked) textlist.Add(CheckBox2.Text);
if (CheckBox3.Checked) textlist.Add(CheckBox3.Text);
if (CheckBox4.Checked) textlist.Add(CheckBox4.Text);
ClientString clientString = string.Join(",", textlist);