Hi,
I have created a custom combo box control that autocompletes entries with
the code as shown below:
However I wish to override the Leave and KeyUp events of the combobox, so
that I can change them so that they call autocompletecombo_keyup and
autocompleteCombo_Leave.
It won't let me override the leave and combo events because the are in the
base
control object not in the combobox object. Is there anyway that I can get
the new Leave and KeyUp events to be embedded within my custom control rather
than the user having to add code to these events to call the new methods.
Thanx in advance
Robert
public partial class AutoCompleteCbo : ComboBox
{
public AutoCompleteCbo()
{
InitializeComponent();
}
public void AutoCompleteCombo_Leave(ComboBox cbo)
{
int iFoundIndex;
iFoundIndex = cbo.FindStringExact(cbo.Text);
cbo.SelectedIndex = iFoundIndex;
}
public void AutoCompleteCombo_KeyUp(ComboBox cbo, KeyEventArgs e)
{
string sTypedText;
int iFoundIndex;
object oFoundItem;
string sFoundText;
string sAppendText;
//Allow select keys without Autocompleting
switch (e.KeyCode)
{
case Keys.Back:
case Keys.Left:
case Keys.Right:
case Keys.Up:
case Keys.Delete:
case Keys.Down:
return;
}
//Get the Typed Text and Find it in the list
sTypedText = cbo.Text;
iFoundIndex = cbo.FindString(sTypedText);
//If we found the Typed Text in the list then Autocomplete
if (iFoundIndex >= 0)
{
//'Get the Item from the list (Return Type depends if
Datasource was bound
// ' or List Created)
oFoundItem = cbo.Items[iFoundIndex];
//'Use the ListControl.GetItemText to resolve the Name in
case the Combo
//' was Data bound
sFoundText = cbo.GetItemText(oFoundItem);
//'Append then found text to the typed text to preserve case
sAppendText = sFoundText.Substring(sTypedText.Length);
cbo.Text = sTypedText + sAppendText;
//'Select the Appended Text
cbo.SelectionStart = sTypedText.Length;
cbo.SelectionLength = sAppendText.Length;
}
}
}
Jack Jackson - 01 Jul 2008 00:00 GMT
>Hi,
> I have created a custom combo box control that autocompletes entries with
[quoted text clipped - 12 lines]
>Thanx in advance
>Robert
Override the OnLeave and OnKeyup methods.
Most .NET classes that raise events have protected methods called
Onxxx whose sole purpose is to raise the event.