[C#][Windows Formsアプリ][ListBox] アイテムの並び替え

スポンサーリンク

はじめに

この記事では、Windows Formsアプリ開発初心者向けに、ListBoxコントロールのSortedプロパティを使ったアイテムの並び替えについて詳しく解説します。ListBoxは、複数のアイテムをリスト形式で表示するためのコントロールで、Sortedプロパティを使うことで、アイテムを自動的にアルファベット順に並び替えることができます。この記事を通じて、Sortedプロパティの基本的な使い方から応用までを学び、ListBoxを自在に操れるようになりましょう。

Sortedプロパティの基本

Sortedプロパティを使うと、ListBoxに追加されたアイテムが自動的に並び替えられます。以下のコード例では、ListBoxにアイテムを追加し、Sortedプロパティにtrueをしてすることで並び替えを有効にする基本的な方法を紹介します。

using System;
using System.Windows.Forms;

namespace ListBoxExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeListBox();
        }

        private void InitializeListBox()
        {
            // ListBoxコントロールを作成
            ListBox listBox = new ListBox();
            listBox.Location = new System.Drawing.Point(10, 10);
            listBox.Size = new System.Drawing.Size(200, 150);

            // Sortedプロパティを有効にする
            listBox.Sorted = true;

            // Itemsプロパティを使用してアイテムを追加
            listBox.Items.Add("バナナ");
            listBox.Items.Add("りんご");
            listBox.Items.Add("オレンジ");

            // フォームにListBoxを追加
            this.Controls.Add(listBox);
        }
    }
}

Sortedプロパティの応用

Sortedプロパティを使って、動的にアイテムを追加しながら並び替えを行うことも可能です。以下のコード例では、テキストボックスに入力された値をListBoxに追加し、Sortedプロパティによって自動的に並び替えられる機能を実装しています。

using System;
using System.Windows.Forms;

namespace ListBoxExample
{
    public partial class Form1 : Form
    {
        private ListBox listBox;
        private TextBox textBox;
        private Button addButton;

        public Form1()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            // ListBoxコントロールを作成
            listBox = new ListBox();
            listBox.Location = new System.Drawing.Point(10, 10);
            listBox.Size = new System.Drawing.Size(200, 150);
            listBox.Sorted = true;

            // TextBoxコントロールを作成
            textBox = new TextBox();
            textBox.Location = new System.Drawing.Point(10, 170);
            textBox.Size = new System.Drawing.Size(200, 20);

            // Buttonコントロールを作成
            addButton = new Button();
            addButton.Text = "追加";
            addButton.Location = new System.Drawing.Point(220, 170);
            addButton.Click += new EventHandler(AddButton_Click);

            // フォームにコントロールを追加
            this.Controls.Add(listBox);
            this.Controls.Add(textBox);
            this.Controls.Add(addButton);
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            // TextBoxの内容をListBoxに追加
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                listBox.Items.Add(textBox.Text);
                textBox.Clear();
            }
        }
    }
}
実行例

実行例

まとめ

ListBoxのSortedプロパティを使うことで、アイテムを自動的に並び替えることができます。この記事では、基本的な並び替え方法から、動的にアイテムを追加する方法、さらによく使用されるプロパティやメソッドについて紹介しました。これらの知識を活用して、より便利で使いやすいWindows Formsアプリを作成してください。

 

Please follow and like us:

コメント

タイトルとURLをコピーしました