Hi,
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.

Recommended Answers

All 2 Replies

Check this out:

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

	comboBox1.Items.Add("--- select ---")
	list = New List(Of String)() From { _
		"item 1", _
		"item 2" _
	}
	For Each item As String In list
		comboBox1.Items.Add(item)
	Next
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

Hi,
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
        Randomize()
        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.Items.Add(item)
        Next
        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.Items.Add(item)
        Next
        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??

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.