[Gtk-sharp-list] System.InvalidCastException

Paulo Pires paulo.pires@vodafone.pt
Tue, 15 Mar 2005 14:43:37 +0000


Hi list

this seems odd!

I'm still working around my Canvas, and what I couldn't do with
CanvasWidget, I'm doing with CanvasGroup. So, I've created an object,
wdgSemaphore, which derives from Gnome.CanvasGroup. So far, so good.

The problem appears when I handle my Canvas.CanvasEvent with the
following code:
<code>

private void OnCanvasEvent(object o, Gnome.CanvasEventArgs args)
	{
		Gdk.EventButton ev = new Gdk.EventButton (args.Event.Handle);
		
		Globals.LogLn("Type -> " + o.GetType());
		
		//Gnome.CanvasGroup item = (Gnome.CanvasGroup) o;
		wdgSemaphore item = (wdgSemaphore) o;
		
		Globals.LogLn("-> " + ev.Type);
		
		switch (ev.Type)
		{
			case Gdk.EventType.ButtonPress:
				if (ev.Button == 1) {
					remember_x = ev.X;
					remember_y = ev.Y;
				} else if (ev.Button == 3) {
					// destroy item and subsequent semaphore
					WorldToUI.RemoveSemaphore(item);
				}
				break;
				
			case Gdk.EventType.MotionNotify:
				Gdk.ModifierType state = (Gdk.ModifierType) ev.State;
				if ((state & Gdk.ModifierType.Button1Mask) != 0)
				{
					double new_x = ev.X, new_y = ev.Y;
					item.Move (new_x - remember_x, new_y - remember_y);
					remember_x = new_x;
					remember_y = new_y;
				}
				break;
				
			case Gdk.EventType.EnterNotify:
				break;
				
			case Gdk.EventType.LeaveNotify:
				break;
		}
	}

</code>

That Global.LogLn, is outputting the type of o and then the
Gdk.Event.Type, and here it follows:

(...)

Type -> TrafficMan.wdgSemaphore
-> MotionNotify
Type -> TrafficMan.wdgSemaphore
-> MotionNotify
Type -> TrafficMan.wdgSemaphore
-> MotionNotify
Type -> TrafficMan.wdgSemaphore
-> LeaveNotify
Type -> Gnome.CanvasGroup

Unhandled Exception: System.InvalidCastException: Cannot cast from
source type to destination type.

(...)

What the hell is going on so that the type is changed on the Event?!

Hugs
Paulo Pires