Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override or overload AutoComplete Append rule

I have a textbox which sends some commands to an instrument. I added the AutoComplete feature to this textbox and things are going easier now.

What I am thinking to improve this, is to add a possibility that when user enters a command (just a text) while AutoComplete finds a match it also shows a description for that command.

At the moment, I have all the AutoComplete strings in a text file and I load it when the application starts. The textfile contains lines like this:

*IDN?   #Query the instrument for identification
*RST    #Resets the instrument

So what is happening in my application is that because AutoComplete is in SuggestAppend mode, the description of command also gets into the textbox (this will be the same if I only put it in Suggest mode)

What I need to know is how to force the AutoComplete to append the text while its

  1. Does not add any text starting from # char while appending the suggested text
  2. Trim() the text to avoid that spaces you see in the textfile source

UPDATE 1 Ok, I think the only way is to make a new class and inherit from AutoCompleteStringCollection And in this new class, somehow override the reponsible method for returning (appending) suggested text. I really have no idea what should I do:

class MyAutoCompleteCollection : AutoCompleteStringCollection 
{
    //How to override Get function of AutoCompleteStringCollection class?
    //It is not avilable to override :(
}

UPDATE 2 I found out that methods in AutoCompleteStringCollectionare not overridable. I am looking for a way to change the way the [] method (to be honest I do not know what to call it!) works. Does someone have any idea about this?

enter image description here

UPDATE 3 When the text without #DESC goes into the textbox, I have a event handler for KeyDown which will transfer the command to the instrument.

like image 598
Saeid Yazdani Avatar asked Dec 26 '11 12:12

Saeid Yazdani


2 Answers

Rather than trying to battle the autocomplete functionality that Microsoft has implemented, I strongly suggest that you use a multi-column combobox instead.

All of the ones that we have used support auto-completion, so you can store your command in the first column and have it be your value and then store your description in the second column.

There are a tremendous number of controls available for purchase (Infragistics, Intersoft, Syncfusion, etc) and you can probably find free or self-built versions on various sites such as CodeProject.

Going this route should save you a lot of time.

like image 181
competent_tech Avatar answered Nov 06 '22 07:11

competent_tech


Staying with using the SuggestAppend method and loading your text file as a Custom Source for the Auto Complete feature of the TextBox.

You could use the Leave event of the TextBox to remove all text after # and trim the result:

private void textBox1_Leave(object sender, EventArgs e)
{
    textBox1.Text = textBox1.Text.Remove(textBox1.Text.LastIndexOf("#")).Trim();
}

This way the description stays in the auto complete list, but as soon as you tab out or leave the textbox, only the command remains.

like image 28
Kev Ritchie Avatar answered Nov 06 '22 07:11

Kev Ritchie