The collection of child elements
Syntax
Example
This code overrides the positionChildElements virtual method of UIElement. This sample shows two different methods for creating and positioning child elements.
| Visual Basic | Copy Code |
|---|
Imports System.Collections
Imports Infragistics.Win
Protected Overrides Sub PositionChildElements()
Dim workRect As Rectangle = Me.RectInsideBorders
Dim ChildElements As ArrayList = Me.ChildElements
Me.childElementsCollection = Nothing
Me.TextElement.Text = Control.Text
Dim textRect As Rectangle = workRect
textRect.Height -= workRect.Height / 4
Me.TextElement.Rect = textRect
Dim buttonElement As ButtonUIElement = GetButtonElement(ChildElements)
If me.Enabled then
buttonElement.Text = "Click Me!"
Else
buttonElement.Text = "Disabled"
End If
Dim buttonRect As Rectangle = workRect
buttonRect.Size = New Size(workRect.Width / 2, workRect.Height / 4)
buttonRect.Location = New Point(workRect.Left, workRect.Bottom - buttonRect.Height)
buttonElement.Rect = buttonRect
Me.ChildElements.Add(Me.TextElement)
Me.ChildElements.Add(buttonElement)
End Sub
Private textUIElement As TextUIElement = Nothing
Public ReadOnly Property TextElement() As Infragistics.Win.TextUIElement
Get
If Me.textUIElement Is Nothing Then
Me.textUIElement = New TextUIElement(Me, String.Empty)
End If
Return Me.textUIElement
End Get
End Property
Private Function GetButtonElement(ByVal childElementsCollection As ArrayList) As ButtonUIElement
Dim btn As ButtonUIElement = ExtractExistingElement(childElementsCollection, GetType(ButtonUIElement), True)
If btn Is Nothing Then
btn = New ButtonUIElement(Me)
End If
Return btn
End Function |
| C# | Copy Code |
|---|
using System.Collections; using Infragistics.Win;
protected override void PositionChildElements()
{
// Since RectInsideBorders is calculated on each get, it is
// best to cache the rect returned rather than make multiple
// calls to RectInsideBorders.
Rectangle workRect = this.RectInsideBorders;
// Store a reference to the ChildElements collection,
// we will be using this in our calls to ExtractExistingElement.
ArrayList childElements = this.ChildElements;
// Null out the ChildElements collection
this.childElementsCollection = null;
// We will be taking two approaches on child element creation.
// The complexity of both methods is rather simple, however
// the appropriateness of the methods is what you should consider when
// making your determination of which to us. And it should be noted that
// you should be careful if you mix and match the methods. As you cannot
// use them on the same type.
// The first method uses a variable of the type TextUIElement.
// The field is private to the class, and is created thru a get
// property. This is most appropriate for single instances of an element
// that will always be visible.
// Here we use the control property to set the
// TextElement's text to the Control's Text property.
this.TextElement.Text = Control.Text;
Rectangle textRect = workRect;
textRect.Height -= workRect.Height / 4;
this.TextElement.Rect = textRect;
// The second method does not use a field. Rather it checks the
// ChildElements collection to see if an element of that type already
// exists and returns it to be re-used. If it does not exist in the collection,
// then we create one. This approach is more appropriate for many elements that may not
// always be displayed. This saves memory by re-using elements, rather than
// creating new ones all the time.
// Create and set position of Button UIElement
// Notice we are not passing in ChildElements, but rather a reference to the
// old collection that was populated in the previous call to
// PositionChildElements.
ButtonUIElement buttonElement = GetButtonElement(childElements);
if(this.Enabled)
buttonElement.Text = "Click Me!";
else
buttonElement.Text = "Disabled";
Rectangle buttonRect = workRect;
buttonRect.Size = new Size (workRect.Width / 2, workRect.Height / 4);
buttonRect.Location = new Point(workRect.Left ,workRect.Bottom - buttonRect.Height);
buttonElement.Rect = buttonRect;
// Add element to ChildElements collection so that it will be
// drawn when control is painted.
this.ChildElements.Add(TextElement);
this.ChildElements.Add(buttonElement);
}
private TextUIElement textUIElement = null;
private TextUIElement TextElement
{
get
{
if(null == this.textUIElement)
this.textUIElement = new TextUIElement(this,string.Empty );
return this.textUIElement;
}
}
private ButtonUIElement GetButtonElement(ArrayList childElementsCollection)
{
// Check ChildElements collection to see if
// there is a button UIElement that can be used.
ButtonUIElement button = ExtractExistingElement(childElementsCollection,
typeof(Infragistics.Win.ButtonUIElement),true)
as Infragistics.Win.ButtonUIElement ;
// If not button will be null in which case we will instantiate a new one.
if(null == button)
button = new ButtonUIElement(this);
return button;
} |
See Also