Back to Devexpress

How to: Provide Size Constraints for Control via IXtraResizable Interface

windowsforms-12430-controls-and-libraries-form-layout-managers-layout-and-data-layout-controls-examples-how-to-provide-size-constraints-for-control-via-ixtraresizable-interface.md

latest4.1 KB
Original Source

How to: Provide Size Constraints for Control via IXtraResizable Interface

  • Nov 13, 2018
  • 2 minutes to read

The following example demonstrates a button control (the SimpleButton control’s descendant) implementing the IXtraResizableControl interface. Size constraints retrieved via the IXtraResizableControl.MaxSize and IXtraResizableControl.MinSize properties prevent the button from being resized. The button’s maximum and minimum sizes are set to a value of an inherited SizeableMinSize property. This property return the minimum size required to display the text in its entirety. When the button’s text changes, the SizeableMinSize property is recalculated automatically, and it’s only required to fire the IXtraResizableControl.Changed event to notify subscribers of the changes.

Note that the SimpleButton control already implements the IXtraResizableControl interface. So this example is only given for demonstration purposes.

csharp
using DevExpress.XtraEditors;
using DevExpress.Utils.Controls;

public class MySimpleButton : SimpleButton, IXtraResizableControl {
    bool IXtraResizableControl.IsCaptionVisible { get { return false; } }
    Size IXtraResizableControl.MinSize { get { return SizeableMinSize; } }
    Size IXtraResizableControl.MaxSize { get { return SizeableMinSize; } }
    private static readonly object layoutInfoChanged = new object();      
    event EventHandler IXtraResizableControl.Changed {
        add { Events.AddHandler(layoutInfoChanged, value); }
        remove { Events.RemoveHandler(layoutInfoChanged, value); }
    }        
    protected void RaiseChanged() {
        EventHandler changed = (EventHandler)Events[layoutInfoChanged];
        if (changed == null) return;
        changed(this, EventArgs.Empty);
    }
    public override string Text {
        get {
            return base.Text;
        }
        set {
            base.Text = value;
            RaiseChanged();
        }
    }
}
vb
Imports DevExpress.Utils.Controls
Imports DevExpress.XtraEditors

Public Class MySimpleButton
    Inherits SimpleButton
    Implements IXtraResizableControl
    Private ReadOnly Property IsCaptionVisible() As Boolean _
    Implements IXtraResizableControl.IsCaptionVisible
        Get
            Return False
        End Get
    End Property
    Private ReadOnly Property MinSize() As Size Implements IXtraResizableControl.MinSize
        Get
            Return SizeableMinSize
        End Get
    End Property
    Private ReadOnly Property MaxSize() As Size Implements IXtraResizableControl.MaxSize
        Get
            Return SizeableMinSize
        End Get
    End Property
    Private Shared ReadOnly layoutInfoChanged As Object = New Object()
    Private Custom Event Changed As EventHandler Implements IXtraResizableControl.Changed
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler(layoutInfoChanged, value)
        End AddHandler
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler(layoutInfoChanged, value)
        End RemoveHandler
        RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim handler As EventHandler = CType(Events(layoutInfoChanged), EventHandler)
            If handler Is Nothing Then Return
            handler(sender, e)
        End RaiseEvent
    End Event

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = Value
            RaiseEvent Changed(Me, EventArgs.Empty)
        End Set
    End Property
End Class