This is a migrated thread and some comments may be shown as answers.

Selecting text in a TextBox

9 Answers 645 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Deborah
Top achievements
Rank 1
Deborah asked on 21 Apr 2011, 07:45 PM

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!

9 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 27 Apr 2011, 07:53 AM
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
0
Accepted
Deborah
Top achievements
Rank 1
answered on 27 Apr 2011, 04:01 PM

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!

0
Stefan
Telerik team
answered on 02 May 2011, 10:39 AM
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
0
Accepted
Ralf
Top achievements
Rank 1
answered on 12 Jul 2011, 08:34 PM
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

0
Alexey
Top achievements
Rank 1
answered on 09 Apr 2012, 01:12 PM
Thank you greatly! The problem with selection text resolved. This code works great!
0
Ralf
Top achievements
Rank 1
answered on 10 Apr 2012, 07:13 AM
Hello Alexey.
I'm pleased to hear that it works for you.

Greetings,
Ralf
0
NorthGates
Top achievements
Rank 1
answered on 10 Nov 2012, 06:19 AM
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(); }));
}




0
Stefan
Telerik team
answered on 14 Nov 2012, 09:55 AM
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.
0
Joel
Top achievements
Rank 2
answered on 13 Feb 2013, 08:49 PM
Awesome! works great! :) Thanx for sharing
Tags
TextBox
Asked by
Deborah
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Deborah
Top achievements
Rank 1
Ralf
Top achievements
Rank 1
Alexey
Top achievements
Rank 1
NorthGates
Top achievements
Rank 1
Joel
Top achievements
Rank 2
Share this question
or