チェックされている項目を取得する場合は
CheckedItemsコレクションが便利です。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 項目を追加(チェック状態を指定しなければデフォルトでOFF) checkedListBox1.Items.Add("りんご"); // 項目を追加(チェックON) checkedListBox1.Items.Add("みかん", true); // 項目を追加(チェックOFF) checkedListBox1.Items.Add("もも", false); } private void button1_Click(object sender, EventArgs e) { // チェックされている項目を列挙 string msg = string.Empty; foreach (string item in checkedListBox1.CheckedItems) msg += item + "\r\n"; MessageBox.Show(msg, "選択された項目一覧"); } } }
コメントを残す