Click here to Skip to main content
15,883,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i have created a usercontrol dynamically and added a video as its content.
Now i want to write Eventhandler for the usercontrol for Drag and dropping it.

// creating user control and loadeing video as content
C#
UserControl uc=new UserControl();
                uc.Height=100;uc.Width =100;           
                MediaElement m1=new MediaElement();
                m1.LoadedBehavior=MediaState.Play;			    
	        m1 .Height =100;m1 .Width =100;			   
	       Uri obj1=new Uri (fd1 .FileName);//loaded from open file dialog box
			    m1 .Source =obj1;
			    uc .Content =m1 ;
			    uniformgrid1 .Children .Add (uc);
			    Mainmedia.Source=obj1 ;
			    uc .MouseMove +=new MouseEventHandler(uc_MouseMove);


Drag code:
C#
private void uc_MouseMove(object sender, MouseEventArgs e)
            { 
 //here it is showing error type or namespace uc cannot be found.          
               uc dragableusercontrol=sender as uc;  
               	if(dragableusercontrol!=null && e .LeftButton == MouseButtonState.Pressed )
               	{
               		DragDrop .DoDragDrop(dragableusercontrol ,
               		                     new DropableCanvasDragDropData(dragableusercontrol .Parent as Panel ,
               		                                                    dragableusercontrol ,e.GetPosition (dragableusercontrol )),
               		                     DragDropEffects.Copy);
              	}
            }

how to access dynamically created usercontrol object in event handler.
if it is not dynamically created user control. the drag code is working perfectly.
Any suggestions will be much appreciated.

thanks,
Posted
Updated 7-Jul-11 22:03pm
v2

Please try the following:

C#
private void uc_MouseMove(object sender, MouseEventArgs e)
            { 
                //Error fixed.          
                UserControl dragableusercontrol = sender as UserControl;  
               	if(dragableusercontrol!=null && e .LeftButton == MouseButtonState.Pressed )
               	{
               		DragDrop .DoDragDrop(dragableusercontrol ,
               		                     new DropableCanvasDragDropData(dragableusercontrol .Parent as Panel ,
               		                                                    dragableusercontrol ,e.GetPosition (dragableusercontrol )),
               		                     DragDropEffects.Copy);
              	}
            }

uc is just a reference object of UserControl and you are using it as a Type which is not the case. Hence you have to cast it to UserControl type.
 
Share this answer
 
All event handlers get passed an object called sender. This is the object that created the event. Cast it to the right type, and there you have it. Looks like you know that and you're doing that. There's no reason for a dynamically created control to be any different. How does it not work ? Is sender not of the right type ? You are creating a UserControl, and trying to cast it as uc. Why are they the same thing ? Why not UserControl uc = sender as UserControl ? What is the type of the object in the debugger ?

Reading the error, your compile time problem is that you are using the variable name instead of the type, uc instead of UserControl. I suspect it worked before bc you did not cast the sender, but grabbed a control you knew the name of.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900