How to get the Selected Text from Active Control in VB.NET?

2.1k Views Asked by At

Is there a way to get the selected text or highlighted text only from the Active Control? Active Control doesn't have .SelectedText option, so I used .Text

Example in the image.

I only highlighted "Rus" from the EnhacedTextBox.

ActiveControl.Text contains "Russia".

How do I get the SelectedText "Rus" to be set in Clipboard.SetDataObject() for copying?

enter image description here

Thanks a lot for your opinions and suggestions.

2

There are 2 best solutions below

0
Aousaf Rashid On

Do you mean you want to get selected text of a textbox ? If so,you can use TextBox.SelectedText property.

I am not sure if you are looking for this but if no, then i assume you are generating multiple textboxes from code behind/during design time ? If so, then try the following code to get the active textbox :

Private Sub GetTheText()
If Me.ActiveControl.[GetType]() = GetType(TextBox) Then
    Dim textBox As TextBox = CType(Me.ActiveControl, TextBox)
    Dim mytext = textbox.SelectedText
End If
End Sub

Hope this helps you

2
ianneAB On
m_strGetText = Me.m_udtNavigationController.TemplateKeyAss.PrimaryTask.ActiveControl.Text.ToString()
        Dim trial As EnhancedTextBox = TryCast(Me.m_udtNavigationController.TemplateKeyAss.PrimaryTask.ActiveControl, EnhancedTextBox)
        Dim trial2 As String = trial.SelectedText().ToString()

Solution from @jmcilhinney.

trial2 now contains the Rus selected text. Thanks.