Autocomplete [contains instead of starting with] in winform TextBox - c#

// [in designer] textBoxInContext.AutoCompleteMode = Suggest
// [in designer] textBoxInContext.AutoCompleteSource = CustomSource
AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(myArrayofStrings);
textBoxInContext.AutoCompleteCustomSource = autoComplete;
I have this code which works well as documented in MSDN. Problem: if user types "PS" it shows all the string starting with "PS"; I would like to display all the strings containing "PS"
Any pointers ?

If you don't find another way, I suggest doing it manually:
Use a combobox with no items(you'll fill them manually later).
Have a string array with your possible suggestions.
At the combobox.TextChanged or KeyUp event take its text and compare it to your string array whichever way you want and, after clearing the combobox.Items, add the found results to the combobox.Items and make sure to set the DroppedDown property to true if you have found suggestions.

The stupid but fun suggestion: make a class that inherits from AutoCompleteStringCollection and play with it in debug to see if you can fake this out.
The normal suggestion: make your own autocomplete with a listbox.

Related

How to change textbox value by selecting suggested value?

I'm creating a Desktop App in Visual Studio With C#.
I've a String array called Tag_Word_Box. In a textbox if I type something, then it will be show suggest words from Tag_Word_Box array.
In briefly, assume that- I've 5 words respectively [aabc] [abc] [abd] [abcg] [bcd] If I type in textbox only 'a' then it will be show all of words without 'bcd'.
If I select 'aabc' or press enter one of suggesting words, then- it will be assign whole word in textbox, that means textbox value will be changed with selecting word by 'a'.
BTW, I know that- it will be solved by trie algorithm to find out words. But I want to know that how to do that operation in visual studio respect of C# that-
1. to show suggesting words when I type something
2. and how do I change textbox value by selecting from those?
Thanks :)
All textboxs have an 'AutoCompleteSource' property. Set it to CustomSource from the Properties toolbar. Then set the 'AutoCompleteMode' property to SuggestAppend. Now, in the code, add this to the TextChanged Event of the textbox:
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
To do the complete thing in code, add this to the TextChanged event of your textbox:
textBoxName.AutoCompleteSource=AutoCompleteSource.CustomSource;
textBoxName.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
Remember to replace textBoxName by the name of your textbox before using this code.

C# How can I get a textbox suggestions list into a list<string>?

C# How can I get a textbox suggestions list into a list?
e.g. say I do
textBox1.AutoCompleteSource = AutoCompleteSource.FileSystemDirectories
or
textBox1.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList
and
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
I get suggestions come up in the popup suggestions window for the textbox but can I retrieve those suggestions in a list?
You will want to set up your TextBox in the following manner:
AutoCompleteStringCollection source = new AutoCompleteStringCollection();
// Add each item to the collection
source.Add("some string");
TextBox textBox = new TextBox();
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox.AutoCompleteCustomSource = source;
Then you can access the items wherever else by doing the following:
foreach(string item in textBox.AutoCompleteCustomSource)
{
Console.WriteLine(item);
}
Or however you want, textBox.AutoCompleteSource is defined now as a class that implements IList, ICollection, and IEnumerable. So you can use any methods that would access a List, Collection, or Enumeration. It's up to you.
To summarize the comments below, to access the list of items in auto-complete for anything other than the CustomSource, you would likely need to hook into the Windows API, or maybe use reflection (though that is unlikely, Windows API would be the most likely to work).
As per my research, i do believe that there's no direct way to get list of suggestions for TextBox control with AutoCompleteSource set to one of defined values.
The author of answer for this question: How do I make the autocomplete list for a TextBox editable? suggest to read CP's article: C# does Shell, Part 4 to be able to use AutoComplete to expand strings written in an edit box.
To reach this result:
you can follow two ways, the firs by environment Properties tab and
setting the follows properties:
The best way is create this effect by code, see my example as a follows:
AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();
foreach (string name in listNames)
{
sourceName.Add(name);
}
txtName.AutoCompleteCustomSource = sourceName;
txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;

C# Listbox selected item text

