How to disable combox dropdown when user enters text into the combox - c#

I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015

If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.

Related

Stop changing text when combobox is dropped down

Using WinForms. I have a combobox with DropDownStyle DropDown. In the items I put a single item "XA". When the user enters "X" into the ComboBox (not yet dropped down) and then presses the drop down button "X" is automatically replaced with "XA". How can I stop this happening? I would like the user to be able to keep text as "X" and only change the text to "XA" if "XA" was clicked in the drop down list.
To recreate create a new WinForms application and add a comboBox then add the following code
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.Items.Add("XA");
}
Note that if the user does not press the dropdown then "X" stays in the combobox.
Note that there is a similar sounding question here but it is actually different.
How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?
I think this solution should help you:
Winforms combobox bug - 2 items with the same value but different key
It changes if (m.Msg == LB_FINDSTRING) to m.Msg = LB_FINDSTRINGEXACT;, which should prevent the behavior you describe.

ComboBox Selected Value to be Blank

Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.

C# Combobox Displaying Blank Items

I'm coding a combobox in C# and for some reason the items in the drop down don't have text. When I have selected an item, it is shown in the combo box text field (the drop down list is always blank whenever I click the drop down button). The datasource seems bound properly because the proper values are being returned when I select items, and the size of the drop down list will change depending on how many items the datasource has. Everything looks fine except for the fact that it seems like my drop down is populated with a bunch of empty strings, which it clearly isn't since as soon as an item is selected the proper text will display.
This is the relevant code:
if (list.Count > 0)
{
cboCustomers.DisplayMember = "Name";
cboCustomers.DataSource = list;
cboCustomers.ValueMember = "ID";
cboCustomers.SelectedIndex = 0;
}
I have looked for an answer to this but can't find it anywhere...I'm sure it's something really simple, but I can't figure it out. The closest problem I found had an answer suggested to set the display member before the data source, which clearly didn't work.
The list is populated from a database query. This will run on keyUp, the idea is that the list is populated as the person is typing based on the info given. So if I wrote 'S' I'd get a combobox with a dropdown that had all the clients starting with 'S'.
Given you don't have any anomalies in your binding, you are probably being affected by DrawMode property of your ComboBox, which may be set to OwnerDrawFixed or OwnerDrawVariable. Set it to Normal and things should get better.
as soon as an item is selected the proper text will display.
A foreground color the same as the background color will produce the same results you are seeing.

Problems with ComboBox AutoComplete feature when Items are dynamically loaded

I am setting up a ComboBox to use the AutoComplete feature with AutoCompleteMode = Append and AutoCompleteSource = ListItems. When I load my form the Item list of the ComboBox is empty, and then I keep adding new values from the ComboBox.Text on a specified event (when a specific button is pressed).
Bottom line is that when the ComboBox's Items property is populated dynamically from an event, the AutoCompletion does not work as expected. My first entry into ComboBox.Items, always completes properly, but the later ones don't. If I click the arrow to dropdown the list, then all the Items so far entered autocomplete properly. If I Alt-Tab into another application and come back to the combobox, all the Items entered so far autocomplete properly.
It comes across to me that internally the combobox reloads its autocomplete list based on some event, but so far I have tried calling
ResumeLayout(true);
Refresh();
Invalidate(true);
Update();
DroppedDown = true;
DroppedDown = false;
but to no avail
Can someone enlighten me on how to dynamically add entries in the ComboBox.Items list and still have auto-complete with ComboBox.AutoCompleteSource = ListItems work correctly.
btnExecute is the default button on the form which contains the combobox. So the below function is invoked on pressing enter on the combobox
private void btnExecute_Click( object sender, EventArgs e ) {
cboCommand.SuspendLayout();
cboCommand.Items.Add(cboCommand.Text);
cboCommand.Text = "";
// Make some call here, so combobox reloads its cache for autocompletion
}
relevant portion from the auto generated winforms code
this.cboCommand.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
this.cboCommand.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cboCommand.FormattingEnabled = true;
I am using .NET4 Client profile on Windows Vista. May be it plays a role here
Anyone?

dynamically changing the listview based on the values entered into the textbox

I have a textbox and a listview. The listviewis populated based on the value entered in the textbox. Suppose I am entering any name in the textbox. As I type the name in the textbox, the results in the listview should change dynamically. For example, if I am entering John in the textbox, after entering Jo, the listview should populate the results that start with Jo and if I enter h the listview should populate the results with Joh and so on. Please suggest me a solution (in c#) for this.
Handle the "TextChanged" event of your textbox and make it run an update on your ListView given the current text.
private void txtExample_textChanged(object sender, EventArgs e)
{
UpdateListView(txtExample.Text);
}
Be wary if your search is time-expensive as the textbox will start "lagging" (missing user keystrokes) while the search is performing.

Categories