Edit

Share via


How to: Remove an Adorner from an Element

This example shows how to programmatically remove a specific adorner from a specified UIElement.

Retrieve an adorner on a UIElement

This verbose code example removes the first adorner in the array of adorners returned by GetAdorners. This example happens to retrieve the adorners on a UIElement named myTextBox. If the element specified in the call to GetAdorners has no adorners, null is returned. This code explicitly checks for a null array, and is best suited for applications where a null array is expected to be relatively common.

Adorner[] toRemoveArray = myAdornerLayer.GetAdorners(myTextBox);
Adorner toRemove;
if (toRemoveArray != null)
{
  toRemove = toRemoveArray[0];
  myAdornerLayer.Remove(toRemove);
}
Dim toRemoveArray() As Adorner = myAdornerLayer.GetAdorners(myTextBox)
Dim toRemove As Adorner
If toRemoveArray IsNot Nothing Then
  toRemove = toRemoveArray(0)
  myAdornerLayer.Remove(toRemove)
End If

Example

This condensed code example is functionally equivalent to the verbose example shown above. This code does not explicitly check for a null array, so it is possible that a NullReferenceException exception may be raised. This code is best suited for applications where a null array is expected to be rare.

try { myAdornerLayer.Remove((myAdornerLayer.GetAdorners(myTextBox))[0]); } catch { }
Try
    myAdornerLayer.Remove((myAdornerLayer.GetAdorners(myTextBox))(0))
Catch
End Try

See also