Back to Devexpress

HtmlContentControl.AllowContentSelection Property

windowsforms-devexpress-dot-xtraeditors-dot-htmlcontentcontrol-657e003f.md

latest4.4 KB
Original Source

HtmlContentControl.AllowContentSelection Property

Gets or sets whether users can select the contents of HTML tags.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue(DefaultBoolean.Default)]
[DXCategory("Behavior")]
public DefaultBoolean AllowContentSelection { get; set; }
vb
<DefaultValue(DefaultBoolean.Default)>
<DXCategory("Behavior")>
Public Property AllowContentSelection As DefaultBoolean

Property Value

TypeDefaultDescription
DefaultBooleanDefault

Specifies the initial value of the user-select CSS property for all HTML elements. The DefaultBoolean.True value sets this property to auto , the DefaultBoolean.False and DefaultBoolean.Default values set it to none.

|

Available values:

NameDescriptionReturn Value
True

The value is true.

|

0

| | False |

The value is false.

|

1

| | Default |

The value is specified by a global option or a higher-level object.

|

2

|

Remarks

Enable the AllowContentSelection property to allow users to select text displayed by HTML templates. Note that when users drag the mouse pointer to select a portion of text, button captions, images, and other HTML elements along the way are also selected.

The textual content of the selected region is stored in the SelectedText property. The code below copies this property’s value to the clipboard when a user presses Ctrl+C:

csharp
private void HtmlContentControl2_KeyDown(object sender, KeyEventArgs e) {
    HtmlContentControl hcc = sender as HtmlContentControl;
    if (e.Control && e.KeyCode == Keys.C)
        Clipboard.SetText(hcc.SelectedText);
}
vb
Private Sub HtmlContentControl2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    Dim hcc As HtmlContentControl = TryCast(sender, HtmlContentControl)
    If e.Control AndAlso e.KeyCode = Keys.C Then Clipboard.SetText(hcc.SelectedText)
End Sub

When the selection changes, the SelectedTextChanged event occurs. Handle this event to inspect and modify the SelectedText property value. For instance, you can remove the contents of non-textual elements that were selected along with regular text.

csharp
private void htmlContentControl2_SelectedTextChanged(object sender, EventArgs e) {
    HtmlContentControl hcc = sender as HtmlContentControl;
    string updatedText = hcc.SelectedText.Replace(" OK Cancel ", " ");
}
vb
Private Sub htmlContentControl2_SelectedTextChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim hcc As HtmlContentControl = TryCast(sender, HtmlContentControl)
    Dim updatedText As String = hcc.SelectedText.Replace(" OK Cancel ", " ")
End Sub

You can also use the user-select CSS property to prevent unwanted HTML elements from being selected when the AllowContentSelection property is enabled.

html
<div class="button unselectable">Cancel</div>
css
.unselectable { user-select: none; }

If the AllowContentSelection property is set to DefaultBoolean.Default or DefaultBoolean.False, utilize the user-select CSS property to enable selection for individual elements.

css
.selectable { user-select: all; }
.selectableText { user-select: text; }

See Also

HtmlContentControl Class

HtmlContentControl Members

DevExpress.XtraEditors Namespace