Question Panel MousePosition Problem

Su33nf@yahoo.com

New member
Joined
Jun 10, 2008
Messages
3
Programming Experience
1-3
Hey. I have a program that dynamically makes panels inside a main panel called "pnlTest". I want to be able to select the panels inside the main one, but I am at a lost on how to do it.

I was thinking that selecting the child control by its location would be the best way to do this.

VB.NET:
Private Sub pnlTest_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlTest.MouseClick

Dim pnlSelectPanel As Panel

Dim pntPosition as New Point (blXPoint.Text, lblYpoint.Text)

pnlSelectPanel = pnlTest.GetChildAtPoint(pntPosition,
GetChildAtPointSkip.None)

pnlSelectPanel.BorderStyle = BorderStyle.Fixed3D

End Sub
But when there are other objects in the main panel, "pnlTest", it stops getting the mouse position...

VB.NET:
Private Sub pnlTest_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlTest.MouseMove

Dim pntMousePosition As Point

pntMousePosition = pnlTest.PointToClient(MousePosition)

lblXPoint.Text = pntMousePosition.X
lblYpoint.Text = pntMousePosition.Y
It only seems to work when there is nothing inside pnlTest... as so has the mouse goes over a child control it no longer picks up the mouse position.

is there anyway around this?
 
Each control raises its own events, if you added a panel and click it then it is the click event of that panel that is raised and not the click event of another control. When you create the new panels dynamically you can also dynamically attach event handlers for their event with the AddHandler statement. For example you have a Panels_MouseClick sub to handle the clicks for all dynamic panels:
VB.NET:
Dim p As New Panel
AddHandler p.MouseClick, AddressOf Panels_MouseClick
In the Panels_MouseClick sub the 'sender' object is the panel that was clicked. For example:
VB.NET:
Dim p As Panel = DirectCast(sender, Panel)
MsgBox(p.Name & " was the panel that was clicked")
 
Hey, thank you very much. I knew there had to be a way to dynamically create event handlers, but didn’t have any idea on how to do it.

I need help in understanding just one more thing. I’m still pretty new at programming and I don’t understand the Directcast(Sender) or object as expression…
 
Never mind... I did a search and found some examples on the forums here on how to do it and get it working...

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

Thanks. :) I've asked around on different message boards on how to get it to where I could select or click into the other panels and your the first one to give me a way to do it.
 
Back
Top