|
-
July 10th, 2007, 06:44 PM
#1
can labels be transparent to mouse events?
Hi, can anyone answer this question, please?
I am placing active panels on a form, that react when the mouse passes over them, but they fail to work if they have a label on them, and the mouse touches the area covered by the label. I should "pass" the event from the label to the panel.
Simple attempts, like this fail:
private void label_MouseMove(object sender, MouseEventArgs e)
{
Control C = ((Label)sender).Parent;
C.MouseMove(C, e); //Error, not allowed!!!
}
I'm new to C#, can anyone suggest a solution?
(Assumption: Labels are created runtime and the event handler of the underlying panel is defined elsewhere)
thanks,
Gyuri
-
July 10th, 2007, 07:00 PM
#2
Re: can labels be transparent to mouse events?
Is there a problem to call the Panel1_Click event when clicking on the label inside the panel?
-
July 10th, 2007, 07:31 PM
#3
Re: can labels be transparent to mouse events?
 Originally Posted by Talikag
Is there a problem to call the Panel1_Click event when clicking on the label inside the panel?
Actually that is what i'm trying here:
C.MouseMove(C, e);
but get:
"Error 1 The event 'System.Windows.Forms.Control.MouseMove' can only appear on the left hand side of += or -= "
The event handler for the panel is defined elsewhere. (Even external plugins can define panels and event handlers so i have no direct access to these functions)
Is there another way to access and call the parent's event handler?
-
July 10th, 2007, 08:18 PM
#4
Re: can labels be transparent to mouse events?
Why not have a declared event for the parent panel's mouse move event and then call this directly from within the label's mousemove:
Code:
private void label1_MouseMove(object sender, MouseEventArgs e)
{
panel1_MouseMove(sender, e);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
xPosLbl.Text = e.X.ToString();
yPosLbl.Text = e.Y.ToString();
}
The panel's mouse_move can be created automatically through the IDE or you can hand-create it yourself by doing this:
Code:
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
-
July 11th, 2007, 01:32 AM
#5
Re: can labels be transparent to mouse events?
Use the same procedure events for panel and label.
Code:
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
this.label1 .MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
Code:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Control c = (Control)sender;
this.Text = "Movin on " + c.Name;
}
-
July 11th, 2007, 10:11 AM
#6
Re: can labels be transparent to mouse events?
And if you need to use the e.X/e.Y or some other data of the EventArgs, you can do that:
Code:
private void MyEventHandler(int x, int y)
{
//do something
//x and y tells the exact location that was clicking on the panel (even if the user clicked on the label).
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
Label c = (Label)sender;
MyEventHandler(c.Location.X + e.X, c.Location.Y + e.Y);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
MyEventHandler(e.X, e.Y);
}
-
July 11th, 2007, 06:54 PM
#7
Re: can labels be transparent to mouse events?
Yeah, you'll need to do a workaround. What you're wanting to do, fundamentally, can't be done because they did not expose public methods for you to call to invoke the event on another control. (The 'button' class has a click invoker, but not panel) The event itself is a public member, but it can only accept event handlers to be notified. The underlying function that invokes it, OnMouseMove, is protected (invisible except to derived classes) so there is no way to get at it within .NET. That would violate the principle of being able to hide class members.
If you really want to do it that way, you could derive a class from Panel, which exposes trigger methods for all of the events you'd like to be able to fire off. Since the derived class could access the protected methods, you could have it work like this:
Code:
public class myPanel : System.Windows.Forms.Panel
{
public void PerformMouseMove(System.MouseEventArgs e)
{
this.OnMouseMove(e);
}
}
That would take some experimentation to get it just right, but it might be worth it to you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|