Override or overload AutoComplete Append rule - c#

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
Does not add any text starting from # char while appending the suggested text
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?
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.

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.

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.

Here is the link which has the complete example for Autocomplete
http://technet.microsoft.com/en-us/query/chff42zw

As an answer to your update nr. 2:
The [] is actually an Index Property.
Your class implements an collection. You can get item at index X by using the collowing code:
var thirdItem = myAutoCompleteCollectionInstance[3];
Index properties can be created manually by using the code below, which you can use to customize the order by returning the items you want at the specified indexes.
public string this[int index] {get ...} {set...}

The AutoComplete property of Textbox is set SuggestAppend which means it adds both the text and the description. So you need to set AutoComplete property of Textbox to "Suggest" value only.
According to MSDN the Autocomplete property can take four enum values namely
The following are the values of AutoCompleteMode:
Append : Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
Suggest : Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
SuggestAppend : Appends both Suggest and Append options.
None : Disables automatic completion. This is the default.
Try these values instead of subclassing the AutoCompleteStringCollection.

Related

C# Textbox Autocomplete: How to provide custom 'matching' function?

I'm using C# TextBox with AutoComplete via a Custom Source of a collection of names, e.g. John Smith, Mary Jane, etc .. The problem is that if I type in 'John', I do see 'John Smith' in the dropdown. But if I start typing 'Smith', I no longer see 'John Smith' in the dropdown. I'd like to change this, and I think I would have to extend the TextBox class and provide my own function for matching data. I just don't know which TextBox function would need to be overwritten.
Secondly, another related issue I'd like to solve this is: The dropdown text is always left aligned. How can I make it center aligned ?
There is no directly way to do it because AutoCompleteCustomSource only allows prefix matching based on your input string.
From TextBox.AutoCompleteCustomSource Property
Use the AutoCompleteCustomSource, AutoCompleteMode, and
AutoCompleteSource properties to create a TextBox that automatically
completes input strings by comparing the prefix being entered to the
prefixes of all strings in a maintained source.
Best option seems to override your OnTextChanged event of your TextBox. The question below has a good example about it;
C# AutoComplete

Textbox autocomplete source

With a combobox in winforms I am able to add items that can be a custom class and what is shown in the combo is the result of the ToString() method of that class
I would ideally like to have similar behaviour with the autocomplete of a TextBox, but it seems to me that you must provide an AutoCompleteStringCollection for this. Is anyone aware of a way I can associate an int value with each string in the AutoCompleteSource for the textbox?
Edit - I cannot simply look up for the int based upon the selected string because they may be strings in the textbox autocomplete that are the same
Here is a possible solution; a bit of a hack, but it might help you out.
If you add the numbers to the string in a way that is both acceptable to the users and easily reconizable/removeable you can test for it in the KeyUp event, extract the index and clean up the Text.
So a string could look like this
New York City <<01>>
and you wouldn't have much trouble to get at the index and removing the markup..
Look at this answer by Hans who deals with detecting the (fake) KeyDown that gets fired. You can adapt this to deal with the KeyUp so you won't interfere with the normal editing.
But to reiterate, this makes no sense, if for the choice of the user the strings have a meaning and not their, say, position..

Make Current Line in Read-Only Rich Text Box Editable

I'm attempting to make a command prompt clone in C# so I can get familiar with using IO. However, instead of just one text body, I created two text boxes (one for the command and another for the "parameters") and a rich text box to view the result of the commands. It may sound confusing and the title may be misleading, but I didn't know how else to word it. Anyway, my question is- how do you make only the current line editable and the rest read-only? And how would I be able to combine the commands and parameters in the two text boxes so I wouldn't need two separate text boxes? I have spent 6 days trying to figure out the logic to implement this but I got nothing.
Here's a picture of the form:
And here's what I want to make it similar to:
I'm not sure if you can do that, but if it was me, and this was a "get it done now" situation (and this is just off the top of my head), I would create a user control to contain the "screen". This user control would have the RTF or list box as the top, and a textbox flush under it.
I would remove the borders and wrap both these controls in a panel that has borders. This would simulate a single control.
The textbox would check for the enter key in one of the key-press events, and the control itself would have events that could be handled by the parent control.
This may be hacky, but it would probably be what I'd do in a last minute situation.
Oh as far as the command and parameter stuff, if you read the textbox as a single value, then split the string into an List or array, you could then define a switch or some other conditional code that would know what to do with parameters (index 1+), based on the value of the first item/index.

