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

18 posts, 0 answers
  1. raj
    raj avatar
    3 posts
    Member since:
    Aug 2007

    Posted 17 Dec 2007 Link to this post

    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
  2. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 19 Dec 2007 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Haber
    Richard Haber avatar
    6 posts
    Member since:
    Jan 2005

    Posted 31 Dec 2007 Link to this post

    I too would like to know how to do this.  Could the answer please be posted here for us all to see?
  5. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 03 Jan 2008 Link to this post

    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
  6. Alex Peachey
    Alex Peachey avatar
    56 posts
    Member since:
    May 2006

    Posted 13 Jan 2008 Link to this post

    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.

  7. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 15 Jan 2008 Link to this post

    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
  8. Alex Peachey
    Alex Peachey avatar
    56 posts
    Member since:
    May 2006

    Posted 15 Jan 2008 Link to this post

    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.
  9. Juan Pablo
    Juan Pablo avatar
    19 posts
    Member since:
    Oct 2008

    Posted 07 Jan 2009 Link to this post

    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.
  10. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 13 Jan 2009 Link to this post

    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.
  11. Ifdev02
    Ifdev02 avatar
    68 posts
    Member since:
    Jul 2007

    Posted 11 Apr 2010 Link to this post

    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?
  12. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 13 Apr 2010 Link to this post

    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.
  13. Andreas Nebel
    Andreas Nebel avatar
    16 posts
    Member since:
    Jan 2010

    Posted 19 Apr 2010 Link to this post

    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
  14. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 28 Apr 2010 Link to this post

    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.
  15. Andrea
    Andrea avatar
    2 posts
    Member since:
    Jan 2013

    Posted 24 Feb 2013 Link to this post

    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.
  16. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 26 Feb 2013 Link to this post

    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.
  17. Andrea
    Andrea avatar
    2 posts
    Member since:
    Jan 2013

    Posted 26 Feb 2013 Link to this post

    Thank you very much. It work well!
  18. Masi
    Masi avatar
    1 posts
    Member since:
    Apr 2014

    Posted 02 Apr 2014 in reply to Nikolay Link to this post

    Hi
    thanks for all your helps, but with this property, backspace doesn't work, please help me, its force
    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.
     
  19. George
    Admin
    George avatar
    500 posts

    Posted 04 Apr 2014 Link to this post

    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.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready