Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:

Knowledge Base Article: KB08483

HOWTO:Taking action when right-clicking the WinGrid


The information in this article applies to:
UltraWinGrid (v4.3.20043, 5.1.20051, 5.2.20052, 5.3.20053)
  Article Created: 
12/2/2005

Last Updated:
12/28/2005

Article Type
How To
  
Page Options
Average Rating:
7 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Summary

There are several situations where a developer would want a specific action to be taken on a right-click on the WinGrid, depending on where the user clicked.

This tutorial uses the MouseDown event of the grid, wherein the element clicked is determined and appropriate action taken, depending on which options are selected.

Additional Information

Question: How can I determine which element a user clicked on?

Solution: It is necessary to get the UIElement from the grid based on the coordinates of the mouse cursor, and from here to check for the element in which you are interested, such as a Header.

Step-By-Step Example

This sample project allows the user to select from two actions to take for each of three elements: a cell, a header, and a row selector.

To start, we must get the UIElement found at the mouse position, using the following code:

In C#:

Point mousePoint = new Point(e.X,e.Y);
UIElement element = ((UltraGrid)sender).DisplayLayout.UIElement.ElementFromPoint(mousePoint);

In VB:

Dim mousePoint As Point = New Point(e.X, e.Y)
Dim element As UIElement = CType(sender, UltraGrid).DisplayLayout.UIElement.ElementFromPoint(mousePoint)

From this UIElement, we need to check to see if we can find the specific element that we are interested in. This is accomplished by the GetContext() and GetAncestor() methods of the UIElement itself, where we specify the UIElement type that we want to find.

The following code shows how to check for a header, cell, or row selector, and then show a context menu, select the relevant object, or in the case of a row selector, delete the associated row:

In C#:

UltraGridCell cell = element.GetContext(typeof(UltraGridCell)) as UltraGridCell;

if (cell != null)
{
    if (rdoContext.Checked == true)
    {
        ShowContextMenu(mousePoint);
    }
    else
    {
        cell.Row.Selected = !cell.Row.Selected;
    }
}

HeaderUIElement hElement = element.GetAncestor(typeof(HeaderUIElement)) as HeaderUIElement;

if (hElement != null)
{
    if (rdoContextHeader.Checked == true)
    {
        ShowContextMenu(mousePoint);
    }
    else
    {
        hElement.Header.Selected = !hElement.Header.Selected;
    }
}

RowSelectorUIElement rsElement = element.GetAncestor(typeof(RowSelectorUIElement)) as RowSelectorUIElement;

if (rsElement != null)
{
    if (rdoContextRS.Checked == true)
    {
        ShowContextMenu(mousePoint);
    }
    else
    {
        rsElement.Row.Delete(false);
    }
}

In VB:

Dim cell As UltraGridCell = CType(element.GetContext(GetType(UltraGridCell)), UltraGridCell)

If Not cell Is Nothing Then
If rdoContext.Checked = True Then
                        ShowContextMenu(mousePoint)
            Else
                        cell.Row.Selected = Not cell.Row.Selected
            End If
End If

Dim hElement As HeaderUIElement = CType(element.GetAncestor(GetType(HeaderUIElement)), HeaderUIElement)

If Not hElement Is Nothing Then
If rdoContextHeader.Checked = True Then
                ShowContextMenu(mousePoint)
            Else
                        hElement.Header.Selected = Not hElement.Header.Selected
            End If
End If

Dim rsElement As RowSelectorUIElement = CType(element.GetAncestor(GetType(RowSelectorUIElement)), RowSelectorUIElement)

If Not rsElement Is Nothing Then
If rdoContextRS.Checked = True Then
                        ShowContextMenu(mousePoint)
            Else
                        rsElement.Row.Delete(False)
            End If
End If

Review:
This project shows how to capture the UIElement where the user clicked and take a specific action depending on what kind of UIElement this object is.

Samples

kb_rightclickactions_cs.zip
 Demonstrates how to take actions when right-clicking on various locations of a WinGrid


kb_rightclickactions_vb.zip
 Demonstrates how to take actions when right-clicking on various locations of a WinGrid



How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)