Question from newbie about reading file name from listview

Hello I've been searching forums for solution of my problem. I think it's very simple but I have no idea how to make it works. In my project I have got a simple listview with *.txt file names from program directory. I would apprecciate it if someone told me how to get value (file name) of currently selected key. Any help would be welcomed!

Answer this question

Question from newbie about reading file name from listview

  • Scheinka

    Hi Pryngiel,

    you can refer to the item with the selecteditems property of the listview. you can get the text value by using listview1.selecteditems(0).text if you are on the first column. hope this helps...

    Dave


  • Ta1os

    Thanks for the answer! I have something like this: private void listView1_SelectedIndexChanged(object sender, EventArgs e) { string Itm; Itm = listView1.SelectedItems(0).Text; } but... it takes error "Error 1 'System.Windows.Forms.ListView' does not contain a definition for 'SelectedItems'" So I have to define listView1.SelectedItems, how to do it I suppose it's something like this "this.listView1.SelectedItems += new " but what's next after "new"
  • chipjollyroger

    Something tells me we're not using the same programing language lol...

  • TobsTec

    I've just made it work!
    It goes like this:
    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    string Itm;
    for (int i = 0; i < listView1.Items.Count; i++)
    {
    if (listView1.ItemsIdea.Selected)
    {
    Itm = listView1.ItemsIdea.Text.ToString();
    MessageBox.Show(Itm);
    }
    }

    Thanks, for trying to help!

  • Tryst

    Hi Pryngiel,

    Oops, that was on VB, my bad... try this on c#:

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
    string itm = this.listView1.SelectedItems[0].Text;
    MessageBox.Show(itm);
    }

    just to show that you're getting the the value when you double click. you can change the event for your own purpose :)

    Dave.


  • Patrick0

    SelectedItems is not in Compact Framework

    I used somethink like:

    ListView.SelectedIndexCollection indexes = this.MyList.SelectedIndices;

    foreach (int index in indexes)

    {

    fieldtext1.Text = MyList.Items[index].Text;

    fieldtext2.Text = MyList.Items[index].SubItems[2].Text;

    }

    index on PDA is only 1 number .... there is not multiline choose


  • Pockey

    Hi Dave,
    When I try to bulid application I'm still getting "Error 1 'System.Windows.Forms.ListView' does not contain a definition for 'SelectedItems'". Do you konow how to describe "SelectedItems" in private void InitializeComponent() in ListView section
    Sorry for being importunate but I really have no idea how to do it...

  • Question from newbie about reading file name from listview