Link to home
Start Free TrialLog in
Avatar of NightBlader
NightBlader

asked on

Moving the mouse over the whole form

Hello!
Im am making a little game on VB .Net, and i have a problem:
I have used the MouseMove event to move a control on my form with a mouse, but I had to make separate event subs for each control on the form to be able to make the control move when my mouse is over the different controls.
The problem is that I have a large area covered by a picturebox "array" (I'm using the suggestion microsoft made concerning control array replacement in VB .Net) built from about a hundred pictureboxes I create during runtime.
Now when I try to make a mousemove sub for the array it wont work, the control which has to move doesnt move when my mouse is over the array.
The array is created artificially and doesnt have a "base" in the design mode.
This is the header of the array function:
Private Sub PictBoxArray_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseMove

How will I be able to move the control  in any mouse position on the form.

The microsoft solution regarding control arrays:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingcontrolarraysinvisualbasicnetvisualcnet.asp

Thank You.
Avatar of NightBlader
NightBlader

ASKER

No one?
Just trying to understand what it is you are doing and want to acheive...give me a bit....

Andrew
ASKER CERTIFIED SOLUTION
Avatar of andrewharris
andrewharris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.
That would work but the picturebox creation is located in a different class, the function should be located in the main form class.
Now when I try accessing the form with:
AddHandler newPictureBox.MouseMove, AddressOf frmMain.MyPictureBox_MouseMove
the compiler says I have to declare frmMain as an object, and I dont know how to declare it.
Could you please advise?
Thank You.
Avatar of Mike Tomlinson
If you are creating and managing the pictureboxes from inside a class, then you should be adding the handler to the class, not the form.  Perhaps you can show us some of your code so we can make more intelligent suggestions.  =)

It sounds like a coding nightmare though.  Another alternative, instead of trying to hand off mouse move events between different controls much as a cell phone call jumps from transmitting station to station as the caller moves around, is to use a timer and poll the current cursor position on the screen.  But without knowing exactly what you app is doing it is hard to make suggestions.

Could you explain in more detail what your app does and how you want it to behave?

~IM
Ah, ok.
I've already added the handle to the class.
Im not managing the pictureboxes through the class, I only use it for one purpose, creating a picturebox collection when the program starts and using the method in it to add pictureboxes to the collection, put them in place and set their size.
Im new to VB .NET (im using my dad's software, im 16), and I didnt use collections before.
If I was working with VB6 I wouldnt have any problems, I would just create a control array and work with it.
But as VB .NET doesnt support control arrays I have to emulate them through a collection using a class, microsoft made that suggestion in the link above.

At the beggining of the program, after positioning different controls I have a loop to generate the pictureboxes(blocks).
With each loop I call the class method which creates my picturebox and add that picturebox to an array as follows:

For i = 0 To BRICK_ROW_NUM - 1
      For k = 0 To BRICK_COL_NUM - 1
            System.Threading.Thread.Sleep(30)
            BrickArr.AddNewPicture(i, k)
            BrickArr(i * BRICK_COL_NUM + k).BorderStyle = BorderStyle.FixedSingle
            BrickArr(i * BRICK_COL_NUM + k).BackColor = ColorArr(Int(((COLOR_NUM - 1) * Rnd()) + 0))
      Next k
Next i

(I declare BrickArr as PictureArray which is my class name)

The method in the class:

Public Function AddNewPicture(ByVal i As Integer, ByVal k As Integer) As System.Windows.Forms.PictureBox
        Dim picDataShow As New System.Windows.Forms.PictureBox
        Me.List.Add(picDataShow)
        AddHandler picDataShow.MouseMove, AddressOf frmMain.Bricks_MouseMove       'THE HANDLER I ADD
        HostForm.Controls.Add(picDataShow)
        picDataShow.Height = BRICK_HEIGHT / 1
        picDataShow.Width = BRICK_WIDTH / 1
        picDataShow.Top = i * picDataShow.Height + PANEL_HEIGHT
        picDataShow.Left = k * picDataShow.Width + 7
        picDataShow.Tag = Me.Count
        Return picDataShow
End Function

frmMain is also automatically set as the name of the main form class because it's the name of the form

The compiler says:
Reference to a non-shared member requires an object reference.
About: frmMain.Bricks_MouseMove

MSDN says I have to declare frmMain, how do I do that?

Thank You.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, of course, I tried that.
But, it gives the mistake:
'Bricks_MouseMove' is not a member of 'System.Windows.Forms.Form'.
When I try Intellisense on HostForm it just displays a list of form properties and none of the methods in the frmMain class.
I think HostForm is just a kinda "C++ pointer" or reference to the form itself, not the class with the same name.

This is how I declare HostName, the following sub is located in the PictureArray class.

Public Sub New(ByVal host As System.Windows.Forms.Form)
        HostForm = host
        Me.AddNewPicture(0, 0)
End Sub

Thank You.
Ah,
I also have
BrickArr = New PictureArray(Me)
added to the
#Region " Windows Form Designer generated code "
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, that works.
The mistake with public I figured out myself, but I had HostForm declared as System.Windows.Forms.Form.
Thank you both very much.
I have actually finished my game but had this annoyence I couldnt move my mouse over half of the screen.
I split the points 40:85.
Glad we could figure it out.

~M
NightBlader/IM,

Sorry, been off the air for a while:-/  apologies.

Andrew