Change text selection when clicking on RadDropDownList

1 Answer 85 Views
DropDownList TextBox
Wojciech
Top achievements
Rank 1
Wojciech asked on 30 Jan 2024, 07:27 AM | edited on 01 Feb 2024, 09:04 AM

Hi,

I have a RadDropDownList control in my WinForms window. Right now, when I click on this control for the first time since opening a window, the whole text inside RadDropDownList text box is selected.  What I would like to happen is to just set cursor at the end of existing text with no text selection.

I tried handling most of the events for RadDropDownList (like GotFocus or Click) with:

    private void OnGotFocus(object sender, EventArgs e)
    {
      SelectionLength = 0;
      SelectionStart = Text.Length;
    }

When I checked during debugging, the selection is properly changed by my method, but in the end the text is selected whole by something else after that.

How can I get no text selection when clicking first time into this RadDropDownList text box?

Any ideas?

 

Thanks

Wojtek

 

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 01 Feb 2024, 12:34 PM

Hello Wojciech,

I appreciate your interest in our RadDropDownList control for WinForms.

The input part of the control is MS TextBox. Our text control is hosting the MS TextBox. You can use its SelectionStart property to make this work. The best place for me will be in the SelectedIndexChanged event handler.

public Form2()
{
    InitializeComponent();
    this.radDropDownList1.SelectedIndexChanged += RadDropDownList1_SelectedIndexChanged;
}

private void RadDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
    this.radDropDownList1.DropDownListElement.TextBox.TextBoxItem.TextBoxControl.SelectionStart = this.radDropDownList1.DropDownListElement.TextBox.TextBoxItem.TextBoxControl.Text.Length;
}

Give this a try and let me know if it works for you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Wojciech
Top achievements
Rank 1
commented on 01 Feb 2024, 01:16 PM

Hello Dinko,

Thank you for your answer.

Unfortunately SelectedIndexChanged is not called when the control is clicked for the first time (when it wasn't focused on before) and RadDropDownList1_SelectedIndexChanged is not hit.

Do you maybe have some other idea what event handler should I try?

Regards,

Wojtek

Dinko | Tech Support Engineer
Telerik team
commented on 06 Feb 2024, 10:00 AM

Hi Wojciech,

I have checked this on my side and indeed clicking on the text box will reset the selection. Upon checking the source code, the selection of the textbox is set in MouseUp event which makes it more difficult to handle. What I came up with is to reset the selection of the text in different events: GotFocus, MouseUp, and Load event of the form. Now in the MouseUp I have used BeginInvoke() method to delay the execution of the code so that it is executed after the internal text selection. To better demonstrate this, you can check the attached project.

I hope that the approach will work for you. I understand that this is not straighforward solution but I think that with some custom code you could implement your requirement.

Wojciech
Top achievements
Rank 1
commented on 07 Feb 2024, 12:31 PM

Hello Dinko,

Thank you for the solution. It fixes my issue :)

Regards

Wojtek

Tags
DropDownList TextBox
Asked by
Wojciech
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or