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

How to restrict user from entering text which is not in a display member of combobox items

17 Answers 948 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
raj
Top achievements
Rank 1
raj asked on 17 Dec 2007, 06:15 PM
How to restrict user from entering text which is not in a display member of combobox items.

scenario:

i have set the following properties:

AutoCompleteMode = SuggestAppend
DropdownStyle = DropDown

now combobox is editable, when i enter text it's suggesting few items which starts with the text which i have entered.
but i don't want to let my user to enter text other than the text that control suggests.

how can i restrict? Is there any direct property or have to write any code?

Thanks,
Rajesh

17 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 19 Dec 2007, 09:04 AM
Hi Rajesh,

Please find the answer to your question in the support ticket you have opened.

Should you have additional questions, do not hesitate to contact us.

Regards,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Richard Haber
Top achievements
Rank 1
answered on 01 Jan 2008, 12:38 AM
I too would like to know how to do this.  Could the answer please be posted here for us all to see?
0
Nikolay
Telerik team
answered on 03 Jan 2008, 08:52 AM
Hello Richard Haber,

Currently, RadComboBox does not support the exact scenario described. This feature will be included in one of our future releases.

Still, you have the following options to achieve similar results:
  1. The first one is to set the AutoCompleteMode to SuggestAppend and DropDownStyle to DropDown. Then you should subscribe to the TextChanging event handler of the RadComboBoxElement:

    RadComboBoxElement comboBoxElement = ((RadComboBoxElement)radComboBox1.RootElement.Children[0]);           
    comboBoxElement.TextChanging += new TextChangingEventHandler(comboBoxElement_TextChanging); 

    The event handler should look like this:
    void comboBoxElement_TextChanging(object sender, TextChangingEventArgs e)           
    {           
         if (radComboBox1.FindItem(e.NewValue) == null)           
         {           
              e.Cancel = true;           
         }           
    }    
     

    The result is: start typing so that the autocomplete finds a match in the combo items collection, then continue typing something that differs from that item and press Enter - you will end up with the item found by the autocomplete.
  2. The second workaround was proposed by a customer. It works fine, except for the case when you use the Backspace or Key arrows, which will lead to undesired results. Just put the following code in the KeyUp event handler:

    private void radComboBox1_KeyUp(object sender, KeyEventArgs e)     
    {     
        RadComboBox rcb = new RadComboBox();     
        rcb = (RadComboBox)sender;     
             
        if (rcb.Text != "")     
        {     
            RadComboBoxItem citem = new RadComboBoxItem();     
            citem = rcb.FindItem(rcb.Text);     
        
            if (citem == null)     
            {     
                if (rcb.Text.Length == 1)     
                {     
                    rcb.Text = "";     
                }     
                else    
                {     
                    rcb.Text = rcb.Text.Substring(0, rcb.Text.Length - 1);     
                    rcb.SelectionStart = rcb.Text.Length;     
                }     
            }     
         }     
    }    
     
If you have additional questions, do not hesitate to contact us.

Sincerely yours,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Alex Peachey
Top achievements
Rank 1
answered on 13 Jan 2008, 07:47 PM
I'd like to do this as well but I find that it does not look like the RadComboBox control has a TextChanging event. I think that would be the cleanest solution barring built-in functionality, but it is not viable without that cancel-able event.

I'll go ahead and try the customer submitted solution but I don't like the fact that it breaks down with the use of special keys.

0
Nikolay
Telerik team
answered on 15 Jan 2008, 02:05 PM
Hello Alex Peachey,

The TextChanging event exists and could be applied to RadComboBox, but through a RadComboBoxElement instance. For a reference, see my previous response.

As I mentioned before, even with the TextChanging event, you will not be able to achieve the exact scenario you're describing. We will implement this feature for one of our future releases.

If we could be of your further assistance, do not hesitate to contact us.

All the best,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Alex Peachey
Top achievements
Rank 1
answered on 15 Jan 2008, 03:07 PM
Thank you Nikolay. I should have read that better. I got excited as I saw what appeared to be the solution to something I was trying to do and basically just read the post and didn't really look at your provided code. I ran off to go handle the TextChanging event and couldn't find it.

Now that I actually re-read the answer and the provided code I see exactly what you are doing and I believe that will work for what I need until the functionality is built in.
0
Juan Pablo
Top achievements
Rank 1
answered on 07 Jan 2009, 05:08 PM
Hello, I'm using the first solution you propose and works fine.
I did a little change and the event handler looks like this:

        void cbNameElement_TextChanging(object sender, Telerik.WinControls.TextChangingEventArgs e)
        {
            if (!e.NewValue.Equals(string.Empty))
            {
                if (cbName.FindItem(e.NewValue) == null)
                {
                    this.cbName.Text = string.Empty;
                }
            }
        }

What I accomplished with this is that if the user writes something that is not in the list the text of the combobox is cleared.

Now I have a problem. You clear the text, then you hit ENTER, and you write something that is not on the list, that text isn't cleared.

Please help me with this.

Thanks in advance.
0
Nikolay
Telerik team
answered on 13 Jan 2009, 09:51 AM
Hello Juan Pablo,

Thank you for sharing your solution with us.

