Question

I've been looking for a solution since this morning, and even after reading tons of other threads on this subject it doesn't work for me. Without further ados let's check this code sample:

// Create Dictionary, Keys = Ids, Values = Names

Dictionary<int, string> ff_names = new Dictionary<int, string>();
ff_names.Add(0, "Cloud");
ff_names.Add(1, "Barret");
ff_names.Add(2, "Tifa");
ff_names.Add(3, "Aerith");
ff_names.Add(4, "Red XIII");

// Populating ListView

foreach( KeyValuePair<int, string> dict in ff_names )
{
    ListViewItem lvi = new ListViewItem(dict.Key.ToString());
    lvi.SubItems.Add(dict.Value);

    listView1.Items.Add(lvi);
}

// Test Item Selection

listView1.Focus();
listView1.Select();
listView1.Items[0].Focused = true;
listView1.Items[0].Selected = true;

string s = listView1.SelectedItems.Count.ToString();

label1.text = s; // sadly, it's equal to 0;
textBox1.Text = listView1.SelectedItems[0].SubItems[0].Text; // program will crash

Technically, I would like to selection an item of the ListView and display one of its element in a textbox. It works when I select an item manually, but when I try to select programmatically like shown above it doesn't want to select anything, the SelectedItems count is equal to zero...

Thank you for you help and hope someone can find a solution to what I'm missing!

Was it helpful?

Solution

Here you go. You'll have to make the event handler for listView1_SelectedIndexChanged.

    public Form1() {
        InitializeComponent();
        listView1.View = View.Details;
        listView1.Columns.Add("Key");
        listView1.Columns.Add("Value");
        LoadListView();
    }

    private void LoadListView() {
        // Create Dictionary, Keys = Ids, Values = Names

        Dictionary<int, string> ff_names = new Dictionary<int, string>();
        ff_names.Add(0, "Cloud");
        ff_names.Add(1, "Barret");
        ff_names.Add(2, "Tifa");
        ff_names.Add(3, "Aerith");
        ff_names.Add(4, "Red XIII");

        // Populating ListView

        foreach (KeyValuePair<int, string> dict in ff_names) {
            ListViewItem lvi = new ListViewItem(new string[] { dict.Key.ToString(), dict.Value });
            listView1.Items.Add(lvi);
        }

        // Test Item Selection

        listView1.Focus();
        listView1.Select();
        listView1.Items[0].Focused = true;
        listView1.Items[0].Selected = true;

    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
        if (listView1.SelectedItems.Count > 0) {
            label1.Text = (string)listView1.SelectedItems[0].Text; // sadly, it's equal to 0;
            textBox1.Text = (string)listView1.SelectedItems[0].SubItems[1].Text;
        }
    }

OTHER TIPS

I think drankin2112's answer is useful, but you say it doesn't work, so I complement it, hope this could be helpful to you. To finish your work, you need to do three things as follow: 1.load data and fill in listview; 2.define the process method when listview's selected item changed; 3.give a test method to programmatically select different item, the you can see result. my sample code is below:

    public MainWindow()
    {
        InitializeComponent();

        listView1.View = View.Details;
        listView1.Columns.Add("Key");
        listView1.Columns.Add("Value");
        this.listView1.FullRowSelect = true;
        //register the process event
        this.listView1.SelectedIndexChanged += this.listView1_SelectedIndexChanged;
        //load data
        LoadListView();
        //test item selection
        ToSelectItem(0);
    }

    void ToSelectItem(int itemIndex)
    {
        if (itemIndex > listView1.Items.Count - 1)
            return;
        listView1.Focus();
        listView1.Select();
        listView1.Items[itemIndex].Focused = true;
        listView1.Items[itemIndex].Selected = true;
    }

    private void LoadListView()
    {
        // Create Dictionary, Keys = Ids, Values = Names

        Dictionary<int, string> ff_names = new Dictionary<int, string>();
        ff_names.Add(0, "Cloud");
        ff_names.Add(1, "Barret");
        ff_names.Add(2, "Tifa");
        ff_names.Add(3, "Aerith");
        ff_names.Add(4, "Red XIII");

        // Populating ListView

        foreach (KeyValuePair<int, string> dict in ff_names)
        {
            ListViewItem lvi = new ListViewItem(new string[] { dict.Key.ToString(), dict.Value });
            listView1.Items.Add(lvi);
        }

    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            label1.Text = (string)listView1.SelectedItems[0].Text; // sadly, it's equal to 0;
            textBox1.Text = (string)listView1.SelectedItems[0].SubItems[1].Text;
        }
    }

the result of my test app

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top