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..
Related
I believe am in need of creating custom RichTextBox in C#. One kind of like that:
I admit it might not even have to be RichTextBox, but after some research I decided it's gonna be the easiest way. Functionality I need are icons at each row, checkboxes and text formatting. My program will process each line of the text and mark lines that are correct, incorrect, and strike out lines not necessary in further work, while showing line that's currently processed and allowing user to edit some lines freely (here: lines before Around 3 000 won't be editable, but those under the line will).
What's the problem then? I have no idea how to get it done. I've seen tutorial on how to make single-line textBox with icon or checkbox nearby, but I have no idea how to make both, and for multiline textBox (so I could freely scroll and everything would work fluently). I've read some questions on SO as well, but neither helped me.
I just don't know how to get started, I realize it won't be 5 min work, but I'm willing to do it. Until now I've been only able to create custom control deriving from RichTextBox, but I have no idea which methods and how to override. Any help appreciated.
Try to use WebBrowser control instead RichTextBox.
You can add CheckBoxes and editable content by setting a correct HTML code to it.
Set each TextBox or CheckBoxes ids and use GetElementsByTagName or GetElementById to access inner elements to get or set its attributes or values.
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.
I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.
Im trying to emulate a 'find as you type' function like that of the address bar ("awesome bar") in FireFox. I want a suggestion box to appear below a textbox, and the suggestion box contains strings that contain what is in the textbox. I looked at the autocomplete feature of a normal WinForms textbox, but it seems to only search the beginning of the strings.
Has anyone here built or have experience with implementing something like this?
edit:
Some clarification- It is a WinForms project.
It needs to search inside a string, not just the beginning (which is what a normal textbox does if i recall correctly). And the suggestions should be displayed in a popup like a textbox autocomplete.
You need to handle the TextChanged event for your text entry field, and when the text changes, start a new thread running that will apply the new search. If the text changes before you get your results back, just kill the thread. If the thread returns results in time, display them.
You can get slightly more advanced (e.g. wait for a short time after the text changes so that the user can type a word without you triggering off loads of useless threads) but essentially that's it.
There was a discussion earlier on this topic where the author concluded that you are better off doing the whole thing yourself.
How can I dynamically change auto complete entries in a C# combobox or textbox?
I did something vaguely similar, but more like the iTunesĀ® search box than the Awesomebar. My control used the textbox to actively filter a grid; so it wasn't for autocompletion.
...but... basically I had a DataView of all eligible items, whenever the TextBox's Text changed I'd update the Filter to hide all non-matching items. It worked well and might suit your needs for filtering the data--but not sure how to go about using it as an AutoComplete source for the textbox.
I have done such a thing for an app of mine not too much time ago.
What I did is make my search function in a new thread, so every time I typed a new letter, it called the search function in another thread, so I could keep on typing.
I can post some code if you need, but this should be enough to get you started. :)
Hemmed and hawed about deleting this after I noticed the OP edit mentioned winforms, but I think it'll be useful to anyone who comes here looking for the same but for asp.net apps.
Just because nobody has mentioned it yet, for a webforms app you absolutely want to do this with ajax (.net controls or pure JS, your choice). The feature is often called "autocomplete" and the one thing you don't want it to be breaking the seamlessness by making server round trips at the page level.
I suggest you look at this and this.
I've used Search As You Type in C# and How do I make a Textbox Postback on KeyUp?
Basically you use the keyup action to call a postback thats attached to the trigger to the update panel. then you do your update in the textbox_changed event with the dataview or whatever your backend looks like.
I have been binding textboxes in a winform with C# to a dataset. Whenever the data doesn't validate with what the database except it silently forces the focus to remain on the textbox. How can I catch the validation error and tell the user about it (and free the focus)? The BindingSource event OnDataError isn't fired.
I had a similar problem once. The focus remained in the textbox which was binded to some numeric database field when the user modified text in a textbox and then deleted it so the text property was an empty string. I solved it with something like:
textbox.DataBindings["Text"].NullValue = "";
It solved the problem for empty inputs. I don't know if it would be of any use in your case, but I'd be also interested in more general solution.
Here's also some related question on SO:
Data-bound TextBox: can't exit
Never rely on just what "Visual studio has done for me" if you don't fully understand what it's doing. I would strongly urge you to take the time and figure out how to do what it is you want to do all by yourself (meaning without designer generated code). To get you started, there are some events on the TextBox that can help you out. Start here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx
Specifically the validating and validated events should be what you're looking for.