Back to Devexpress

How to: Access an item located at a specific point

windowsforms-7831-controls-and-libraries-form-layout-managers-layout-and-data-layout-controls-examples-how-to-access-an-item-located-at-a-specific-point.md

latest2.4 KB
Original Source

How to: Access an item located at a specific point

  • Nov 13, 2018
  • 2 minutes to read

This example shows how to focus a control within a LayoutControl when a corresponding label is clicked. In the MouseDown event handler, the CalcHitInfo method is used to access a layout item at the current mouse position. If the layout item has a control, the control is focused.

csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraLayout;
using DevExpress.XtraLayout.HitInfo;

namespace LayoutControl_CalcHitInfo {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void layoutControl1_MouseDown(object sender, MouseEventArgs e) {
            LayoutControl lc = sender as LayoutControl;
            BaseLayoutItemHitInfo hi = lc.CalcHitInfo(e.Location);
            LayoutControlItem currentItem = hi.Item as LayoutControlItem;
            if (currentItem == null || hi.HitType != LayoutItemHitTest.TextArea) return;
            if(currentItem.Control != null)
                currentItem.Control.Focus();
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraLayout
Imports DevExpress.XtraLayout.HitInfo

Namespace LayoutControl_CalcHitInfo
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub layoutControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles layoutControl1.MouseDown
            Dim lc As LayoutControl = TryCast(sender, LayoutControl)
            Dim hi As BaseLayoutItemHitInfo = lc.CalcHitInfo(e.Location)
            Dim currentItem As LayoutControlItem = TryCast(hi.Item, LayoutControlItem)
            If currentItem Is Nothing OrElse hi.HitType <> LayoutItemHitTest.TextArea Then
                Return
            End If
            If currentItem.Control IsNot Nothing Then
                currentItem.Control.Focus()
            End If
        End Sub
    End Class
End Namespace