1.11M Members

Hej i am trying to create a simple game as homework from my school. now i just stuck on one stage.

see the picture on this link how it looks like.

i have created a module here is the code for module file

``````Module Module1
Public Sub bknapper()

'create button on fly
Dim intLoop As Integer
Dim rand As New Random
'genrate array that will get the random values from c variable and
'will use to compare if the value is unique
Dim kontroll(49) As Integer
' Off sets
Dim y As Integer = 65
Dim x As Integer = 0
Dim a, b, c, d, e1 As Integer
a = 1
b = 10
' Loop through buttons
For intLoop = 0 To 24

' Create button and set its width
Dim newbutton As New CheckBox
newbutton.Appearance = Appearance.Button
newbutton.TextAlign = ContentAlignment.MiddleCenter
newbutton.Width = 40
newbutton.Height = 40

' If we have hit 4 buttons, adjust the y value by adding 10 onto the controls height
' Reset x back to 0
If intLoop Mod 5 = 0 Then
' This gives us a 10 pixel buffer below each row of buttons
y += newbutton.Height + 1
x = 0
End If

' give random value rage
'first line will show random values between 1 and 10
'second line will show random values between 10 and 20
'and so on
Select Case intLoop
Case 5
a += 10
b += 10
Case 10

a += 10
b += 10
Case 15

a += 10
b += 10
Case 20

a += 10
b += 10
Case 25

a += 10
b += 10

End Select
'genrate random values and save that value in variable c
c = rand.Next(a, b)
newbutton.Text = c

'check if the randomize value is unique if not give that another random value and check it again
For d = 0 To 49
If kontroll(d) = c Then
c = rand.Next(a, b)
newbutton.Text = c
Else
Dim d1 As Integer
For d1 = 0 To 49
If kontroll(d1) = c Then
c = rand.Next(a, b)
newbutton.Text = c
End If
Next

End If
Next d
'assgin all random c variable values to an array of integer "kontroll"
kontroll(intLoop) = c
newbutton.Top = y
' This gives us a 5 pixel right buffer between buttons
newbutton.Left = 350 + (x * (newbutton.Width + 1))
x += 1
Next
End Sub
End Module``````

and what i want when i press the button on form1 it will reload and show new random numbers on that buttons and code for form 1 is here

``````Public Class Form1

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

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Form2.Show()
End Sub

Private Sub AvslutaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AvslutaToolStripMenuItem.Click
End
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End Sub
End Class``````

can any one help me.. or tell me the logic what i am doing wrong.

>How to reload method form module.

I guess you want to call a method. Isn't it?

yes but what is the syntax and what i am doing wrong. i just want to recreate buttons on fly and asign them again the new random values.
bknapp() to recall this method/sub procedure on button_2 click event.
but nothing happens

You have to call that method in button click event handler.

``````Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WindowState = FormWindowState.Maximized
Randomize()
bknapper()
End Sub``````

i am still getting the same problem..
is that another way that i can solv this issue?

>i am still getting the same problem..is that another way that i can solv this issue?

You have to remove all checkboxes from the controls collection before adding new.

``````Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each ch In Me.Controls
If TypeOf ch Is CheckBox Then
Me.Controls.Remove(ch)
End If
Next

Randomize()
bknapper()

End Sub``````

Thanks for effots i fixed the problem with the other way
what i did that i just created the buttons on fly on form load and on button_click event i genrated the random numbers and assign these numbers as text to the buttons.

Question Answered as of 4 Years Ago by __avd
You