加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

填写列表视图和图像列表选择项目混淆c#

发布时间:2020-12-15 05:39:47 所属栏目:百科 来源:网络整理
导读:我是编程和C#的新手,似乎已经让自己陷入了上述困境. 我想要做的是创建一个客厅媒体电脑的前端没有什么可以开始的,因为我明白这是一个像我这样的完全noobie的妈妈的任务. 我已经拍完了,完全可以使用外部exe,存储/加载资源……并且对我2周冲浪的结果非常满意.
我是编程和C#的新手,似乎已经让自己陷入了上述困境.
我想要做的是创建一个客厅媒体电脑的前端没有什么可以开始的,因为我明白这是一个像我这样的完全noobie的妈妈的任务.
我已经拍完了,完全可以使用外部exe,存储/加载资源……并且对我2周冲浪的结果非常满意.

所以我开始我的项目只是启动一个模拟器开始,我想要做的是扫描文件夹中的zip文件和图像文件,如果找到匹配的图像和zip文件,它会在列表视图中显示每个拉链发现.

所以我填充这样的列表框,让我的2个列表框显示我想看的东西.

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);
        }
    }

所以我现在有了我的2个列表框,可以看到我有game1.zip和game1.jpg,现在我可以使用game1图像填充我的listview并启动他说简单的模拟器.

这就是我目前填充列表视图的方式.

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

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

这就是我目前正在推出的方式,它运作正常.

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

那你可能会问什么问题?
而不是继续输入每个项目的所有信息我想要使用某种声明,如果可能的话,我不知道搜索什么没有帮助.
图像名称将始终与zip名称匹配,只需要松开文件扩展名,以便填充listview这样的东西.

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

然后我希望能够使用这样的东西启动.

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

希望我在我的智慧结束时有意义.

问候

德里克

解决方法

如果我理解正确,您希望以编程方式读取,匹配和显示所有相同的图像和拉链,以及当用户单击列表视图行以启动ROM时.这可以按如下方式完成:

>读取所有图像 – 无需检查文件是否存在,因为如果不存在,则GetFiles将无法找到它!
>阅读所有的roms – 与上面相同!
>取所有同时包含图像和zip文件的名称(相同)
>同时填充图像列表和列表视图
>使用正确的图像绑定列表视图项
>存储rom位置
>处理鼠标单击行并获取rom位置
>用rom文件做一些事情

代码:

public ListForm()
    {
        InitializeComponent();

        // path to images and roms
        const string location = @"d:temproms";
        // 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 ...
        }
    }

无需单独处理图像列表和列表视图.输出如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读