I am trying to use AutoComplete feature of textboxes in my winforms application.
The textbox is used to send some GPIB/SCPI commands to an instrument. So before I get the idea of autocomplete it was working fine, but with autocomplete, the KeyPress event is no more working.
The code I wrote is this:
//AutoComplete collection
AutoCompleteStringCollection _commandHistory = new AutoCompleteStringCollection();
public Form1() //Constructor
{
InitializeComponent();
_commandHistory.AddRange(new string[3]{"*IDN?", "*RST", ":READ?"});
tbCommandLine.AutoCompleteSource = AutoCompleteSource.CustomSource;
tbCommandLine.AutoCompleteCustomSource = _commandHistory;
tbCommandLine.AutoCompleteMode = AutoCompleteMode.Append;
}
And here is what I defined for the KeyPress event
private void tbCommandLine_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
SendCommand(tbCommandLine.Text.Trim()); //Send Command
_commandHistory.Add(tbCommandLine.Text.Trim()); //Store command in autocomple source
tbCommandLine.Text = String.Empty; //clear textbox
}
}
After I added the AutoComplete, if I press enter or type something that is in the source of autocomple, the text in the textbox will get hihglighted and the enter key will not do anything.
Solved by changing
KeyPresstoKeyDown