Simon,
http://groups.google.com/group/micro...2df568c7b5c0c6
I hope this helps, I did it never this way but it should go
Cor
"Simon Harvey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Cor,
>
> Thanks very much for your answer.
>
> Hmm - I'm not quite sure how it would work. I've been trying to do
> something whereby I handle the key press event and detect if the user is
> about to enter a character that would be invalid.
>
> I can stop the user from entering the bogus character, but unfortunately
> something I'm doing seems to prevent the remainder of the text of the
> target item from remaining selected. I'm doing it in the KeyPress event
> and whatever I'm doing to prevent the users keypress from continuing seems
> to be interfering with the normal autocomplete behaviour of the control
> because when I kill the users action, the control immediately deselects
> the users text - which in effect kills the autocomplete behaviour.
>
> Sorry I hope that makes sense. I've copied the code of my control below
> just in case it helps. I'm really close to getting it to work - I just
> need to figure out how to stop my code from interfering with the
> autocomplete part that the control does itself.... :-(
>
>
> Thanks again for your thoughts
>
> S
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using System.Windows.Forms;
>
> namespace AutoCompleteCombo {
>
> public class CustomCombobox: ComboBox {
>
> public CustomCombobox()
> : base() {
> }
>
> protected override void OnKeyPress(KeyPressEventArgs e) {
> string startOfText = null;
> string proposedComboValue = null;
>
> Console.Out.WriteLine(e.KeyChar.ToString());
>
> // If the character entered by the user is not a valid
> number/letter/symbol etc, we don't want to bother with our masking
> // logic as the user is probably just pressing the
> delete/backspace key or something similar
> if (Char.IsLetterOrDigit(e.KeyChar) == false &&
> Char.IsPunctuation(e.KeyChar) == false && Char.IsSymbol(e.KeyChar) ==
> false) {
> base.OnKeyPress(e);
>
> return;
>
> }
>
> # region [-- Determine Proposed Value --]
>
> startOfText = this.Text.Substring(0, this.SelectionStart);
>
> Console.Out.WriteLine("Start Value: " + startOfText);
>
> // If the character that the user has just provided would
> create a string that doesn't exist in our list of items, we need
> // to prevent the key press from continuing
>
> proposedComboValue = startOfText +
> e.KeyChar.ToString().ToLower();
> Console.Out.WriteLine("Proposed Value: " +
> proposedComboValue);
>
> # endregion
>
> # region [-- Determine if Proposed Value Is Valid --]
>
> foreach (object currentListItem in this.Items) {
>
> // If we find at least one item in the list that starts
> with the proposed text, we know that the user should be allowed
> // to continue with what they've done
> if
> (currentListItem.ToString().ToLower().StartsWith(proposedComboValue) ==
> true) {
> base.OnKeyPress(e);
>
> Console.Out.WriteLine("Value is in the list");
>
> return;
>
> }
>
> }
>
> # endregion
>
>
> // If we reach here it means that the user has attempted to
> add a character that would mean that the text entered in the combo
> // no longer represents a valid list item.
> Console.Out.WriteLine("Value is NOT in the list");
>
> // Prevent the KeyPressEvent from continuing by marking the
> event as "handled"
> e.Handled = true;
>
> }
>
> private string FindFirstMatchingItem(string targetText) {
> string result = null;
>
> foreach (object currentItem in this.Items) {
>
> if
> (currentItem.ToString().ToLower().StartsWith(targetText) == true) {
> return currentItem.ToString();
> }
>
> }
>
> return result;
> }
>
> }
>
> }
>
>
> Cor Ligthert[MVP] wrote:
>> Hi Simon,
>>
>> Why not simply check it using the method findstring in the textchanged
>> event
>>
>> http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.findstring(VS.71).aspx
>> Cor
>>
>> "Simon Harvey" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> Hi Guys,
>>>
>>> I have a small problem with the Windows Forms Combobox control.
>>>
>>> I need to allow it to have its Autocomplete behaviour enabled so that
>>> the user can type in items and have the combo suggest values. This works
>>> fine apart from the fact that it allows the user to add entirely new
>>> items to the list - which causes validation problems later on.
>>>
>>> Essentially what I would like is for the combobox to work exactly as it
>>> does now, with the exception that if the user presses a key that would
>>> mean that the new text would no longer represent a valid item in the
>>> list - the control should just totally ignore the key press. It should
>>> be as though the user hadn't pressed the key at all.
>>>
>>> I've created a class that inherits from the Windows Forms combobox class
>>> and that all works fine - I just can't figure out what to do from there.
>>> I've tried handling things like the TextChanged event and KeyDown event
>>> but I haven't been able to figure out how to get the correct effect.
>>>
>>> Does anyone have an example of how to do this? I'm sure it must be very
>>> simple, but I've never really inherited from an existing control.
>>>
>>> Any help would be much appreciated
>>>
>>> Many thanks in advance
>>>
>>> S
>>