Selecting text in a TextBox

10 posts, 2 answers
  1. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 21 Apr 2011 Link to this post

    The users want the TextBox to select its contents when the user clicks into the TextBox.

    I tried this code:

    Private Sub RadTextBox1_Enter(sender As Object, e As System.EventArgs) Handles RadTextBox1.Enter
        Dim tb = TryCast(RadTextBox1.TextBoxElement.TextBoxItem.HostedControl, TextBox)
        tb.SelectAll()
    End Sub

    This works when the user Tabs into the TextBox, but not when the user clicks into the TextBox.

    Is there a way to select the text when the user clicks into the TextBox?

    Thanks!

  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 27 Apr 2011 Link to this post

    Hello Deborah,

    Thank you for writing.

    Tabbing into the control will automatically select the text in it, so you do not need to add additional logic for this. In order to handle the case when the control is clicked you have to subscribe to the Click event of the TextBoxItem. here is a sample of this:
    Public Sub New()
        InitializeComponent()
        AddHandler RadTextBox1.TextBoxElement.TextBoxItem.Click, AddressOf TextBoxItem_Click
    End Sub
     
    Private Sub TextBoxItem_Click(sender As Object, e As EventArgs)
        RadTextBox1.SelectAll()
    End Sub

    I hope this helps. Should you have any other questions, do not hesitate to contact us.
     
    Best wishes,
    Stefan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 27 Apr 2011 Link to this post

    I was not able to make that technique work because the user needs to be able to work with the TextBox as a normal text box. That is to say, they need to be able to select and edit portions of the text. If I hook the click event, then the user cannot select any text to edit it.

    I was trying to use the Enter event so that it would only select all of the text the first time that the user clicked on it. If the user then works with the text (clicking to select a portion of the text to edit), I don't want to reselect the entire text.

    I found that this seemed to work for me:

    Private Sub SelectionTextBox_Enter(sender As Object, e As System.EventArgs) Handles SelectionTextBox.Enter
        Dim tb = TryCast(SelectionTextBox.TextBoxElement.TextBoxItem.HostedControl, TextBox)
        tb.BeginInvoke(New MethodInvoker(Sub() tb.SelectAll()))
    End Sub

    Can you mark this reply as the answer?

    Thanks for replying!

  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 02 May 2011 Link to this post

    Hello Deborah,

    Thank you for writing back.

    I am glad that you have found a solution for your scenario. Alternatively, if someone looks for a different solution, using a boolean variable might also do the trick:
    Public Sub New()
            InitializeComponent()
            AddHandler RadTextBox1.TextBoxElement.TextBoxItem.Click, AddressOf TextBoxItem_Click
        End Sub
     
        Private Sub TextBoxItem_Click(sender As Object, e As EventArgs)
            If IsEntered = True Then
                RadTextBox1.SelectAll()
            End If
            IsEntered = False
        End Sub
     
        Private IsEntered As Boolean = False
        Private Sub RadTextBox1_Enter(sender As Object, e As System.EventArgs) Handles RadTextBox1.Enter
            IsEntered = True
        End Sub

    I have marked your post as answer as well. Should you need anything else, do not hesitate to contact us.

    Kind regards,
    Stefan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Answer
    Ralf
    Ralf avatar
    4 posts
    Member since:
    Oct 2012

    Posted 12 Jul 2011 Link to this post

    Hello Deborath.

    I think you found a solution meanwhile, but here is elegant way to select the whole contents:
    private void RadMaskedEditboxSelectAll(object sender, EventArgs e)
    {
        this.BeginInvoke((MethodInvoker)delegate()
        {
            ((RadMaskedEditBox)sender).SelectAll();
        });
    }

    Point your Enter-Event to this function. This works if the controls receives focus either by "tabbing" to it, or by clicking into it.
    You only have to handle one event and all RadMaskedEditBox controls on your form can use the same function.

    Ralf

  7. Alexey
    Alexey avatar
    1 posts
    Member since:
    Nov 2011

    Posted 09 Apr 2012 Link to this post

    Thank you greatly! The problem with selection text resolved. This code works great!
  8. Ralf
    Ralf avatar
    4 posts
    Member since:
    Oct 2012

    Posted 10 Apr 2012 Link to this post

    Hello Alexey.
    I'm pleased to hear that it works for you.

    Greetings,
    Ralf
  9. NorthGates
    NorthGates avatar
    13 posts
    Member since:
    Oct 2012

    Posted 10 Nov 2012 Link to this post

    For C#, looking at a combination of the code on this thread, for my txtSearch, which is a CommandBarTextBox:

    In my form Load I inserted:
    txtSearch.GotFocus += txtSearch_GotFocus;

    Then the event:
    void txtSearch_GotFocus(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)txtSearch.TextBoxElement.TextBoxItem.HostedControl;
        tb.BeginInvoke(new MethodInvoker(delegate() { tb.SelectAll(); }));
    }




  10. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 14 Nov 2012 Link to this post

    Hi Luc,

    Thank you for sharing your code with the community.
     
    Greetings,
    Stefan
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  11. Joel
    Joel avatar
    3 posts
    Member since:
    Nov 2006

    Posted 13 Feb 2013 Link to this post

    Awesome! works great! :) Thanx for sharing
Back to Top
UI for WinForms is Visual Studio 2017 Ready