Windows Develop Bookmark and Share   
 index > Windows Forms General > MouseMove in a form
 

MouseMove in a form

I'm trying to get the location of the mouse pointer so that i can check whether or not it is within an mdi child, i declared the event handler in the form with this code:

private void frmMain_MouseMove(object sender, MouseEventArgs e)
{
bool hit = MDIchild.CheckHit(new Point(e.X,e.Y));
if(hit)
MDIchild.createObject();
}

CheckHit function within MDIchild
public bool CheckHit(point p)
{
if(this.Bounds.Contains(p))
return true;
return false;
}


the problem is that the event never triggers so I don't even know if CheckHit works...
I've tried setting breakpoints but they never pause execution, I've tried a label that shows the current mouse pointer location none works... I did get the code to work when I changed the event to a panel but obviously it doesn't work out side the panel so it does me no good...
Panel1_MouseMove(...){...}


any suggestions, what am I doing something wrong?

James

edit: (this is in C#, posted in wrong forum)
jankowiak  Thursday, September 07, 2006 12:41 AM

If you really don't need the mouse location except for knowing if the mouse is within the MDI child, put your code inside the MouseEnter event handler of the MDI child. if you created MDIchild at runtime by something like:

MDIChild MDIchild = new MDIChild();

you can assign the event handler dynamically immediatly after the initializing of the instance by adding the following line after the line above:

MDIchild.MouseEnter += new EventHandler(MDIchild_MouseEnter);

Of course you will need to add a method MDIchild_MouseEnter(object sender, EventArgs e) to your class containing the code you want to execute (MDIchild.createObject();)

All mouse events of the form do not work when the form is an MDI parent (MouseClick, MouseEnter, MouseLeave, MouseDoubleClick ... etc.) The reason is in the previous post so i guess that makes the post by Chi the primary Answer! However, what i'm saying is that you might not need the MouseMove and the CheckHit() method at all!

A better code, if you create many child forms and you want all of them to call createObject when the mouse enters them, will be to create a general MouseEnter event handler for all your child forms, something like the following (assuming all your child forms are of Type MDIChild):

void MDIChild_MouseEnter(object sender, EventArgs e){

MDIChild temp = (MDIChild)(sender);
temp.createObject();

}

Then, whenever you create a new MDI child, you just assign this general event handler to its MouseEnter event:

MDIChild newInstance = new MDIChild();
newInstance.MouseEnter += new EventHandler(MDIChild_MouseEnter);

This will do for your code i guess!

Hope this helped.

CSharpFreak  Thursday, September 07, 2006 11:00 AM
It seems that your mouse events were received by your child windows. If what you want is something like "click in child1, parent do action1, click in child2, parent do action2", you may handle these mouse event in your childs, and get their parent's handle, use the handle to do something.
Wang Chi  Thursday, September 07, 2006 3:24 AM

If you really don't need the mouse location except for knowing if the mouse is within the MDI child, put your code inside the MouseEnter event handler of the MDI child. if you created MDIchild at runtime by something like:

MDIChild MDIchild = new MDIChild();

you can assign the event handler dynamically immediatly after the initializing of the instance by adding the following line after the line above:

MDIchild.MouseEnter += new EventHandler(MDIchild_MouseEnter);

Of course you will need to add a method MDIchild_MouseEnter(object sender, EventArgs e) to your class containing the code you want to execute (MDIchild.createObject();)

All mouse events of the form do not work when the form is an MDI parent (MouseClick, MouseEnter, MouseLeave, MouseDoubleClick ... etc.) The reason is in the previous post so i guess that makes the post by Chi the primary Answer! However, what i'm saying is that you might not need the MouseMove and the CheckHit() method at all!

A better code, if you create many child forms and you want all of them to call createObject when the mouse enters them, will be to create a general MouseEnter event handler for all your child forms, something like the following (assuming all your child forms are of Type MDIChild):

void MDIChild_MouseEnter(object sender, EventArgs e){

MDIChild temp = (MDIChild)(sender);
temp.createObject();

}

Then, whenever you create a new MDI child, you just assign this general event handler to its MouseEnter event:

MDIChild newInstance = new MDIChild();
newInstance.MouseEnter += new EventHandler(MDIChild_MouseEnter);

This will do for your code i guess!

Hope this helped.

CSharpFreak  Thursday, September 07, 2006 11:00 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGrid cell KeyDown event
• Create a Main subroutine to manage forms in a project
• Are there any Windows Forms Sample Projects ?
• Enter text without click on a cell in dataGridView
• Change Tab control image
• Plotting a graph of a function
• WaitCursor and the TextBox
• How to get displayed rows in datagridview
• SplitterPanel Background
• Updating A Context Menu At Run-Time