Indeed, this is the current behavior of RadComboBox in the regarded scenario. In order to achieve your goals, you should clear the text in TextChanged, but not in TextChanging event handler. I am attaching a sample project to demonstrate how this should be done. To test the example, please first type "Ar" for Argentina, then press the Q key to clear the field, then Enter and finally the Z key to test whether the field will be cleared again.

I hope this helps. If you have additional questions, feel free to contact me.

Greetings,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ifdev02
Top achievements
Rank 1
answered on 11 Apr 2010, 05:25 AM
Not sure how long this post was but the winform still doesnt have the custom text disable mode for combobox which is quite standard for this type of control.   ...
What could be the replacement control if i want to use simple selector box, that shown selected item?
0
Nikolay
Telerik team
answered on 13 Apr 2010, 04:54 PM
Hi Ifdev02,

If you regards the general user ability to type text, RadComboBox supports a mode where the user is not allowed to type in the editor part. For additional information, please refer to the following article: DropDownStyle.

This forum thread concerns a custom scenario where the user is able to type only the text which is a part of the an existing RadComboBox item.

I hope that this information is helpful. If you have additional questions, feel free to contact me.

Best wishes,
Nikolay
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
n/a
Top achievements
Rank 1
answered on 19 Apr 2010, 04:19 PM
Hi,

I was searching for this Solution, but it don't work within my VB.net Project:

Dim MyComboBoxElement As RadComboBoxElement     
    
MyComboBoxElement = cboHolderManufacturer.RootElement.Children(0)        
       
AddHandler MyComboBoxElement.TextChanging, AddressOf ComboBoxTextChangingEventHandler       
 

Private Sub ComboBoxTextChangingEventHandler(ByVal sender As ObjectByVal e As TextChangingEventArgs)  
    If sender.FindItem(e.NewValue) Is Nothing Then 
        e.Cancel = True 
    End If 
 
End Sub 

The Event will be fired, but e.cancel do nothing.

Thank you!
Andy
0
Nikolay
Telerik team
answered on 28 Apr 2010, 05:06 PM
Hi Andreas Nebel,

Currently, we have an issue with canceling the TextChanging event of RadComboBoxElement. Until the issue is addressed, please subscribe to the TextChanging event of MyComboBoxElement.TextBoxElement.TextBoxItem.

If you have additional questions, feel free to contact me.

All the best,
Nikolay
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
Andrea
Top achievements
Rank 1
answered on 24 Feb 2013, 09:55 PM
And now with Q3 2012 SP1 how i can do the same thing? With Dropdownlist, using Autocompletemode = SuggestAppened and Dropdownstyle = Dropdown. I want do the same that i can see in the Telerik Demo Application (DropDown & List > DropDownList > Auto complete example).
Thanks for reply.

EDIT
I can get almost the same effect with: 
Private Sub RadDropDownList1_KeyUp(sender As Object, e As KeyEventArgs) HandlesRadDropDownList1.KeyUp
        If RadDropDownList1.FindStringExact(RadDropDownList1.Text.Trim) = -1 Then
            RadDropDownList1.Text = RadDropDownList1.Text.Trim.Remove(RadDropDownList1.Text.Length - 1)
            RadDropDownList1.SelectionStart = RadDropDownList1.Text.Length
        End If
End Sub
But in your example the user can't enter a char different from the list of items.
0
Nikolay
Telerik team
answered on 26 Feb 2013, 05:33 PM
Hi Andrea,

In order to achieve the behavior of the example, you should set the LimitToList of the AutoCompleteAppend to true:

this.radComboDemo.DropDownListElement.AutoCompleteAppend.LimitToList = true;

I hope this helps.

Greetings,
Nikolay
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
Andrea
Top achievements
Rank 1
answered on 26 Feb 2013, 09:34 PM
Thank you very much. It work well!
0
Masi
Top achievements
Rank 1
answered on 02 Apr 2014, 11:14 AM
Hi
thanks for all your helps, but with this property, backspace doesn't work, please help me, its force
[quote]Nikolay said:Hi Andrea,

In order to achieve the behavior of the example, you should set the LimitToList of the AutoCompleteAppend to true:

this.radComboDemo.DropDownListElement.AutoCompleteAppend.LimitToList = true;

I hope this helps.

 

Greetings,
Nikolay
the Telerik team
 
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
 
[/quote]
0
George
Telerik team
answered on 04 Apr 2014, 08:24 AM
Hello Masume,

Thank you for contacting us.

What you are talking about is an issue in RadDropDownList. This issue has been resolved in our latest release. You can either upgrade to the latest version or use the following workaround: subscribe to the KeyUp event of RadDropDownList ‚Äčand use the following code:
void list_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back)
    {
        if (!string.IsNullOrEmpty(this.list.Text))
        {
            int position = this.list.DropDownListElement.TextBox.TextBoxItem.SelectionStart;
            this.list.Text = this.list.Text.Substring(0, this.list.Text.Length - 1);
 
            this.list.DropDownListElement.TextBox.TextBoxItem.SelectionStart = position;
        }
    }
}

Let me know if you have any further questions.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
raj
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Richard Haber
Top achievements
Rank 1
Alex Peachey
Top achievements
Rank 1
Juan Pablo
Top achievements
Rank 1
Ifdev02
Top achievements
Rank 1
n/a
Top achievements
Rank 1
Andrea
Top achievements
Rank 1
Masi
Top achievements
Rank 1
George
Telerik team
Share this question
or