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
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
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!
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
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
I'm pleased to hear that it works for you.
Greetings,
Ralf
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(); }));
}
Thank you for sharing your code with the community.
Greetings,
Stefan
the Telerik team