8 Replies - 4406 Views - Last Post: 28 June 2011 - 01:48 PM Rate Topic: -----

#1 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

ClickEvents to Form (through control)

Posted 28 June 2011 - 07:28 AM

Hello,
Currently I am trying to make mouse driven game of Reversi/Othello. I've made a UserControl called ucSquare which is used to manage the colour change of the squares on the board. When the form loads I I programmatically generate the board and initial state. Up to now everything seems good. To show I've done at least something here's the function on the form to load the board while the attached picture shows it generated.

                 Public Sub PopulateSquares()

		squareDimension = (SmallestNumber(Me.Width, Me.Height) \ 9)

		Dim pt As Point
		
		For i = 0 To 7
			For j = 0 To 7
				pt = New Point(i * squareDimension, j * squareDimension)
				squares(i, j) = New ucSquare(squareDimension)
				squares(i, j).Location = pt
				squares(i, j).BorderStyle = BorderStyle.FixedSingle

				Me.Controls.Add(squares(i, j))
			Next
		Next

		squares(3, 3).State = ucSquare.enumState.White
		squares(4, 4).State = ucSquare.enumState.White
		squares(3, 4).State = ucSquare.enumState.Black
		squares(4, 3).State = ucSquare.enumState.Black


	End Sub


Issue:
In an attempt to keep the square Control as dumb as possible(only changing colour) I want to manage all the tests for appropriate moves from the form (or another parent control at a later date) but whenever I click on the squares the ClickEvent is picked up by the userControl.

Question:
is there a way force this click to the form (force the event handler)?
or
If already in the clickEvent of the userControl what is the best way(or a way) to allow the form to take over and/or obtain he curser/array position for calculation?

Regards,
Drew

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: ClickEvents to Form (through control)

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 08:06 AM

This is likely because you're go about things wrong.

You should begin by thinking about the mechanics of the game.
By breaking down the game in terms of distinct objects first (the GUI part come later).

I'd first define a class to encapsulate the functionality of a Square.
What are possible states of the square Empty, Black, White.
To me this suggests an Enum would be most suitable

Public Enum Square_States A Integer
 Empty = 0
 White = 1
 Black = 2
End Enum


So back to Square Class
Public Class Square
 Protected mState As Square_State
 Public Sub New()
  mState = Square_State.Empty
 End Sub

 Public Property State() As Square_State
  Get
   Return Me.mState
  End Get
  Set(value As Square_State
   If Enum.IsDefine(Square_State,value) Then
    If Me.mState<>value Then
     Me.mState=Value
     RaiseEvent Me.StateChanged(DirectCast(Me,Object),Me.mState)
    End If   
   End IF
  End Set
 End Property
 Public Event StateChanged(ByVal sender As Object,ByVal state AS Square_State)
End Class


Note: You could also define a new event arg class for the state change.

What is a Othello game board? A collection of squares.
Public Class Board
 Protected _Squares As New List(Of Squares)(_MaxSquares)
 Protected _MaxSquare  As Integer=64
 ' etc
End Class


And so on. Another controls the Manage the game play
Another define what a player is (both human and computer), I think they should implement an interface
Interface IPlayer
 Sub Initialise() ' Called when class is first initialised.
 Sub NewGame(playcolor As ?? )' Called when a new game starts.
 Sub RequestMove( boardstate As ?? ) ' Called when it the player's turn.
 Event MoveMade( ?? As) ' Raised when the player makes a move.
End Interface




By abstracting a way the GUI, you focus more of the functionality, rather making it pretty.

Now the GUI is simple, all it has to do is listen to the events raised by the classes.
Was This Post Helpful? 0
  • +
  • -

#3 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 08:22 AM

Adam,

Interesting enough I was looking at your enumeration tutorial yesterday when i made this code into the ucSquare:
	Public Enum enumState
		Empty = 0
		White = 1
		Black = 2
	End Enum
	Public Property State As enumState
		Get
			Return _State
		End Get
		Set(ByVal value As enumState)
			_State = value
			Call ChangeBackColour()
			
		End Set

omitting the integer portion.

I wasn't really focusing on the GUI at all. Infact I tried to keep it as simple as i could so i could get to validating the clicks that occur. My issue was exposing the square that's clicked on. I would like to 'listen' for events but am not sure how, you suggest preparing an 'event arg class' which I don't know too much about so maybe the answer lay in that.

Drew
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 08:31 AM

You should always check that the actual enum is defined.
Also use events, an object can "listen"/ react to it's own events

This post has been edited by AdamSpeight2008: 28 June 2011 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 09:18 AM

View PostAdamSpeight2008, on 28 June 2011 - 08:31 AM, said:

Also use events, an object can "listen"/ react to it's own events


Say the reaction was to call a sub/function in the parent? is this possible?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 10:58 AM

Why should it care about who is using it.
Who is using reacts to the event it raises.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 11:31 AM

Have a look at Crying Baby Example
Was This Post Helpful? 1
  • +
  • -

#8 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 12:19 PM

Awesome, Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 Brainojack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-March 10

Re: ClickEvents to Form (through control)

Posted 28 June 2011 - 01:48 PM

I've sorted out the issue with passing events off using Dim Withevents & Addhandler. It seems my initial problem was a result of trying to Not being able to use WithEvents with an Array of objects.

The changes I made to my above code are below.
-create dummy object with events
-set the dummy object to a new instance
-stuff that instance into an array.
Now when I click on one of my userControls(squares) the event is first picked up within that control and then on my form.

I hope this doesn't come back down the road and bite me.

 
Dim WithEvents tempSquare As ucSquare	
Public Sub PopulateSquares()

		squareDimension = (SmallestNumber(Me.Width, Me.Height) \ 9)

		Dim pt As Point

		For i = 0 To 7
			For j = 0 To 7

			         tempSquare = New ucSquare(squareDimension)
				AddHandler tempSquare.Click, AddressOf SquareClick

				pt = New Point(i * squareDimension, j * squareDimension)
				squares(i, j) = tempSquare
				squares(i, j).Location = pt
				squares(i, j).BorderStyle = BorderStyle.FixedSingle

				Me.Controls.Add(squares(i, j))
			Next
		Next

		squares(3, 3).State = ucSquare.enumState.White
		squares(4, 4).State = ucSquare.enumState.White
		squares(3, 4).State = ucSquare.enumState.Black
		squares(4, 3).State = ucSquare.enumState.Black


	End Sub


	Sub SquareClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

		Beep()
	End Sub

This post has been edited by Brainojack: 28 June 2011 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1