ListBox.Sort メソッドとは? わかりやすく解説

ListBox.Sort メソッド

ListBox 内の項目を並べ替えます。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

解説解説
使用例使用例

Sort メソッド使用するコード例次に示します。この例では、ListBox クラスから継承し派生クラスSort メソッドオーバーライドして、ユーザー定義の並べ替え実行する方法示します。この例を実行するには、次のコードを空のフォーム貼り付けます。

Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SortByLengthListBox1 = New SortByLengthListBox
        Me.SuspendLayout()
        Me.Button1.Location = New System.Drawing.Point(64,
 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(176,
 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Click me for list
 sorted by length"
        Me.SortByLengthListBox1.Items.AddRange(New
 Object() {"System", _
            "System.Windows.Forms", "System.Xml",
 _
            "System.Net", "System.Drawing",
 "System.IO"})
        Me.SortByLengthListBox1.Location = New
 System.Drawing.Point(72, 48)
        Me.SortByLengthListBox1.Name = "SortByLengthListBox1"
        Me.SortByLengthListBox1.Size = New
 System.Drawing.Size(120, 95)
        Me.SortByLengthListBox1.TabIndex = 1
        Me.ClientSize = New System.Drawing.Size(292,
 266)
        Me.Controls.Add(Me.SortByLengthListBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Sort Example"
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents Button1 As
 System.Windows.Forms.Button
    Friend WithEvents SortByLengthListBox1
 As SortByLengthListBox

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub

    
    Private Sub Button1_Click(ByVal
 sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 Button1.Click

        ' Set the Sorted property to True to raise the overridden Sort
        ' method.
        SortByLengthListBox1.Sorted = True
    End Sub
End Class

' This class inherits from ListBox and implements a different 
' sorting method. Sort will be called by setting the class's Sorted
' property to True.
Public Class SortByLengthListBox
    Inherits ListBox

    Public Sub New()
        MyBase.New()
    End Sub

    ' Overrides the parent class Sort to perform a simple
    ' bubble sort on the length of the string contained in each item.
    Protected Overrides Sub
 Sort()
        If (Items.Count > 1) Then

            Dim swapped As Boolean

            Do
                Dim counter As Integer
 = Items.Count - 1
                swapped = False
                While (counter - 1 > 0)

                    ' Compare the items' length.
                    If Items(counter).ToString.Length < _
                       Items(counter - 1).ToString.Length Then

                        ' If true, swap the items.
                        Dim temp As Object
 = Items(counter)
                        Items(counter) = Items(counter - 1)
                        Items(counter - 1) = temp
                        swapped = True

                    End If
                    ' Decrement the counter.
                    counter -= 1
                End While
            Loop While (swapped = True)
        End If
    End Sub

End Class
// The following code example demonstrates using the ListBox.Sort method
// by inheriting from the ListBox class and overriding the Sort method.


using System.Drawing;
using System.Windows.Forms;

public class Form1:
    System.Windows.Forms.Form
{

    internal System.Windows.Forms.Button Button1;
    internal SortByLengthListBox sortingBox;
    
    public Form1() : base()
    {        
        this.Button1 = new System.Windows.Forms.Button();
        this.sortingBox = 
            new SortByLengthListBox();
        this.SuspendLayout();
        this.Button1.Location = new System.Drawing.Point(64,
 16);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(176,
 23);
        this.Button1.TabIndex = 0;
        this.Button1.Text = "Click me for
 list sorted by length";
        this.Button1.Click += new System.EventHandler(Button1_Click);
        this.sortingBox.Items.AddRange(new
 object[]{"System", 
            "System.Windows.Forms", "System.Xml", "System.Net",
 
            "System.Drawing", "System.IO"});
        this.sortingBox.Location = 
            new System.Drawing.Point(72, 48);
        this.sortingBox.Size = 
            new System.Drawing.Size(120, 95);
        this.sortingBox.TabIndex = 1;
        this.ClientSize = new System.Drawing.Size(292,
 266);
        this.Controls.Add(this.sortingBox);
        this.Controls.Add(this.Button1);
        this.Name = "Form1";
        this.Text = "Sort Example";
        this.ResumeLayout(false);
    }
    
       public static void
 Main()
    {
        Application.Run(new Form1());
    }

    
    private void Button1_Click(System.Object
 sender, System.EventArgs e)
    {
        // Set the Sorted property to True to raise the overridden Sort
        // method.
        sortingBox.Sorted = true;
    }
}

// This class inherits from ListBox and implements a different 
// sorting method. Sort will be called by setting the class's Sorted
// property to True.
public class SortByLengthListBox:
    ListBox

{
    public SortByLengthListBox() : base()
    {        
    }

    // Overrides the parent class Sort to perform a simple
    // bubble sort on the length of the string contained in each item.
    protected override void Sort()
    {
        if (Items.Count > 1)
        {
            bool swapped;
            do
            {
                int counter = Items.Count - 1;
                swapped = false;
                
                while (counter - 1 > 0)
                {
                    // Compare the items' length. 
                    if (Items[counter].ToString().Length  
                        < Items[counter-1].ToString().Length)
                    {
                        // Swap the items.
                        object temp = Items[counter];
                        Items[counter] = Items[counter-1];
                        Items[counter-1] = temp;
                        swapped = true;
                    }
                    // Decrement the counter.
                    counter -= 1;
                }
            }
            while((swapped==true));
        }
    }
}

// The following code example demonstrates using the ListBox.Sort method
// by inheriting from the ListBox class and overriding the Sort method.
using namespace System::Drawing;
using namespace System::Windows::Forms;

// This class inherits from ListBox and implements a different 
// sorting method. Sort will be called by setting the class's Sorted
// property to True.
public ref class SortByLengthListBox: public
 ListBox
{
public:
   SortByLengthListBox()
      : ListBox()
   {}

protected:

   // Overrides the parent class Sort to perform a simple
   // bubble sort on the length of the string contained in each item.
   virtual void Sort() override
   {
      if ( Items->Count > 1 )
      {
         bool swapped;
         do
         {
            int counter = Items->Count - 1;
            swapped = false;
            while ( counter - 1 > 0 )
            {
               
               // Compare the items' length. 
               if ( Items[ counter ]->ToString()->Length
 < Items[ counter - 1 ]->ToString()->Length )
               {
                  
                  // Swap the items.
                  Object^ temp = Items[ counter ];
                  Items[ counter ] = Items[ counter - 1 ];
                  Items[ counter - 1 ] = temp;
                  swapped = true;
               }
               
               // Decrement the counter.
               counter -= 1;
            }
         }
         while ( (swapped == true) );
      }
   }
};

public ref class Form1: public
 System::Windows::Forms::Form
{
internal:
   System::Windows::Forms::Button^ Button1;
   SortByLengthListBox^ sortingBox;

public:
   Form1()
      : Form()
   {
      this->Button1 = gcnew System::Windows::Forms::Button;
      this->sortingBox = gcnew SortByLengthListBox;
      this->SuspendLayout();
      this->Button1->Location = System::Drawing::Point(
 64, 16 );
      this->Button1->Name = "Button1";
      this->Button1->Size = System::Drawing::Size( 176,
 23 );
      this->Button1->TabIndex = 0;
      this->Button1->Text = "Click me for
 list sorted by length";
      this->Button1->Click += gcnew System::EventHandler(
 this, &Form1::Button1_Click );
      array<Object^>^temp0 = {"System","System.Windows.Forms"
,"System.Xml","System.Net","System.Drawing","System.IO"};
      this->sortingBox->Items->AddRange( temp0 );
      this->sortingBox->Location = System::Drawing::Point(
 72, 48 );
      this->sortingBox->Size = System::Drawing::Size( 120,
 95 );
      this->sortingBox->TabIndex = 1;
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Controls->Add( this->sortingBox
 );
      this->Controls->Add( this->Button1
 );
      this->Name = "Form1";
      this->Text = "Sort Example";
      this->ResumeLayout( false );
   }

private:
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      // Set the Sorted property to True to raise the overridden Sort
      // method.
      sortingBox->Sorted = true;
   }

};

int main()
{
   Application::Run( gcnew Form1 );
}
// The following code example demonstrates using the ListBox.Sort method
// by inheriting from the ListBox class and overriding the Sort method.
import System.Drawing.*;
import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.Button button1;
    private SortByLengthListBox sortingBox;

    public Form1()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.sortingBox = new SortByLengthListBox();
        this.SuspendLayout();
        this.button1.set_Location(new System.Drawing.Point(64,
 16));
        this.button1.set_Name("Button1");
        this.button1.set_Size(new System.Drawing.Size(176,
 23));
        this.button1.set_TabIndex(0);
        this.button1.set_Text("Click me for
 list sorted by length");
        this.button1.add_Click(new System.EventHandler(button1_Click));
        this.sortingBox.get_Items().AddRange(new
 Object[] { "System", 
            "System.Windows.Forms", "System.Xml", "System.Net",
 
            "System.Drawing", "System.IO" });
        this.sortingBox.set_Location(new System.Drawing.Point(72,
 48));
        this.sortingBox.set_Size(new System.Drawing.Size(120,
 95));
        this.sortingBox.set_TabIndex(1);
        this.set_ClientSize(new System.Drawing.Size(292,
 266));
        this.get_Controls().Add(this.sortingBox);
        this.get_Controls().Add(this.button1);
        this.set_Name("Form1");
        this.set_Text("Sort Example");
        this.ResumeLayout(false);
    } //Form1

    public static void main(String[]
 args)
    {
        Application.Run(new Form1());
    } //main

    private void button1_Click(Object sender,
 System.EventArgs e)
    {
        // Set the Sorted property to True to raise the overridden Sort
        // method.
        sortingBox.set_Sorted(true);
    } //button1_Click
} //Form1

// This class inherits from ListBox and implements a different 
// sorting method. Sort will be called by setting the class's Sorted
// property to True.
public class SortByLengthListBox extends ListBox
{
    public SortByLengthListBox()
    {
        super();
    } //SortByLengthListBox

    // Overrides the parent class Sort to perform a simple
    // bubble sort on the length of the string contained in each item.
    protected void Sort()
    {
        if (get_Items().get_Count() > 1) {
            boolean swapped;
            do {
                int counter = get_Items().get_Count() - 1;
                swapped = false;

                while (counter - 1 > 0) {
                    // Compare the items' length. 
                    if (get_Items().get_Item(counter).ToString().get_Length()
 
                        < get_Items().get_Item((counter - 1)).
                        ToString().get_Length()) {
                        // Swap the items.
                        Object temp = get_Items().get_Item(counter);
                        get_Items().set_Item(counter, get_Items().
                            get_Item((counter - 1)));
                        get_Items().set_Item((counter - 1), temp);
                        swapped = true;
                    }
                    // Decrement the counter.
                    counter -= 1;
                }
            } while (swapped == true);
        }
    } //Sort
} //SortByLengthListBox
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からListBox.Sort メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からListBox.Sort メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からListBox.Sort メソッドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ListBox.Sort メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ListBox.Sort メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS