Articles in this section
Category / Section

How to get the selected text from a TextBox cell?

2 mins read

In order to get the selected text from a TextBox cell in WinForms Grid Control, the TextBox renderer has to be acquired from the CurrentCell renderer inside the CurrentCellStartEditing event.

Code Snippet

C#

// form()
// Trigger the required event.
gridControl1.CurrentCellStartEditing += gridControl1_CurrentCellStartEditing;
 
void gridControl1_CurrentCellStartEditing(object sender, CancelEventArgs e)
{
    GridCurrentCell cc = this.gridControl1.CurrentCell;
    if (cc.Renderer is GridTextBoxCellRenderer)
    {
       GridTextBoxCellRenderer renderer = cc.Renderer as GridTextBoxCellRenderer;
       renderer.TextBox.MouseUp += TextBox_MouseUp;
    }
}
 
void TextBox_MouseUp(object sender, MouseEventArgs e)
{
    TextBox text = sender as TextBox;
    if (text.SelectionLength > 0)
    {
        MessageBox.Show(text.SelectedText);
    }
}

 

VB

' form()
' Trigger the required event.
Private gridControl1.CurrentCellStartEditing += AddressOf gridControl1_CurrentCellStartEditing
 
Private Sub gridControl1_CurrentCellStartEditing(ByVal sender As Object, ByVal e As CancelEventArgs)
 Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell
 If TypeOf cc.Renderer Is GridTextBoxCellRenderer Then
    Dim renderer As GridTextBoxCellRenderer = TryCast(cc.Renderer, GridTextBoxCellRenderer)
    AddHandler renderer.TextBox.MouseUp, AddressOf TextBox_MouseUp
 End If
End Sub
 
Private Sub TextBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
 Dim text As TextBox = TryCast(sender, TextBox)
 If text.SelectionLength > 0 Then
  MessageBox.Show(text.SelectedText)
 End If
End Sub
 

 

Screenshot

Showing selected text from TextBox in Grid

 

Sample links:

C# SelectedText_CS 

VB SelectedText_VB

Conclusion

 

Hope you enjoyed learning about how to get the selected text from a TextBox cell.

You can refer to our WinForms GridControl’s feature tour page to learn about its other groundbreaking feature representations. You can explore our WinForms GridControl documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms GridControl and other WinForms components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied