We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the selected text from a TextBox cell?

Platform: WinForms |
Control: GridControl |
Published Date: May 13, 2016 |
Last Revised Date: May 29, 2019

In order to get the selected text from a TextBox cell, 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

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon