トップ  >  サンプルソース  >  ListView追加/参照/削除(C#/VB.NET)

ListView追加/参照/削除(C#/VB.NET)

2010年06月19日

ListViewに行追加/行変更(行取得)/行削除するサンプル(ソース/コード)です。
忘れやすいのでメモって置きます。

全ての行(項目)を削除する時は、ListView1.Items.Clear()を使用します。

' -----------------------------------------------------------
' ListViewの追加/変更(取得)/削除のサンプル(VB.NET/VS2005)
Private Sub Form1_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load

    ' ヘッダー初期化
    ListView1.Items.Clear()
    ListView1.FullRowSelect = True
    ListView1.GridLines = True
    ListView1.View = View.Details
    Dim column1 As ColumnHeader = New ColumnHeader
    Dim column2 As ColumnHeader = New ColumnHeader
    Dim column3 As ColumnHeader = New ColumnHeader
    column1.Text = "Hour"
    column2.Text = "Minute"
    column3.Text = "Second"
    Dim colHeaderRegValue() As ColumnHeader = _
      {column1, column2, column3}
    ListView1.Columns.AddRange(colHeaderRegValue)
End Sub

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

    ' 選択行特定
    Dim intRow As Integer = -1
    If ListView1.SelectedItems.Count > 0 Then
        intRow = ListView1.SelectedItems(0).Index
    End If

    If intRow < 0 Then
        ' 行追加
        Dim citm(2) As String
        citm(0) = DateTime.Now.Hour
        citm(1) = DateTime.Now.Minute
        citm(2) = DateTime.Now.Second
        Dim ritm As ListViewItem = New ListViewItem(citm)
        ListView1.Items.Add(ritm)
    Else
        ' 行挿入
        Dim citm(2) As String
        citm(0) = DateTime.Now.Hour
        citm(1) = DateTime.Now.Minute
        citm(2) = DateTime.Now.Second
        Dim ritm As ListViewItem = New ListViewItem(citm)
        ListView1.Items.Insert(intRow, ritm)
        ListView1.Select()
    End If
End Sub

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

    ' 選択行特定
    Dim intRow As Integer = -1
    If ListView1.SelectedItems.Count > 0 Then
        intRow = ListView1.SelectedItems(0).Index
    End If
    If intRow < 0 Then Return

    ' 選択行更新
    Dim citm(2) As String
    citm(0) = DateTime.Now.Hour
    citm(1) = DateTime.Now.Minute
    citm(2) = DateTime.Now.Second
    Dim ritm As ListViewItem = New ListViewItem(citm)
    ListView1.Items(intRow) = ritm

    ' 再選択
    If ListView1.Items.Count > intRow Then
        ListView1.Items(intRow).Selected = True
        ListView1.Select()
    End If
End Sub

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

    ' 選択行特定
    Dim intRow As Integer = -1
    If ListView1.SelectedItems.Count > 0 Then
        intRow = ListView1.SelectedItems(0).Index
    End If
    If intRow < 0 Then Return

    ' 選択行削除
    ListView1.Items.RemoveAt(intRow)

    ' 再選択
    If ListView1.Items.Count > intRow Then
        ListView1.Items(intRow).Selected = True
        ListView1.Select()
    End If
End Sub

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

    ' 選択行特定
    Dim intRow As Integer = -1
    If ListView1.SelectedItems.Count > 0 Then
        intRow = ListView1.SelectedItems(0).Index
    End If
    If intRow < 0 Then Return

    ' 選択行参照
    System.Diagnostics.Debug.WriteLine( _
        ListView1.Items(intRow).SubItems(0).Text & ":" & _
        ListView1.Items(intRow).SubItems(1).Text & ":" & _
        ListView1.Items(intRow).SubItems(2).Text)

    ' 再選択
    If ListView1.Items.Count > intRow Then
        ListView1.Items(intRow).Selected = True
        ListView1.Select()
    End If
End Sub
' -----------------------------------------------------------

// ----------------------------------------------------------
// ListViewの追加/変更(取得)/削除のサンプル(C#.NET/VS2005)
private void Form1_Load(object sender, EventArgs e)
{
    // ヘッダー初期化
    listView1.Items.Clear();
    listView1.FullRowSelect = true;
    listView1.GridLines = true;
    listView1.View = View.Details;
    ColumnHeader column1 = new ColumnHeader();
    ColumnHeader column2 = new ColumnHeader();
    ColumnHeader column3 = new ColumnHeader();
    column1.Text = "Hour";
    column2.Text = "Minute";
    column3.Text = "Second";
    ColumnHeader[] colHeaderRegValue = new ColumnHeader[]
      {column1, column2, column3};
    listView1.Columns.AddRange(colHeaderRegValue);
}

private void button1_Click(object sender, EventArgs e)
{
    // 選択行特定
    int intRow  = -1;
    if (listView1.SelectedItems.Count > 0)
    {
        intRow = listView1.SelectedItems[0].Index;
    }

    if (intRow < 0)
    {
        // 行追加
        String[] citm = new String[3];
        citm[0] = DateTime.Now.Hour.ToString();
        citm[1] = DateTime.Now.Minute.ToString();
        citm[2] = DateTime.Now.Second.ToString();
        ListViewItem ritm = new ListViewItem(citm);
        listView1.Items.Add(ritm);
    }
    else
    {
        // 行挿入
        String[] citm = new String[3];
        citm[0] = DateTime.Now.Hour.ToString();
        citm[1] = DateTime.Now.Minute.ToString();
        citm[2] = DateTime.Now.Second.ToString();
        ListViewItem ritm = new ListViewItem(citm);
        listView1.Items.Insert(intRow, ritm);
        listView1.Select();
    }
}

private void button2_Click(object sender, EventArgs e)
{
    // 選択行特定
    int intRow = -1;
    if (listView1.SelectedItems.Count > 0)
    {
        intRow = listView1.SelectedItems[0].Index;
    }
    if (intRow < 0) return;

    // 選択行更新
    String[] citm = new String[3];
    citm[0] = DateTime.Now.Hour.ToString();
    citm[1] = DateTime.Now.Minute.ToString();
    citm[2] = DateTime.Now.Second.ToString();
    ListViewItem ritm = new ListViewItem(citm);
    listView1.Items[intRow] = ritm;

    // 再選択
    if (listView1.Items.Count > intRow)
    {
        listView1.Items[intRow].Selected = true;
        listView1.Select();
    }
}

private void button3_Click(object sender, EventArgs e)
{
    // 選択行特定
    int intRow = -1;
    if (listView1.SelectedItems.Count > 0)
    {
        intRow = listView1.SelectedItems[0].Index;
    }
    if (intRow < 0) return;

    // 選択行削除
    listView1.Items.RemoveAt(intRow);

    // 再選択
    if (listView1.Items.Count > intRow)
    {
        listView1.Items[intRow].Selected = true;
        listView1.Select();
    }
}

private void button4_Click(object sender, EventArgs e)
{
    // 選択行特定
    int intRow = -1;
    if (listView1.SelectedItems.Count > 0)
    {
        intRow = listView1.SelectedItems[0].Index;
    }
    if (intRow < 0) return;

    // 選択行参照
    System.Diagnostics.Debug.WriteLine(
        listView1.Items[intRow].SubItems[0].Text + ":" +
        listView1.Items[intRow].SubItems[1].Text + ":" +
        listView1.Items[intRow].SubItems[2].Text);

    // 再選択
    if (listView1.Items.Count > intRow)
    {
        listView1.Items[intRow].Selected = true;
        listView1.Select();
    }
}
// ----------------------------------------------------------
スポンサーサイト

TreeView追加/参照/削除(C#/VB.NET) | トップページへ戻る | ComboBox追加/参照/削除(C#/VB.NET)

このページのトップに戻る

コメント

名前
題名
メールアドレス
WEBサイト
 
コメント
パスワード
  管理者にだけ表示を許可する

このページのトップに戻る

トラックバック

このページのトップに戻る