如何使comboBox中的项目值具有特定范围? [英] How to make an item's value in comboBox have a spesific range?

查看:72
本文介绍了如何使comboBox中的项目值具有特定范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使comboBox中的项目值具有特定范围?

例如:

我们将产生5个值,这些值来自random(1,15).每个值的最小2点必须彼此不同.
输出示例(带注释):
3
8-> 8比6高2点
6
12
13-> 13比12高1点,必须防止这一点

这是我的代码:

How to make an item''s value in comboBox have a specific range?

For example:

We will produce 5 values, the values come from random(1,15). And each value must have different min 2 point from other.
Example of output (with comment):
3
8 --> 8 is higher 2 point from 6
6
12
13 --> 13 is higher 1 point from 12, we must prevent this one

Here my code:

Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim random As New Random(), i As Integer = 1
        Do While i < 5
            Dim newItem As Int32 = random.Next(1, 15)
            If Not cmbRnd.Items.Contains(newItem) Then
                cmbRnd.Items.Add(newItem)
                i = i + 1
            End If
        Loop
    End Sub
End Class




对不起,我的英语不好.我是印尼人:)




Sorry my english bad. i''m an indonesian :)

推荐答案

您错过了条件部分.

尝试这样的事情.

You miss the condition part.

Try something like this.

int i = 0;
var random = new Random();
var randomList = new List<int>();

while (i < 5) {
   int x = random.Next(1, 15);
   if (!randomList.Contains(x) || !randomList.Contains((x + 1)) || !randomList.Contains((x - 1))) {
      randomList.Add(i);
      ++i;
   }
}

randomList.ForEach(r => cmbRnd.Items.Add(r.ToString()));




VB




VB

Dim i As Integer = 0
Dim random = New Random()
Dim randomList = New List(Of Integer)()

While i < 5
    Dim x As Integer = random.[Next](1, 15)

    If Not randomList.Contains((x + 1)) OrElse Not randomList.Contains((x - 1)) OrElse Not randomList.Contains(x) Then
        randomList.Add(i)
        i += 1
    End If
End While

randomList.ForEach(Function(r) cmbRnd.Items.Add(r.ToString()))



希望对您的任务有所帮助.

< edit>
重要的是条件
列表应该包含(随机,随机+1或随机-1)



Hope it helps for your assignment.

<edit>
The important part is the condition
List should not contain (random or random+1 or random-1)


您要做的就是增加测试范围:
All you have to do is increase the range of tests:
If Not cmbRnd.Items.Contains(newItem) Then

成为

If Not (cmbRnd.Items.Contains(newItem - 1) OrElse cmbRnd.Items.Contains(newItem) OrElse cmbRnd.Items.Contains(newItem + 1) Then


这篇关于如何使comboBox中的项目值具有特定范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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