Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling Listview & Imagelist selected item confusion c#

Tags:

c#

I new to programming and C# and seemed to have worked myself into a bit muddle with the above. What i am trying to do is create a front end for the living room media pc nothing to fancy to start with as i understand this is a mamoth task for a total noobie like me. Ive flapped about and am totally fine with launching external exe's,storing/loading resouces ect.. and been very happy with my results for my 2 week surfing.

So im starting off my project by just launching an emulator to start with and what i would like to do is scan a folder for zip files and image files and if it finds matching image and zip files it displays an image in a list view for each zip found.

So i populate my listboxes like this and get my 2 listboxes showing the stuff i want to see.

PopulateListBox(listBox1, "\\SomePath\\", "*.zip");
PopulateListBox(listBox2, "\\Images\\", "*.jpg");

private void PopulateListBox(ListBox lsb, string Folder, string FileType)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Folder);
        FileInfo[] Files = dinfo.GetFiles(FileType);
        foreach (FileInfo file in Files)
        {
            lsb.Items.Add(file.Name);
        }
    }

So i now have my 2 listboxes and can see i have game1.zip and game1.jpg, great now i can populate my listview with the game1 image and launch the emulator he say's simple.

This is how i am currently populating the listview.

 PopulateListView();
 private void PopulateListView()
 {
  if (listBox1.Items.Contains("game1.zip"))
 {
   if (File.Exists("\\Images\\game1.jpg"))
    {
     imageList1.Images.Add(Image.FromFile("\\Images\\game1.jpg"));
     listView1.Items.Add("", 0);
    }
 }

 if (listBox1.Items.Contains("game2.zip"))
 {
   if (File.Exists("\\Images\\game2.jpg"))
    {
     imageList1.Images.Add(Image.FromFile("\\Images\\game2.jpg"));
     listView1.Items.Add("", 1);
    }
 }
}

This is how i am currently launching and it works ok.

 // launch item
 private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
 {
   if (listView1.Items[0].Selected == true)
    {
      string rom = "\\" + listBox1.Items[0].ToString();
      // Launch code in here
    }

    if (listView1.Items[1].Selected == true)
     {
       string rom = "\\" + listBox1.Items[1].ToString();
       // Launch code in here
     }
    }

So what the problem you may ask ? Instead of keep typing in all that info for each item i want to use some kind of statment if possible and i dont know what to search for which does not help. The image name will allways match the zip name just need to loose the file extensions so to populate the listview somthing like this.

if (listbox1 item = listbox2 item)
 {
  add to imagelist and listview automaticly with same index
 } 

Then i want to be able to launch just using somthing like this.

 private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
 {
      string rom = "\\" + listview.selected.item.tostring;
      // Launch code in here
 }

Hope im making sense im at my witts end.

Regards

Derek

like image 295
Chossy Avatar asked Oct 21 '22 01:10

Chossy


1 Answers

If i understand correctly, you want to read, match and show programmatically all images and zips that are the same and when the user clicks a list view row to start the rom. This can be done as follows:

  • read all images - there is no need to check if the file exists, because if it does not, then GetFiles will not find it!
  • read all roms - same as above!
  • take all names that have both image and zip file (that are the same)
  • fill image list and list view at the same time
  • bind list view item with the correct image
  • store rom location
  • handle mouse click on row and fetch rom location
  • do something with rom file

The code:

    public ListForm()
    {
        InitializeComponent();

        // path to images and roms
        const string location = @"d:\temp\roms\";
        // bind image list with list view
        listViewControl.SmallImageList = imageList;
        // get all images without extension
        var images = System.IO.Directory.GetFiles(location, "*.gif").Select(f => System.IO.Path.GetFileNameWithoutExtension(f)).ToList();
        // get all roms without extension
        var zips = System.IO.Directory.GetFiles(location, "*.zip").Select(f => System.IO.Path.GetFileNameWithoutExtension(f)).ToList();
        // find all entries (images and zips) that have the same name
        var matching = images.Intersect(zips);
        var imageIndex = 0;
        // fill image list and list view at the same time and store rom location
        foreach (var match in matching)
        {
            // path to file without extension
            var file = System.IO.Path.Combine(location, match);
            // add image to image list
            imageList.Images.Add(match, Bitmap.FromFile(string.Format("{0}.gif", file)));
            // create list view item 
            var lvi = new ListViewItem(match);
            // and set list view item image
            lvi.ImageIndex = imageIndex;
            // store rom location
            lvi.Tag = string.Format("{0}.zip", file);
            imageIndex++;
            // and show
            listViewControl.Items.Add(lvi);
        }
    }

    private void listViewControl_MouseClick(object sender, MouseEventArgs e)
    {
        // when user clicks an item, fetch the rom location and go
        var item = listViewControl.GetItemAt(e.X, e.Y);
        if (item != null)
        {
            var pathToRom = item.Tag as string;
            // do something with rom ...
        }
    }

There is no need to handle the image list and the list view separately. The output looks like:

enter image description here

like image 186
keenthinker Avatar answered Oct 23 '22 18:10

keenthinker