sfComboBox - KeyPress event is not working

Hi,

I have to create a Combobox that will accept user input (of course NOT dropdown list mode). However, the input must be limited to words in list items only, not any other character. Means it should stop on word other than present in the item list. Same time it should accept backspace and enter key.

Have tried to achieve the above using the 'KeyPress' event. However, it's not working.

Request you to provide solutions to achieve the same.

8 Replies 1 reply marked as answer

EM Elakkiya Muthukumarasamy Syncfusion Team March 12, 2021 01:45 PM UTC

Hi RAAHUL, 
 
Greetings from Syncfusion. 
 
We have checked your query and we would like to let you know that you can achieve this by using DropDownOpening event. You can avoid DropDown list opening set e.cancel is true. Please refer the below sample and code snippet for the same. 
 
Code example: 
 
sfComboBox2.DropDownOpening += SfComboBox2_DropDownOpening; 
 
sfComboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
 
private void SfComboBox2_DropDownOpening(object sender, Syncfusion.WinForms.ListView.Events.DropDownOpeningEventArgs e) 
{ 
e.Cancel=true; 
} 
 
 
 
Please try it with the above solution and let us know if you need any other further assistance on this. We will be glad to assist you. 
 
Regards, 
Elakkiya 



RA RAAHUL March 12, 2021 05:58 PM UTC

Hi Elakkiya,

Thanks for the update. The solution provided will not suffice my requirement.

For Ex:

ComboBox contains the following list as Datasource.

1. 
Dim usStates As New List(Of String)()
usStates.Add("Alaska")
usStates.Add("Arizona")
usStates.Add("Arkansas")
usStates.Add("California")
usStates.Add("Colorado")
usStates.Add("Connecticut")
usStates.Add("Delaware")
usStates.Add("Florida")
usStates.Add("Georgia")
sfComboBox1.DataSource = usStates

2. DropDownStyle = Dropdown
3. ComboBoxMode = SingleSelection
4. AutoCompleteMode = SuggestAppend
4. AutoCompleteSuggestMode = StartsWith

Now, when the user enters letters other than those mentioned in the List then it should not type in textbox.
For Ex. the user tries to enter the letter "B" or "b", then it should not type. This can be achieved by using the KeyPress event.

Refer attached sample example.

Note: The attached sample contains both conventional ComboBox and sfComboBox. Code is working fine with Microsoft ComboBox. However, not with sfComboBox.



Attachment: combobox_4fb66cfe.zip


EM Elakkiya Muthukumarasamy Syncfusion Team March 15, 2021 02:06 PM UTC

Hi RAAHUL, 

Thanks for your update. 

We are working on this reported query “When the user enters letters other than those mentioned in the List then it should not type in textbox.” and we will update you the further details on 17th March 2021.We appreciate your patience until then. 

Regards, 
Elakkiya 



RA RAAHUL March 16, 2021 03:43 PM UTC

Hi,

Attached wrong sample. 

Please find attached an updated sample.

Regards,
Raahul

Attachment: Combobox_aadcd265.rar


VR Vijayalakshmi Roopkumar Syncfusion Team March 17, 2021 12:55 PM UTC

Hi Raahul 
 
Thank you for your update. 
 
We were able to reproduce the reported behavior with "KeyPress event is not working with SfComboBox"  and logged a defect report on this. The patch for this fix is estimated to be available on 7th April, 2021. 
 
You can track the status of this defect using the following feedback link: 
 
 
Please let us know if you have any other concerns. 
 
Regards, 
Vijayalakshmi VR 



VR Vijayalakshmi Roopkumar Syncfusion Team April 7, 2021 03:25 PM UTC

Thank you for your patience. 
We have fixed the reported behavior “KeyPress event is not working in sfcombobox” and patch for the same can be downloaded from following links.   
  
Recommended approach - exe will perform automatic configuration.   
   
Please find the patch setup from below location:   
   
   
  
  
  
Assembly Version: 19.1.0.54 
   
Installation Directions:   
This patch should replace the files "Syncfusion.SfListView.Winforms” under the following folder.   
$system drive:\ Files\Syncfusion\Essential Studio\$Version # \precompiledassemblies\$Version#\[TargetFramework]   
Eg : $system drive:\Program Files\Syncfusion\Essential Studio\18.4.0.39\precompiledassemblies\18.4.0.39\4.6   
   
To automatically run the Assembly Manager, please check the Run assembly manager checkbox option while installing the patch. If this option is unchecked, the patch will replace the assemblies in precompiled assemblies’ folder only. Then, you must manually copy and paste them to the preferred location or you must run the Syncfusion Assembly Manager application (available from the Syncfusion Dashboard, installed as a shortcut in the Application menu) to re-install assemblies.   
   
   
Please try this patch and let us know if it is resolved at your end. 
  
   
Regards,   
Vijayalakshmi VR 
 


Marked as answer

SZ SZL replied to Vijayalakshmi Roopkumar July 29, 2022 08:01 PM UTC

Hello,

Can you inform me please, what was the solution of this question?

I would like to prevent or cancel the user input (typing) when the item is not in the list.

Thank you!

BR, SZL



GT Gokul Thanudhas Syncfusion Team August 2, 2022 02:46 PM UTC

Hi Raahul,


We have created a sample based on your requirement. Please refer the sample for your reference.
Code Snippets


Private Sub SfComboBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles SfComboBox1.KeyPress

        If e.KeyChar = ControlChars.Back AndAlso e.KeyChar = ControlChars.Back Or Val(SfComboBox1.Text) >= 1 Then

            Return

        End If


        Dim t As String = SfComboBox1.Text

        Dim typed = t.Substring(0, SfComboBox1.Text.Length)

        Dim newtext = typed & e.KeyChar

        Dim view As List(Of Object) = SfComboBox1.DropDownListView.View.Items.ToList()


        Dim filter As List(Of Object) = view.Where(Function(x) x.ToString().StartsWith(newtext, StringComparison.InvariantCultureIgnoreCase)).ToList()

        If filter.Count = 0 Then

            e.Handled = True

        End If

    End Sub


Regards,

Gokul T.


Attachment: Combobox_30faa53c.zip

Loader.
Up arrow icon