如何在选择时将ListView中所选项目的内容打印到文本框中?

时间:2011-12-08 06:49:04

标签: c# listview

我正在尝试将所选行的内容显示在文本框中。此代码不起作用,因为它只添加第一个选定的项目。

编辑:这种工作,但我的显示看起来像这样:ListViewSubItem: {a} ListViewSubItem: {b}

if (!string.IsNullOrEmpty(PC.SubItems[1].Text) && !string.IsNullOrEmpty(PC.SubItems[2].Text))
{
      txtPc.Text = e.Item.SubItems[1].ToString() 
      + " " + e.Item.SubItems[2].ToString();
}

编辑:这很有效:

private void SelectedItem(object sender, ListViewItemSelectionChangedEventArgs e)
{
    if (tabSelectPage.SelectedTab != tabPage2)
        txtSelected.Text = "User: " + e.Item.SubItems[1].Text + 
            "Pass" + e.Item.SubItems[2].Text;
    else
        txtSelected.Text = "URL: " + e.Item.SubItems[1].Text + 
            "User: " + e.Item.SubItems[2].Text + 
            "Pass" + e.Item.SubItems[3].Text;
}

2 个答案:

答案 0 :(得分:2)

我就是这样做的:

    private void SelectedItem(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        if (tabSelectPage.SelectedTab == tabPage1)
            txtSelected.Text = " User Name:  " + e.Item.SubItems[1].Text +
                "     Password:  " + e.Item.SubItems[2].Text;
        else if (tabSelectPage.SelectedTab == tabPage2)
            txtSelected.Text = " URL:  " + e.Item.SubItems[1].Text +
                "     User Name:  " + e.Item.SubItems[2].Text +
                "     Password:  " + e.Item.SubItems[3].Text;
        else if (tabSelectPage.SelectedTab == tabPage3)
            txtSelected.Text = " Software Name:  " + e.Item.SubItems[1].Text +
                "     Serial Code:  " + e.Item.SubItems[2].Text;
    }

答案 1 :(得分:1)