Combobox select item in dropdown list C# - c#

I have an combobox poppulated with items from a database table.
When I change the text i repopulate the combobox with items from the database table.
But when I enter text and the list with suggestions opens no item in de list is selected.
And i want a item to be selected so when you press enter that it becomes the selected item.
This is a winforms application.
Thanks.
cbxNaam.Items.Clear();
string query = "SELECT bedr_naam FROM tblbedrijf WHERE bedr_naam LIKE '%" + cbxNaam.Text + "%'";
string[] bedrijfsnamen = Functions.DataTableToArray(Global.db.Select(query));
cbxNaam.Items.AddRange(bedrijfsnamen);
cbxNaam.Select(cbxNaam.Text.Length + 1, 0);

You need to select an item in the list - it looks like you are trying to select some text in an item.
piecing together some info from your other comments it looks like you want to essentially do a wildcard match on the text in the dropdown list, to do this you will need to modify behaviour of the handler for the text changed event/method, either derive from combobox and override it or ad an event listener on your instance.
you can then do a search on the data in your array - something like
private void cbxNaam_TextChanged(object sender, EventArgs e)
{
var matchingStrings = bedrijfsnamen.Where(s => s.Contains(cbxNaam.Text));
cbxNaam.SelectedItem = matchingStrings[0];
}
you will need to be a little careful of multiple matching items etc.

If you are using WINFORMS,
You have to use AutoCompleteMode set to AutoCompleteMode.Append and AutoCompleteSource set to AutoCompleteSource.ListItems
Please check this answer ComboBox AutoComplete Custom Capabilities

I don't know if I understand you good, sorry if not! Here is my answer:
If you want for your comboBox to be populated when you write in you comboBox you need to set its properties:
AutoCompleteMode to Append
and
AutoCompleteSource to ListItems
on Enter your item will be selected.
Hope I helped you? Rock On!!!:-)

Related

How to disable combox dropdown when user enters text into the combox

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.

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.

C# - Change index of ComboBox item?

I have a combobox that I want to be able to add items to the beginning of. For example, when you click it, you get 1,2,3 , but I want to be able to add an option, 0, so that when you click it you get 0,1,2,3.
Is this possible without rebuilding the combobox?
Just use the Insert method of the Items property of the combo box:
myComboBox.Items.Insert(0, "New item at top");
Yes, you can use the Insert() method on the Items property of your ComboBox object.
var comboBox = new ComboBox();
comboBox.Items.Add("1");
comboBox.Items.Add("2");
comboBox.Items.Add("3");
comboBox.Items.Insert(0, "0");

Master-Detail via ComboBox in C#?

I have a form showing the details of a client (various controls), along with their orders (in a DataGridView).
I am trying to add a ComboBox to let the user select one of the client's orders and display the items associated with it in a separate DataGridView.
However, I cannot work out which DataSource/DataBindings I need for either the ComboBox or the items DataGridView - please can anyone give me a few pointers?
Orders will be the data-source for the ComboBox - OrderId will be the Value field while Order Number or Order Date will be the text field. Items for that order will be data source for the items DataGridView. This grid needs to be bound in the combo-box's selection change event (set auto postback true for the combo box). Hope this helps.
Psuedo code for selection change event would be
protected void Orders_SelectedIndexChanged(object sender, EventArgs e)
{
var orderId = int.Parse(Orders.SelectedValue);
// Get items for this order from data store
var items = ...
// Bind with items grid
OrderItems.DataSource = items;
OrderItems.DataBind();
}
Orders is name of combo-box having orders while OrderItems is gridview to display items.
This seems to be already answered.
For example here:
How to get or set data from ComboBox in DataGridView
Or even better, just search for "DataGridView combobox" in StackOverflow and you will find many topics which cover every aspect of that problem.

Categories