AutoComplete ComboBox (not working on second pass)  
Author Message
SylvirCoder





PostPosted: Visual C# General, AutoComplete ComboBox (not working on second pass) Top

Where to begin...

I copied the following code from a helpful site and placed it in the KeyPress event of a ComboBox Control.

private void aComboBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

ComboBox comboBox = (ComboBox)sender;

if(Char.IsControl(e.KeyChar))

return;

string ToFind = comboBox.Text.Substring(0, comboBox.SelectionStart) + e.KeyChar;

int Index = comboBox.FindStringExact(ToFind);

if(Index==-1)

Index = comboBox.FindString(ToFind);

if(Index==-1)

return;

comboBox.SelectedIndex = Index;

comboBox.SelectionStart = ToFind.Length;

comboBox.SelectionLength = comboBox.Text.Length - comboBox.SelectionStart;

e.Handled = true;

}

The first time through, the autopoulate works without any problem. The next step in my app is a button that clears the Text portion of the Combo. After a user goes back into the control to select the next item, they are unable to enter any data in the field (the keypress still activates and runs as expected, but the e.KeyChar never shows up in the comboboxs.text area, while I do see the value when hovering over in the code). They can use the drop down feature to select the next item, and once they have done so, the autopopulate begins to work again.

On occasion, I have been able to statigically place a [comboBoxName].selectedindex = -1 to correct the issue, however that also seems to introduce its own set of issues. To the untrained eye (mine) it seems like the comboBox.SelectedIndex = Index; only works on the first pass.

C# .net 1.1 (can't upgrade to anything else, even if its free) =(



Visual C#13  
 
 
SylvirCoder





PostPosted: Visual C# General, AutoComplete ComboBox (not working on second pass) Top

Resolved the issue.

I placed an new GotFocus event on the control and added the [comboboxName].selectedindex = -1 logic to it.

Thus the cotrol is reset every time someone clicks on it.


 
 
ANAND2082





PostPosted: Visual C# General, AutoComplete ComboBox (not working on second pass) Top

thankyou very much i wrote this code and changed the dropdownstyle to simple its working like the way exactly i wanted a control to work.