I have a combobox called 'cptype' with 2 items and a default item 'select'. I also have a button called 'populate'. How do I get the program to randomly select one of the 2 items.

I also have a button called 'reset'. How do I get the combobox to reset and show 'select' when I click on the button 'Reset'.

Any help will be very much appreciated.

Check this out:

Private r As New Random()
Private list As List(Of String)
Public Sub New()

	comboBox1.Items.Add("--- select ---")
	list = New List(Of String)() From { _
		"item 1", _
		"item 2" _
	For Each item As String In list
End Sub

Private Sub button1_Click(sender As Object, e As EventArgs)
	comboBox1.SelectedItem = list(r.[Next](list.Count))
End Sub

Private Sub button2_Click(sender As Object, e As EventArgs)
	comboBox1.SelectedIndex = 0
End Sub

Here is the code I have written for the 'Populate' Button.

Public Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim pipedia As Double
        pipedia = CDbl(CDbl((0.5 * Rnd()) + 0.0))
        pipediameter.Text = Format(pipedia, "0.000")

I am very new to VB and I'm struggling to understand how to incorporate your example to the existing code above.

Here is what I have tried to do.

Dim cptype As New Random()
        Dim list As List(Of String)
        list = New List(Of String)() From {"Sacrificial Anode CP", "Impressed Current CP (ICCP)"}
        For Each item As String In list
        ComboBox2.SelectedItem = list(cptype.[Next](list.Count))

        Dim anodetype As New Random()
        Dim list1 As List(Of String)
        list1 = New List(Of String)() From {"Bracelet Anode", "Sled Slender Anode"}
        For Each item As String In list1
        ComboBox1.SelectedItem = list1(anodetype.[Next](list1.Count))

However, doing this adds the already existing items numerous times and does not select each of the two items at every 2 iterations.

Any ideas pls??


This article has been dead for over six months. Start a new discussion instead.