js06

I am creating a small learning game project.

I have a grid of 169 labels. I want to randomly select a label and then popup a question based on the selection.

What i was thinking of doing was to assign a number to each of the labels

Put the number range in a random statement. (1 to 170)

Then based on the random number selection it would select the proper label.

Would this be this easiest way to go about it or is there a way to directly select a random label control If you can randomly select a label, then how do you "get" the one that is selected (active label or control) Focus

I think i know how to do it, i don't know how to put code together for it. If someone would be able to tell me the best way with a little code help i would be grateful.

Recap

select random label

highlight selected label with different color

depending on which label is selected ask a specific question

Thanks for your help




Re: Visual Basic Express Edition Random selection of controls?

Tall Dude

JohnWein has some neat code at

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1446070&SiteID=1

It uses 121 'buttons'.

It can be modified to handle a 'button' click.

Public Class BoxDown

Public Event BxDown(ByVal x As Integer)

Public Sub New()

'MsgBox("Class created")

RaiseEvent BxDown(Form1.currentVal)

End Sub

End Class

Option Strict Off

Public Class Form1

' This string was in the original code

Dim strChars As String = _

"`~€ ¡­ ¡®¡¯""?¨C¡ª !¡é¡ê £¤|¡ì¨Eca -R¡¥¡ã¡À23¡¯¦Ì ¡¤©o1o AAAAAAACEEEEIIIIDNOOOOO¡ÁOUUUUYTsaaaaaaaceeeeiiiienooooo¡Âouuuuyty"

Dim StrArray() As Char

Dim NumToDisp As New List(Of String)

Dim S As Int16 = 35 'CellSize

Dim GrpBmp As Bitmap

Dim GrpBmpRect As Rectangle

Dim MDChar As Int16

Dim MDRegion As Region

Public currentVal As Integer = 0

' Dim obj As BoxDown

Public WithEvents bd As New BoxDown

Public Event MyMouseEvent(ByVal Status As String)

Public Sub SomeMouseDown(ByVal z As String)

RaiseEvent MyMouseEvent(z)

End Sub

Public Sub msg(ByVal x As String) Handles Me.MyMouseEvent

Select Case x

Case Is = "1"

MsgBox("question 1")

Case Is = "2"

MsgBox("question 2")

Case Else

MsgBox("Some key other than 1 or 2 pressed")

End Select

End Sub

Sub LoadArray()

StrArray = strChars.ToCharArray

GrpBmpRect = New Rectangle(35, 35, 11 * S, 11 * S)

GrpBmp = New Bitmap(11 * S, 11 * S)

Dim I As Int16 = strChars.Length - 1

For J As Int16 = 0 To I

DrawRaised(True, J)

DrawCharacters(J)

Next

End Sub

Private Sub DrawCharacters(ByVal K As Byte)

Dim g As Graphics = Graphics.FromImage(GrpBmp)

Dim SI As Int16 = S * (K \ 11)

Dim SJ As Int16 = S * (K Mod 11)

g.DrawString(NumToDisp(K), Font, Brushes.Black, SI + 5, SJ + 5)

End Sub

Private Sub DrawRaised(ByVal Up As Boolean, ByVal K As Byte)

Dim SI As Int16 = S * (K \ 11)

Dim SJ As Int16 = S * (K Mod 11)

Dim S1 As Int16 = S - 1

Dim G As Graphics = Graphics.FromImage(GrpBmp)

Dim Points1 As Point() = {New Point(SI, SJ + S1), _

New Point(SI, SJ), _

New Point(SI + S1, SJ)}

Dim Points2 As Point() = {New Point(SI + S1, SJ), _

New Point(SI + S1, SJ + S1), _

New Point(SI, SJ + S1)}

If Up Then

G.DrawLines(Pens.White, Points1)

G.DrawLines(Pens.Black, Points2)

Else

G.DrawLines(Pens.Black, Points1)

G.DrawLines(Pens.White, Points2)

End If

G.Dispose()

End Sub

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

For x As Integer = 1 To 121

NumToDisp.Add(x.ToString)

Next

Show()

LoadArray()

PictureBox1.Width = GrpBmp.Width

PictureBox1.Height = GrpBmp.Height

End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

Dim I As Int16 = e.X \ 35

Dim J As Int16 = e.Y \ 35

MDChar = 11 * I + J

MDRegion = New Region(New Rectangle(35 * I, 35 * J, 35, 35))

DrawRaised(False, MDChar)

PictureBox1.Invalidate(MDRegion)

'Place your code here to respond to the pressing of a character button.

End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

DrawRaised(True, MDChar)

PictureBox1.Invalidate(MDRegion)

SomeMouseDown((MDChar + 1).ToString)

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

e.Graphics.DrawImage(GrpBmp, 0, 0)

End Sub

End Class






Re: Visual Basic Express Edition Random selection of controls?

js06

Thanks for the post Tall Dude

I found some code that i was able to modify

I think with this code, yours and the link you gave for JohnWein i can put it all together

Thanks again

Dim crandom As Random = New Random

Dim rnumber As Integer = crandom.Next(1, 4)

Dim clabel As Control = CType(Me.Controls("Label"), Label)

For Each clabel In Me.Controls

If clabel.Text = "Label1" Then

clabel.Text = "got it"

Exit For

ElseIf clabel.Text = "Label2" Then

clabel.Text = "found next one"

Exit For

End If

Next






Re: Visual Basic Express Edition Random selection of controls?

spotty

A couple of items with this this code.

What is the intention on the line

Dim clabel As Control = CType(Me.Controls("Label"), Label)

as label is used in the loop condition so the initialization seems rather pointless - and if the form has no controls this would fail. Whereas in the for each loop it is only set with a value if there are controls.

You use the text property - is this correct or should you be using the .name property for control names and does this apply to only specific types of controls (ie. labels / textboxes etc. which have a text property)

You dont appear to have any type checking and are using the text property. This is making an assumption that anything called Label1 or Label2 is some control with a Text property but that is really an assumption.

Also the code is a one shot deal, this means once its been run, it wont run again correctly the second time this is called. As the would be no text property matches as youve already changed the text on each of the controls. If you used the control name to determine the control and then set the text property for the control - this would run the saem if called again. The original behaviour may be correct in some circumstances.

Also this code wont work if the label1 or label2 controls are placed within a containing control such as a panel, tab page etc.

Just comments on the code though.




Re: Visual Basic Express Edition Random selection of controls?

js06

Spotty,

The main purpose for the code above was just a test to get the controls and their properties.

The ctype was just a shortcut to eliminate the following line

If clabel.Name = "Label" & rnumber.ToString Then

You are correct on all the above, i am assuming a lot because i am specifically designing the program to work only with these controls.

And they are all as you said it "one shot deal" controls, once they have been used they won't be used again unless the whole program is restarted and the controls are reset. I'm not going to have the controls placed in another control either.

The further i get into it i am still considering just assigning a value to each of the controls, randomly selecting the values and then calling the control directly by it's name. I was trying to save a bunch of code writing but i think either way i won't be able to eliminate too much.

If it helps to understand what i am doing, it is a multiplication times table. The labels will actually be on top of buttons for a grid effect, i decided not to use a grid or table because i couldn't lay it out the way i wanted.

I was going to develop it with XNA but i have to get a new video card. So decided to use what i have.

Do you think i am going about it the right way In the back of my mind i keep thinking there might be a better way.

Either way, thank you for the input.