一次取一个列表框项目然后执行代码 [英] Take One List Box Item At A Time Then Perform Code

查看:74
本文介绍了一次取一个列表框项目然后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI




所以基本上我想一次为列表框拿一个项目,如果标签''timer''达到0那么拿另一个项目并用它执行一些代码,直到列表框的所有项目都被执行。



i有这个但是我不确定它是否正确

 对于 每个项目  .ListBox1.Items 





但是我不知道怎么说如果计时器点击0然后一次又一次地对项目执行操作,直到系统地所有项目一次执行一个。



例如



如果timer =0那么

随机函数

取一个列表框项目和执行鳕鱼

其他

什么都不做







等请帮助我:))

解决方案

请按照以下链接:

ListBox类 [ ^ ]

从Windows窗体ComboBox,ListBox或CheckedListBox控件添加和删除项目 [ ^ ]

Timer Class(System.Threading) [ ^ ]

Timer Class(System.Windows.Forms) [ ^ ]

有你''找到例子......;)


要做的第一件事就是停止使用标签文本作为倒计时 - 你可以用它来向用户展示价值,但在内部你应该使用类级变量,可能是整数。



你可能正在处理Timer.Tick事件,并改变标签文本:相反,改变一个整数,并将整数的新值输出到tick处理程序方法末尾的标签。在处理程序中,检查新值是否小于零,如果它大于你需要:

1)将其设置为完整周期值

2 )检查列表框是否包含任何项目。如果是,则继续

3)从列表中获取第一项:这只是从获取项目的情况.Listbox1.Items(0)并将其存储在适当的变量中。然后,您可以从列表框中删除它 - 使用ListBox1.Items.Remove并提供刚刚检索到的值或者使用Listbox1.Items.RemoveAt(0)

4)处理您的项目。



在任何时候你都不需要循环 - 这是由计时器有效提供的。



如果处理你的物品可能需要很长一段时间,然后你可能需要开始使用BackgroundWorker或类似的,但这是高级课程! :笑:

HI

So basically i want to take one item for a listbox at a time and if the label ''timer'' reaches 0 then take another item and do perform some code with it until all the items of the listbox has been executed.

i have this but i am not sure if it is right

For Each item In Me.ListBox1.Items 



but i am not sure how to say if the timer hits 0 then perform the actions with the items again and again until systematically all the items have been executed one at a time.

for example

if timer = "0" then
random function
take one listbox item and perform cod
else
do nothing



and so on please help me :)

解决方案

Follow the below links:
ListBox Class[^]
Adding and Removing Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control[^]
Timer Class (System.Threading)[^]
Timer Class (System.Windows.Forms)[^]
There you''ll find examples... ;)


The first thing to do is stop using the label text as a countdown - you can use it to present the value to the user, but internally you should be using a class level variable, probably integer.

You are presumably handling the Timer.Tick event, and altering the label text in that: instead, alter an integer, and output the new value of the integer to the label at the end of the tick handler method. In the handler, check if the new value is less than zero, if it is than you need to:
1) Set it to the "full period" value
2) Check if the listbox contains any items. If it does, then continue
3) Get the first item from the list: this is just a case of getting the item from Listbox1.Items(0) and storing it in an appropriate variable. You can then remove it from the listbox -using either ListBox1.Items.Remove and supplying the value you just retrieved or with Listbox1.Items.RemoveAt(0)
4) Process your item.

At no point do you need a loop - that is effectively provided by the timer.

If processing your item may atek a long while, then you may need to start using a BackgroundWorker or similar, but that''s the advanced class! :laugh:


这篇关于一次取一个列表框项目然后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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