すらすらプログラマーへのお問い合わせ
すらすらのブログ
アクセス数  累計:000,099,194  昨日:000,000,137  本日:000,000,094
Loading
リストボックス
.NET プログラミング
プロパティグリッド
印刷
リストボックス
スレッド
リストビュー
インストーラー
やってみよう!

できるVisual Studio 2015 Windows /Android/iOS アプリ対応

独習C# 第3版

VisualBasic2013パーフェクトマスター (Perfect Master SERIES)

プログラミング.NET Framework 第4版 (Microsoft Press)

VisualBasic2013逆引き大全555の極意

猫でもわかるWindowsプログラミング 第4版 (猫でもわかるプログラミング)

VisualC#2013逆引き大全555の極意

複数行選択 最終更新:2010/07/22
【Amazon ランキング:本 - ビジネス・経済】

リストボックスは複数行選択することも可能です。
ここでは複数選択されたアイテムの情報を取得する方法を説明します。
選択されたアイテムの取得
複数選択されたアイテムを取得するには ListBox.SelectedItems プロパティを参照します。このプロパティには選択されているアイテムがコレクションに格納されています。
[VB]
Me.listBox1.SelectedItems

[C#]
this.listBox1.SelectedItems;
フォームの作成
Form1 に ListBox, Label をはりつけます。
ListBox の SelectionMode プロパティを [MultiSimple],[MultiExtended]のどちらかに設定すると複数行選択できるようになります。

ListBox:[listBox1]
SelectionMode MultiSimple

Label:[Label1]

Label:[Label2]
VB[Form1.vb]
Public Class Form1

    
Private Sub Form1_Load(ByVal sender As Object, _
                           
ByVal e As System.EventArgs) Handles Me.Load
        
' リストボックスアイテム追加
        For i As Integer = 0 To 99
            
Me.listBox1.Items.Add(String.Format("アイテム{0:00}", i))
        
Next
    End Sub

    Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, _
                                              
ByVal e As System.EventArgs) _
                                              
Handles listBox1.SelectedIndexChanged

        
' リストボックスで選択されているアイテムを表示
        Dim itemsText As StringBuilder = New StringBuilder()
        
For Each item As Object In Me.listBox1.SelectedItems
            
If (itemsText.Length > 0) Then itemsText.Append(",")
            itemsText.Append(item.ToString())
        
Next

        Me.label2.Text = itemsText.ToString()
    
End Sub
End
 Class
C#[Form1.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        
private void Form1_Load(object sender, EventArgs e)
        {
            
// リストボックスボックスアイテム追加
            forint i=0 ; i< 100 ; i++ )
                
this.listBox1.Items.Add(string.Format("アイテム{0:000}",i));

        }

        
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
// リストボックスで選択されているアイテムを表示
            StringBuilder itemsText = new StringBuilder();
            
foreach (object item in this.listBox1.SelectedItems)
            {
                
if (itemsText.Length > 0) itemsText.Append(",");
                itemsText.Append(item.ToString());
            }

            
this.label2.Text = itemsText.ToString();
        }
    }
}
※このページで紹介しているサンプルコードについて管理者は動作保障をいたしません※
※サンプルコードを使用する場合は、自己責任でお願いします※

【楽天 ランキング:パソコン・周辺機器 - ノートパソコン】




このサイトはフリーソフトのMerge HTMLで作成されています。
このサイトはリンクフリーです。

ページの先頭に戻る Copyright© 2010-2015 Jun.Shiozaki All rights reserved.