.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Right click events in Silverlight controls : Tutorial

Posted By:Dhananjay Kumar       Posted Date: January 13, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

This article will give an introduction of mouse right click events in Silverlight controls. Silverlight 4.0 introduces context menu for all the controls.
 

Objective

This article will give an introduction of mouse right click events on Silverlight controls.  Silverlight 4.0 introduces context menu for all the controls.

Right Click Mouse Event is added newly added feature in Silverlight 4.0.

Design

Create a Button on Silverlight page.



Adding Right click events

Now add mouse right click events to button.  Context menu or Right click event could be added to any control in  Silverlight 4.0

There are two Mouse right click events. 
  1. MouseRightButtonDown
  2. MouseRightButtonUp

1.gif

So after adding the events

myButton.MouseRightButtonDown += new MouseButtonEventHandler(mosuedown)
myButton .MouseRightButtonUp  +=new MouseButtonEventHandler(mouseup);

We could handle the event according to our need.

void
mouseup(object sender, MouseButtonEventArgs e)
{
  // Handle the event as of need
}
void mosuedown(object sender, MouseButtonEventArgs e)
{
  // Handle the event as of need
}

While running above code, we will get below output. Once right click on button, default context of Silverlight will come.  See the images below. 

2.gif

On clicking on Silverlight, Silverlight configuration window will get open.  See the image below.

3.gif

Disabling default context of Silverlight
We need to tell Silverlight that context has been handled by making handled as True.

void mosuedown(object sender, MouseButtonEventArgs e)
{
  e.Handled = true; 
}


After enabling handled true the default context of Silverlight will be disabled.  Now if you run the application you won't get default Silverlight context on right click.

Now, adding a message box to handle the events.

Complete code is as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;  
namespace TestingRightClickEvents 

{ 

    public partial class MainPage : UserControl 

    { 

        public MainPage() 

        { 

            InitializeComponent(); 

  

            myButton.MouseRightButtonDown += new MouseButtonEventHandler(mosuedown); 

            myButton .MouseRightButtonUp  +=new MouseButtonEventHandler(mouseup); 

  

        }  

        void mouseup(object sender, MouseButtonEventArgs e) 

        { 

            MessageBox.Show("Right Click of Button is Up ");            

        }  

        void mosuedown(object sender, MouseButtonEventArgs e) 

        {           

            e.Handled = true; 

        } 

    }
}


Output

4.gif

5.gif

Conclusion

In this article, I introduced Context menu or Mouse Right click event handling in Silverlight 4.0 Thanks for reading.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend