使这段代码运行得更快,而不是在VB.NET中使用带有列表框的selecteditem [英] Make this code run faster, instead of using selecteditem with listsbox in VB.NET

查看:61
本文介绍了使这段代码运行得更快,而不是在VB.NET中使用带有列表框的selecteditem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表框中的每个项目选择都需要很长时间......我正在寻找一种更快捷的方式在图片框中加载36张图片



我尝试了什么:



It takes too long for each selection of items in listbox... I'm looking for a faster way to load the 36 images in picture boxes on my form

What I have tried:

If ListBox2.Items.Count > 36 Then

      ListBox2.SelectedIndex = 0
      PictureBox1.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 1
      PictureBox2.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 2
      PictureBox3.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 3
      PictureBox4.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 4
      PictureBox5.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 5
      PictureBox6.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 6
      PictureBox7.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 7
      PictureBox8.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 8
      PictureBox9.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 9
      PictureBox10.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 10
      PictureBox11.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 11
      PictureBox12.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 12
      PictureBox13.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 13
      PictureBox14.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 14
      PictureBox15.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 15
      PictureBox16.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 16
      PictureBox17.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 17
      PictureBox18.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 18
      PictureBox19.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 19
      PictureBox20.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 20
      PictureBox21.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 21
      PictureBox22.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 22
      PictureBox23.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 23
      PictureBox24.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 24
      PictureBox25.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 25
      PictureBox26.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 26
      PictureBox27.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 27
      PictureBox28.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 28
      PictureBox29.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 29
      PictureBox30.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 30
      PictureBox31.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 31
      PictureBox32.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 32
      PictureBox33.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 33
      PictureBox34.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 34
      PictureBox35.Image = Image.FromFile(ListBox2.SelectedItem)

      ListBox2.SelectedIndex = 35
      PictureBox36.Image = Image.FromFile(ListBox2.SelectedItem)


  Else
      '
  End If

推荐答案

为什么每次都设置SelectedIndex,而不是使用 ListBox.Items属性(System.Windows.Forms) [ ^ ] dire ctly?

这可能会加快一点,但是......如果你从一个文件加载35个图像,那么它将变慢,取决于各个图像的大小。



首先使用秒表 [ ^ ]计算整个负载的时间,因此您可以获得一个基线。然后使用更多的秒表来计算单个负载的时间并找出慢速代码的位置。一旦你有了这个,你可以开始看有可能显着改善这个吗?但直到那时。
Why are you setting the SelectedIndex each time, instead of using the ListBox.Items Property (System.Windows.Forms)[^] directly?
That may speed it up a little, but ... if you are loading 35 images from a file then it's going to be slow, depending on how big the individual images are.

Start by using a Stopwatch[^] to time the whole load, so you get a base line to work from. Then use more Stopwatches to time the individual loads and find out where the "slow code" is. Once you have that, you can start looking at "is it possible to significantly improve this?" but not until then.


您不需要设置所选索引;你可以直接索引列表框项目。



... Image = Image.FromFile(ListBox2.Items [0])



但这可能是文件阅读,需要时间。您应该考虑后台工作程序,另一个线程或异步进程来加载列表框以提高速度感知。
You don't need to set the selected index; you can index list box items directly.

... Image = Image.FromFile(ListBox2.Items[0])

But it's probably "file reading" that's taking the time. You should consider a background worker, another thread or async process to load your list box to improve speed "perception".


这篇关于使这段代码运行得更快,而不是在VB.NET中使用带有列表框的selecteditem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