すらすらプログラマーへのお問い合わせ
すらすらのブログ
アクセス数  累計:000,099,221  昨日:000,000,137  本日:000,000,121
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/15
【Amazon ランキング:ゲーム - ダウンロード】

ここでは、リストボックスにアイテムの追加、削除を行う方法を説明します。
選択されたアイテムの取得
アイテムの追加、削除はリストアイテムプロパティの Insert、Remove メソッドを使って行います。
[VB]
Me.listBox1.Items.Insert(index, "アイテム1")
Me.listBox1.Items.Remove(Me.listBox1.SelectedItem)

[C#]
this.listBox1.Items.Insert(index, "アイテム1");
this.listBox1.Items.Remove(this.listBox1.SelectedItem); 
フォームの作成
Form2 に ListBox, Buton を貼り付けます。

ListBox:[listBox1]
Button:[button1]
Text 追加

Button:[button2]
Text 削除

VB[Form2.vb]
Public Class Form2

    
Private _itemNumber As Integer = 1

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

        
'アイテムの挿入位置
        Dim index As Integer = Me.listBox1.SelectedIndex
        
If index < 0 Then
            index = 0
        
End If

        ' アイテムの追加
        Me.listBox1.Items.Insert(index, String.Format("アイテム{0:000}", _itemNumber))
        _itemNumber = _itemNumber + 1

    
End Sub

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

        
' アイテムの削除
        If (Me.listBox1.SelectedItem Is Nothing = FalseThen
            Me.listBox1.Items.Remove(Me.listBox1.SelectedItem)
        
End If

    End Sub
End
 Class
C#[Form2.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 Form2 : Form
    {
        
private int _itemNumber = 1;

        
public Form2()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
// アイテムの挿入位置
            int index = this.listBox1.SelectedIndex;
            
if( index < 0 )
                index = 0;

            
// アイテムの追加
            this.listBox1.Items.Insert(index, string.Format("アイテム{0:000}", _itemNumber));
            _itemNumber++;
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
// アイテムの削除
            ifthis.listBox1.SelectedItem != null)
                
this.listBox1.Items.Remove(this.listBox1.SelectedItem); 
        }
    }
}
※このページで紹介しているサンプルコードについて管理者は動作保障をいたしません※
※サンプルコードを使用する場合は、自己責任でお願いします※

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




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

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