如何以编程方式选择ListView项目?

时间:2013-12-07 22:40:00

标签: c# winforms listview selecteditem

我从今天早上起就一直在寻找解决方案,甚至在阅读了关于这个主题的大量其他主题之后,它对我来说也不起作用。如果没有进一步说明,请查看此代码示例:

// 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

从技术上讲,我想选择ListView的一个项目,并在文本框中显示其中一个元素。它在我手动选择项目时有效,但是当我尝试以编程方式选择如上所示它不想选择任何内容时,SelectedItems计数等于零......

感谢您的帮助,希望有人能找到我缺少的解决方案!

2 个答案:

答案 0 :(得分:1)

你走了。您必须为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;
        }
    }

答案 1 :(得分:0)

我认为drankin2112的答案很有用,但你说它不起作用,所以我补充它,希望这对你有所帮助。 要完成工作,您需要做以下三件事: 1.加载数据并填写listview; 2.当listview的选定项目发生变化时,定义处理方法; 3.给出一个测试方法,以编程方式选择不同的项目,你可以看到结果。 我的示例代码如下:

    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