2010-10-13 104 views
0

我有一個包含10個複選框的組框。我想建立一個字符串連接所有選中的複選框的Checkbox.Text。基於checkbox.checked填充文本框衆多複選框的值

當然,任何時候任何複選框的選中狀態都會改變,它將需要重建字符串。我怎麼能這樣做呢?

注意:這需要在飛行中發生,因爲複選框被選中/取消選中。

這是我的想法,但我覺得有一個更好的方式來做到這一點 - 而且我不知道當一個項目未被選中時我將如何去除字符串。

有什麼想法?

private void CheckBox_CheckedChanged(System.Object sender, System.EventArgs e) 
{ 
    if (((CheckBox)sender).Checked) 
    { 
     switch (((CheckBox)sender).Name) 
     { 
      case "CheckBox1": 
       sb = sb + "This is checkbox 1." + "\n"; 
       break; 
      case "CheckBox2": 
       sb = sb + "This is checkbox 2." + "\n"; 
       break; 
      case "CheckBox3": 
       sb = sb + "This is checkbox 3." + "\n"; 
       break; 
      case "CheckBox4": 
       sb = sb + "This is checkbox 4." + "\n"; 
       break; 
      case "CheckBox5": 
       sb = sb + "This is checkbox 5." + "\n"; 
       break; 
      case "CheckBox6": 
       sb = sb + "This is checkbox 6." + "\n"; 
       break; 
      case "CheckBox7": 
       sb = sb + "This is checkbox 7." + "\n"; 
       break; 
      case "CheckBox8": 
       sb = sb + "This is checkbox 8." + "\n"; 
       break; 
      case "CheckBox9": 
       sb = sb + "This is checkbox 9." + "\n"; 
       break; 
      case "CheckBox10": 
       sb = sb + "This is checkbox 10." + "\n"; 
       break; 
     } 
    } 
    else 
    { 
    } 
} 

回答

1

你可以做這樣的事情,假設該複選框名稱格式「複選框#」:

private void checkBox_CheckedChanged(object sender, EventArgs e) 
{ 
    var checkboxes = from c in groupBox1.Controls.OfType<CheckBox>() 
         where c.Checked 
         orderby int.Parse(c.Name.Substring(8)) 
         select c; 

    sb.Clear(); 

    foreach (var cb in checkboxes) 
    { 
     sb.Append(string.Format("This is checkbox {0}", cb.Name.Substring(8))); 
     sb.Append(Environment.NewLine); 
    } 

    textBox1.Text = sb.ToString(); 
} 

如果你不想擔心複選框名字的格式,你可以使用AlphanumComparator,但你必須做出源一些細微的變化,使其通用:

AlphanumComparator<string> comparator = new AlphanumComparator<string>(); 

var checkboxes = (from c in groupBox1.Controls.OfType<CheckBox>() 
        where c.Checked 
        select c).OrderBy(c => c.Name, comparator); 
+0

作品,更容易跟蹤,+我剛開始學習LINQ的,所以獎金居然剛開克去應用它。謝謝! – user234702 2010-10-13 17:51:01

0

下面的代碼工作:但是,我仍然有興趣聽到是否有更好的方法來做到這一點?

private void CheckBox_CheckedChanged(System.Object sender, System.EventArgs e) 
     { 
      if (((CheckBox)sender).Checked) 
      { 
       switch (((CheckBox)sender).Name) 
       { 
        case "CheckBox1": 
         sb.Append("This is checkbox 1.\r\n"); 
         break; 
        case "CheckBox2": 
         sb.Append("This is checkbox 2.\r\n"); 
         break; 
        case "CheckBox3": 
         sb.Append("This is checkbox 3.\r\n"); 
         break; 
        case "CheckBox4": 
         sb.Append("This is checkbox 4.\r\n"); 
         break; 
        case "CheckBox5": 
         sb.Append("This is checkbox 5.\r\n"); 
         break; 
        case "CheckBox6": 
         sb.Append("This is checkbox 6.\r\n"); 
         break; 
        case "CheckBox7": 
         sb.Append("This is checkbox 7.\r\n"); 
         break; 
        case "CheckBox8": 
         sb.Append("This is checkbox 8.\r\n"); 
         break; 
        case "CheckBox9": 
         sb.Append("This is checkbox 9.\r\n"); 
         break; 
        case "CheckBox10": 
         sb.Append("This is checkbox 10.\r\n"); 
         break; 
       } 

       txtBox.Text = sb.ToString(); 
      } 
      else 
      { 
       switch (((CheckBox)sender).Name) 
       { 
        case "CheckBox1": 
         sb.Replace("This is checkbox 1.\r\n", ""); 
         break; 
        case "CheckBox2": 
         sb.Replace("This is checkbox 2.\r\n", ""); 
         break; 
        case "CheckBox3": 
         sb.Replace("This is checkbox 3.\r\n", ""); 
         break; 
        case "CheckBox4": 
         sb.Replace("This is checkbox 4.\r\n", ""); 
         break; 
        case "CheckBox5": 
         sb.Replace("This is checkbox 5.\r\n", ""); 
         break; 
        case "CheckBox6": 
         sb.Replace("This is checkbox 6.\r\n", ""); 
         break; 
        case "CheckBox7": 
         sb.Replace("This is checkbox 7.\r\n", ""); 
         break; 
        case "CheckBox8": 
         sb.Replace("This is checkbox 8.\r\n", ""); 
         break; 
        case "CheckBox9": 
         sb.Replace("This is checkbox 9.\r\n", ""); 
         break; 
        case "CheckBox10": 
         sb.Replace("This is checkbox 10.\r\n", ""); 
         break; 
       } 

       txtBox.Text = sb.ToString(); 
      } 
     }