Solved

Moving the mouse over the whole form

Posted on 2004-10-13
13
699 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
  • 6
  • 4
  • 3
13 Comments
 

Author Comment

by:NightBlader
Comment Utility
No one?
0
 
LVL 4

Expert Comment

by:andrewharris
Comment Utility
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
Comment Utility
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
 

Author Comment

by:NightBlader
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Ah,
I also have
BrickArr = New PictureArray(Me)
added to the
#Region " Windows Form Designer generated code "
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Glad we could figure it out.

~M
0
 
LVL 4

Expert Comment

by:andrewharris
Comment Utility
NightBlader/IM,

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

Andrew
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with uploading excel file 2 31
Starting to learn JAVA, 7 46
.net VBA word safemode 1 23
Tabcontrol Caption? 1 21
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now