簡體   English   中英

檢查並將復選框值分配給C#中的字符串

[英]Checking and assigning Checkbox values to a string in C#

我很累,很難過,所以想知道是否有人知道更好的方法:

我有4個復選框,用戶可以選擇無,一個或全部,或任何組合。 然后在后面的代碼中,我想檢查所有CheckBoxes,如果它已被選中,則取其值(文本)並將其分配給1個字符串,並根據需要用逗號分隔。

這很好用但是分配所有.Text是否已經檢查了項目。

 if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked || CheckBox4.Checked)
    {
      ClientString = CheckBox1.Text + ", " + CheckBox2.Text + ", " + CheckBox3.Text + ", " + CheckBox4.Text; 
    }

我正盯着它,我知道解決方案非常簡單,但我現在太累了,無法想清楚,想知道我做錯了什么。 代碼是C#(ASP.NET)。

如何輪詢4個CheckBox並檢查它是否已將其值分配給字符串,如果未選中則忽略它?

謝謝。

這樣的事情怎么樣? (未測試)

對於.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);

對於.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

好吧,我把你的問題看作是要求所有復選框的價值,如果它們全部被選中並基於此我說:當我認為你打算使用AND時你用OR寫了這個

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

你可以這樣做

//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; 
    }

以逗號分隔列表:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM