Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
3.40/5 (4 votes)
See more:
Hello,

I have a combobox that is populated via a dataset. That's all fine. I have 'AutoCompleteMode' set to 'SuggestAppend' and 'AutoCompleteSource' set to 'ListItems'. That's all fine and works. The problem is, that the user can still enter any old text he wants. I want the current behaviour (suggest/append) with the user being able to type in stuff, but I don't want them to be able to enter anything that ISN'T in the list items.

I figure I could handle one of the 'Focus' events, but is there a better way?????

Cheers,
Brad
Posted
Updated 24-Jan-18 19:57pm
Comments
Gerry Logrosa 23-Jun-11 22:49pm    
I have the same with this. Any solution for this please post if any. thanks

Take a look at ComboBox.TextUpdate
Perhaps this can help
ComboBox.TextUpdate
 
Share this answer
 
I don't have the appropriate docco to hand, but there is a style-type property with a name like "DropDownList" which disables typed entry, but still allows "autocomplete" lookup.
Good luck,
Peter
 
Share this answer
 
Comments
Sandeep Mewara 4-Aug-10 1:29am    
Comment from OP: Thanks for the quick reply Peter, but when you change the style to "DropDownList", the user can no longer type in the entry. This is important (in my case) because the number of entries is many
This is another solution, which is implemented based on Gerry Logrosa's solution.


1. set these properties
AutoCompleteMode = SuggestAppend
AutoCompleteSource = ListItems
DropDownStyle = ComboBoxStyle.DropDown;

2.
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < 32 || e.KeyChar > 126)
{
return;
}
string t = comboBox1.Text;
string typedT = t.Substring(0, comboBox1.SelectionStart);
string newT = typedT + e.KeyChar;

int i = comboBox1.FindString(newT);
if (i == -1)
{
e.Handled = true;
}
}

private void comboBox1_Leave(object sender, EventArgs e)
{
string t = comboBox1.Text;

if (comboBox1.SelectedItem == null)
{
comboBox1.Text = "";
}
}


Hope that this can help you. Please let me know if there is any issue.
 
Share this answer
 
Comments
Member 11615391 12-Aug-15 2:25am    
text type nhi ho rha
Hello,

Please try this solution. It works in my side.

In you combo box

1. set these properties
AutoCompleteMode = SuggestAppend
AutoCompleteSource = None
DropDownStyle = ComboBoxStyle.DropDown;

2. Add KeyPress event handle and write this
C#
public virtual void Cbo_KeyPress(object sender, KeyPressEventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    cb.DroppedDown = true;
    string strFindStr = "";
    if (e.KeyChar == (char)8)
    {
        if (cb.SelectionStart <= 1)
        {
            cb.Text = "";
            return;
        }

        if (cb.SelectionLength == 0)
            strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
        else
            strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
    }
    else
    {
        if (cb.SelectionLength == 0)
            strFindStr = cb.Text + e.KeyChar;
        else
            strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
    }
    int intIdx = -1;
    // Search the string in the ComboBox list.
    intIdx = cb.FindString(strFindStr);
    if (intIdx != -1)
    {
        cb.SelectedText = "";
        cb.SelectedIndex = intIdx;
        cb.SelectionStart = strFindStr.Length;
        cb.SelectionLength = cb.Text.Length;
        e.Handled = true;
    }
    else
        e.Handled = true;
}




Thanks. Hope it helps!
 
Share this answer
 
Comments
CHill60 3-Jun-13 9:31am    
I suspect that nearly 3 years after posting this question that the OP has either solved this problem or moved onto other things
ayushmidha 5-Jul-15 23:06pm    
this works fine but there is little problem with this code while backspace it select text but i want to erase txt ono by one character
thnx in advance
Member 12949179 28-Jul-17 15:21pm    
nice solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900