如何使用For…Next语句在ComboBox中显示项目. [英] How to display items in a ComboBox with a For…Next statement.

查看:76
本文介绍了如何使用For…Next语句在ComboBox中显示项目.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看此For…Next代码,并告诉我它有什么问题,因为这些项目不想在运行时显示在ComboBox中.

Please take a look at this For…Next code and tell me what''s wrong with it because the items doesn''t want to show in the ComboBox at Runtime.

Dim verses As Integer

        For verses = 1 To 50 Step 1
            chaptersComboBox.Items.Add(verses.ToString)
        Next

推荐答案

您显示的代码没有问题,它将列出组合框下拉列表中的数字1到50.

也可以这样写;
There is nothing wrong with the code you have shown, it will list the numbers 1 to 50 in the combobox dropdown.

It could also be written like this;
For verses as Integer = 1 to 50
    chaptersComboBox.Items.Add(verses.toString)
Next



该变量被声明为for 语句的一部分,x to Y操作的默认step 1,因此无需指定.

也许还有其他正在执行的事情正在执行此代码后清除组合框项目集合?



The variable is declared as part of the for statement, the default step on a x to Y operation is 1, so doesn''t need to be specificed.

Maybe there is something else executing that is clearing the combobox items collection after this code has executed?


我会改用foreach.
这样,您无需担心计数.

I would use foreach instead.
This way you don''t need to worry about a count.

For Each item As string chaptersComboBox.Items
    result = item
Next


我刚刚尝试了一下,效果很好.
一个空的组合框,按一下按钮,它会按预期填充数字1到50.


唯一没有发生的事情是下拉菜单中什么也没有出现,直到我进入控件并按下下拉箭头.只需使用ChaptersComboBox.SelectedIndex属性即可解决.
I just tried it and it works fine.
An empty combobox, press the button, it fills with the numbers 1 to 50 as expected.


The only thing which didn''t happen was that nothing appeared in the drop down part until I entered the control and pressed the drop arrow. Easily solved with the chaptersComboBox.SelectedIndex property.


这篇关于如何使用For…Next语句在ComboBox中显示项目.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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