How create console usercontrol

I need to create a usercontrol "Console".
I was faced with such problems:
If I use a TextBox, how do I prevent removal of an already recruited command?
If I use a ListBox/ListView, how do I select all the text?
Please tell me what to do from the Console.
The console should be able to complete the command (by pressing Tab), allow selection of text, and prevent the entry of already established commands.
Here is a start:
http://ansiconsole.codeplex.com
I used a bitmap, and render text to it. This way I have complete control over the input and output.
If you need some "simple" console application: insert commands, I presume in some DSL language, view result of execution, and other stuff, you can try to programm on RichTextBox base, which can give also some styling to content.
Reuse some already ready (complicated) editors, like for example:
Scintilla
And work to limit possibilities of that kind of component to fit your needs.
Regards.
You could consider deriving from the RichTextBox control, as Tigran suggested.
Depending on what you want the user to be able to do, you will have to put some logic in there that restricts what they can and cannot select. (For example, if you don't want them selecting previous commands). You can obtain the text that they've selected via the SelectedText property. And then put in your custom logic, for example, Ctrl+C will copy the text into a variable.
You may consider having a MaximumSize property so that old commands will be erased after the console becomes so large.
Winforms already has a type of Autocomplete that you could use, or simply keep a list of keywords and when the user presses TAB, fill in the first word in your list that starts with what they've already typed.
To obtain the command itself, and not any of the previous text that was entered, you will probably want to take everything from the LAST newline to the end.
The code may look something like this:
String allText = this.richTextBox1.Text; // All the text from the rich text box
Int32 lastIndex = allText.LastIndexOf("\n"); // Find the position of the last newline
String command = allText.Substring(lastIndex + 1); // Substring starting at the character after the last newline
And of course when the user presses RETURN, the command will be sent to your code and executed.

Coding field navigation while creating paperless online versions of paper forms using WPF with C# and LinqToXml

Using WPF(with C# and LinqToXml), I am reproducing a paperless version of an existing paper form. The existing form includes some one-character-per-box text strings. I have already duplicated the appearance of the paper form using XAML. How can I add code to a one-character TextBox to automatically send control to the next TextBox once it has been filled(without requiring the user to tab to the next TextBox)?
Also, these TextBox sequences facilitate the input of key field values. Once the last one-character TextBox receives a value from the keyboard, how could I code an event trigger to automatically retrieve the appropriate data record field values from the Xml data file?
Will I need to include a button on the form, or can I code the form so that the retrieval automatically occurs when the last one-character TextBox has been filled?
The neatest way to achieve what you want to do is probably to create an class representing the code that exposes the digits via properties that are bound to the textboxes (or as a string via one property and then use a ValueConverter to update the appropriate digits) and implements the INotifyPropertyChanged interface (throwing a PropertyChanged event each time the property/properties are set. You can then either create a handler that listens for PropertyChanged events from the code object, checks that all of the digits are filled in and if so loads the data from XML, or alternatively you could do that checking inside the object and raise some other event to let the application know a full code is entered.
As for how to move to the next textbox, you could create a TextChanged event handler that calls the UIElement.MoveFocus() method and register it with all of the textboxes, like so:
private void textChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null && textBox.Text.Length == 1)
{
TraversalRequest tr = new TraversalRequest(FocusNavigationDirection.Next);
textBox.MoveFocus(tr);
}
}
You may also want to set the MaxLength of each textbox to 1 to prevent copy and pasting of text, etc. and you could also look into things like ValidationRules for checking for illegal characters, etc.
Hope this helps (just ask if you need help with any of that).
Regards,
James

Categories

Resources