Natural_orange

I need to rename 15 buttons with a number 1-15 so that none of them have the same number.

How can I randomize the order If someone had code that would do something like randomize the order of number in an array and then apply them to the buttons

any ideas


Re: Visual Basic Express Edition Randomize buttons?

Snixtor

How about a list (in any order) with your numbers and use Random.Next to choose the index. With each number chosen you push that element onto the end of the list and reduce the range of Random.Next by one so as not to choose any elements already chosen.




Re: Visual Basic Express Edition Randomize buttons?

Natural_orange

I've never used a "list" before, any help on what that code might look like

i tried something with an array but it didn't work out....




Re: Visual Basic Express Edition Randomize buttons?

Solitaire

Here ya go:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim shuffle(14) As Integer

Dim randvar As Random = New Random()

Dim mix, temp As Integer

For x As Integer = 0 To 14

shuffle(x) = x + 1

Next x

For x As Integer = 1 To 15

mix = randvar.Next(1, 15)

temp = shuffle(mix)

shuffle(mix) = shuffle(x - 1)

shuffle(x - 1) = temp

Next x

Dim count As Integer = 0

For Each ctl As Control In Controls

If TypeOf ctl Is Button Then

ctl.Text = shuffle(count).ToString

count += 1

End If

Next ctl

End Sub

Note 1: If you have more buttons that do not belong to this group, then you need to identify the set that should be included by adding a Tag property to them. For example, the If TypeOf statement may be changed to :

If TypeOf ctl Is Button and CType(ctl.Tag, Integer) = 1 Then

Note 2: If your set of buttons is inside a container, then you must include the name of the container in the For Each statement. Example:

For Each ctl As Control In Panel1.Controls






Re: Visual Basic Express Edition Randomize buttons?

Snixtor

That's pretty much what I was trying to get at Solitaire. Couldn't provide an example though because I code in C#, not VB.




Re: Visual Basic Express Edition Randomize buttons?

Natural_orange

I think that will work.

All of my buttons are in one container including one that i don't want to scramble so ill use the Tag variable

Thank You Very Much




Re: Visual Basic Express Edition Randomize buttons?

Natural_orange

Code Snippet

Private Sub Randomize()
Dim shuffle(14) As Integer
Dim randvar As Random = New Random()
Dim mix, temp As Integer

For x As Integer = 0 To 14
shuffle(x) = x + 1
Next x

For x As Integer = 1 To 15
mix = randvar.Next(1, 15)
temp = shuffle(mix)
shuffle(mix) = shuffle(x - 1)
shuffle(x - 1) = temp
Next x

Dim counta As Integer = 0

For Each ctl As Control In Controls
If TypeOf ctl Is Button And CType(ctl.Tag, Integer) = 1 Then
ctl.Text = shuffle(counta).ToString
counta += 1
End If

Next ctl

End Sub


That didn't work.... i changed the count to counta since i already have a count in use in the general declrations. It doesnt do anything

i changed all the tags to 1

nevermind.... igot it to work

changed controls to TableLayoutPanel1.controls

THANK YOU