header
CodePanic! > C#.NET Tips > 今ここ

■項目を選択する

ListBoxの項目をプログラムで選択する例です。

        private void button1_Click(object sender, EventArgs e)
        {
            // すべての項目の選択を解除
            listBox1.ClearSelected();

            // インデックス0番の項目を選択
            listBox1.SetSelected(0, true);
            listBox1.SelectedIndex = 0;     // こちらでもOK

            // Textプロパティに値を指定する方法もあります
            listBox1.Text = "苺";
            listBox1.SelectedItem = "苺";   // こちらでもOK

            // 複数の項目を選択する場合
            for (int index = 0; index < 3; index++)
                listBox1.SetSelected(index, true);
        }





Copyright © 2008.07 - shougo suzaki