I am working on a wpf app, c#, in visual studio. My aim is to take the selected item text from a listbox on button press and add it to a list whilst also appending it to a text block. My code thus far:
bill.BillItems.Add(lstbxVeg.SelectedItem.ToString());
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
Where bill is the class name, BillItems the List name. I can see that items are indeed added to the text block, yet instead of the desired string relating to the listbox item selected, it reads System.Data.DataRowView. Where am I going wrong?
EDIT: This is not the correct answer.
lstbxVeg.SelectedItem.Text.ToString()
I think the actual problem is this line:
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
You mentioned you want to append the value, this line is not accomplishing that.
I think you want to do this:
txtblkBill.Text += lstbxVeg.SelectedItem.ToString();
If that isn't the answer you should look up String.Join() as it takes a String[] not a List.

Text property of Listbox control returns string when nothing is selected

listBox_groupmakingrepairs is a listbox control on my form. When first loading the form, nothing is selected in the listbox and the code in the following 'if' statement will run because the condition is true.
if (listBox_groupmakingrepairs.Text == "")
{
Error = "You must indicate what group will be making the repairs.";
Con = listBox_groupmakingrepairs;
}
But, if I run the following code...
listBox_groupmakingrepairs.Text = "Cell";
Followed by this code...
listBox_groupmakingrepairs.ClearSelected();
The listbox will not have anything selected, yet it will cause the first code snippet above to be false and not run the code in the 'if' block. When I step through and check the value of 'listBox_groupmakingrepairs.Text' it is "Cell". Yet on the form, the listbox clearly has nothing selected.
Am I using the Text property of the Listbox control incorrectly? Or is this a bug? Or am I missing something altogether obvious?
The way I see it I have a property (Text) that seems to work well most of the time. But under certain conditions it returns a value that isn't the correct value anymore. Why is the Text property returning an old value that has since changed? Does this make logical sense to anyone who can explain it to me?
That is because the ListBox control of C# winform control handles both the Text property and the SelectedItem separately, but in a way together.
It works in the following way:
IF [SelectedItem is True i.e. Item is selected] THEN
Text property = SelectedItem.ToString()
ELSE IF [SelectedItem is False i.e. No Item is selected] THEN
Text Property will still assume its current value (or in a way previous value) unless its manually reset
So always in such cases donot forget resetting the Text property. Hope this helps...
Maybe I am a bit slow, but eventually I realized that Microsoft is the creator, so to them I will go for the answer.
Here is what I found:
According to Microsoft, the following code I was using is a correct way of selecting text in a listbox control.
listBox_groupmakingrepairs.Text = "Cell";
Then, also according to Microsoft, the following code I was using is a correct way of deselecting all items in a listbox.
listBox_groupmakingrepairs.ClearSelected();
Finally, also according to Bill Gates (see two links above) the Text property is also a correct way to retrieve the text of the first selected item.
string SelectedText = listBox_groupmakingrepairs.Text;
But, as I described in my original question above, when I evaluated the Text property WHEN NOTHING WAS SELECTED IN THE LISTBOX, it contained text. But Microsoft says "this property returns the text of the first selected item." WHICH IS FALSE. It should have returned null because there was nothing selected...yet it returned text which was NOT SELECTED.
So to answer my two questions in my original post:
"Am I using the Text property of the Listbox control incorrectly?" -- The answer is YES.
"Or is this a bug?" -- Yes, it is.

How to get values from dynamically created elements using jquery in code behind

Hi and thanks for taking the time to asnwer my question.
I have the following problem. I have a form and a button which says "add new activities".
Whenever the button is clicked I add a new set of elements, namely 2 drop down menus + text area. How can I get the values of these newly created elements in code behind since I cannot know their ids up front?
If there is something unclear about my question, please let me know.
Thanks again!
But you must be setting id's (more importantly - name attributes) of new elements using certain pattern. Use the same pattern in a loop in server-side code to get values from Request.Form. Provide a hidden input where you put the total count of items added for the server-side to know the upper bound of loop counter.
You should set the ids when you create the elements if you plan to access them again
So if you this is your text area:
var textarea = document.createElement('textarea');
You can set the id to like this:
textarea.id = "taId";

Categories