Solved

Moving the mouse over the whole form

Posted on 2004-10-13
13
709 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:NightBlader
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 

Author Comment

by:NightBlader
ID: 12303133
No one?
0
 
LVL 4

Expert Comment

by:andrewharris
ID: 12304199
Just trying to understand what it is you are doing and want to acheive...give me a bit....

Andrew
0
 
LVL 4

Accepted Solution

by:
andrewharris earned 50 total points
ID: 12304326
OK, you are dynamically adding picture boxes to a form and you want a single method to handle the MouseMove event for all of them...

When you are creating the PictureBox ensure that you add this line (newPictureBox being the new picturebox you just added to the form):

    AddHandler newPictureBox.MouseMove, AddressOf MyPictureBox_MouseMove

Then add the following method in an appropriate place:

    Private Sub MyPictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim myPictureBox As New Windows.Forms.PictureBox

        myPictureBox = sender

        MsgBox("Mouse is moving over " & myPictureBox.Name)

    End Sub

Let me know if I was close:-)

Andrew
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NightBlader
ID: 12308603
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12308696
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
0
 

Author Comment

by:NightBlader
ID: 12309045
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.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
ID: 12309236
Ok.  Coming from VB6, this is probably pretty frustrating since there you could reference a from with that syntax.

You have two options to solve problem.  Modify your AddNewPicture() function so that it also receives a reference to frmMain, or add a variable to your PictureArray class to hold a reference to frmMain so you can use it in AddNewPicture().  

Judging by the code though, it looks like you have already implemented the second option because you have a variable named HostForm to which you are adding the new PictureBox.

With that in mind, this code should correctly add the Handler to the form:

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 HostForm.Bricks_MouseMove
        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

In frmMain, your signature for the event handler would look like this and you can use the sender variable to determine which picturebox fired the event:

    Private Sub Bricks_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

~IM

0
 

Author Comment

by:NightBlader
ID: 12309374
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.
0
 

Author Comment

by:NightBlader
ID: 12309407
Ah,
I also have
BrickArr = New PictureArray(Me)
added to the
#Region " Windows Form Designer generated code "
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
ID: 12312098
Ok.  One mistake on my part:

    Private Sub Bricks_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

Would have to be:

    Public Sub Bricks_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

Also, your declaration of HostForm could not be a Generic Form.  It would have to be of type frmMain:

    Private HostForm As frmMain

    Public Sub New(ByVal host As frmMain)
        HostForm = host
        Me.AddNewPicture(0, 0)
    End Sub

~IM
0
 

Author Comment

by:NightBlader
ID: 12312321
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12312330
Glad we could figure it out.

~M
0
 
LVL 4

Expert Comment

by:andrewharris
ID: 12327223
NightBlader/IM,

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

Andrew
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question