2
votes

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!

2
Literarily copy-pasted your code and it worked for me. The label1 even showed 1.user2509901
Ditto. Do you have any event handlers on the listView that could be mucking things up?Steve Wellens
Actually, I use a form that inherits from another form which contains the listview control, but as I said when I select the listview manually with the mouse it works.TheScholar
P.S: But no, I haven't used any event handlers so far.TheScholar
Hey Chrismas Unicorn, when you copy-pasted my code, what else special did you do with the Form?TheScholar

2 Answers

2
votes

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;
        }
    }
0
votes

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