0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# - ListViewのサンプル コードべた書き(Visual Studio不使用)

Last updated at Posted at 2020-10-26

目次: C# - Windows Formsでよく使うコントロールたち (Visual Studioなし環境向け) - Qiita

画面キャプチャ

image.png

テンプレ


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

class ListViewSample : Form
{
    ListView lsv;

    ListViewSample()
    {
        ClientSize = new Size(500, 300);
        
        Controls.Add(lsv = new ListView() {
            Dock = DockStyle.Fill,
            View = View.Details,
            FullRowSelect = true,
            HideSelection = false,
            MultiSelect = false,
            GridLines = true,
        });

        // 列ヘッダを登録
        lsv.Columns.Add("angle", 70, HorizontalAlignment.Left);
        lsv.Columns.Add("cos(angle)", 70, HorizontalAlignment.Left);
        lsv.Columns.Add("sin(angle)", 70, HorizontalAlignment.Left);

        lsv.SelectedIndexChanged += Lsv_SelectedItemChanged;

        AddItemsToTheListView();
    }

    void Lsv_SelectedItemChanged(object sender, EventArgs e)
    {
        if ( lsv.SelectedItems.Count == 1 ) { // 無選択(0個)になる場合がありえるため、チェックしている。
            // Form のタイトルを変更
            Text = "angle="+lsv.SelectedItems[0].SubItems[0].Text + ", cos(angle)=" + lsv.SelectedItems[0].SubItems[1].Text;
        }
        else {
            Text = "No item is selected.";
        }
    }

    void AddItemsToTheListView()
    {
        lsv.Items.Clear(); // ※Itemsを忘れると列ヘッダごと消えます

        lsv.BeginUpdate();
        
        for ( int i=0; i<100; i++ ) {
            double degree = i*7.5;
            double x = degree*Math.PI/180.0;
            double cosX = Math.Cos(x);
            double sinX = Math.Sin(x);
            lsv.Items.Add(new ListViewItem(new string[]{degree.ToString("F1"),cosX.ToString("F3"),sinX.ToString("F3")}));
        }

        lsv.EndUpdate();
    }
    

    [STAThread]
    static void Main()
    {
        Application.Run(new ListViewSample());
    }
}

参考サイト

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?