C# - Change index of ComboBox item? - c#

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");

Related

Manually change the selected item in a combo box

I am trying to manually change the selected item in a combo box so that it will physically change the value on the interface. I have tried changing the index value and display value on the drop down list, but this does not change the value on the interface. Has anyone ever done this?
Use case:
A user adds a new value to the database. The observable collection that feedsd the combo box is updated in memory with the new value to choose from. But, you want to save the user a click and just make the new value they added as the selected item in the combo box, rather than forcing them to add the new item, and then go over to the combo box to select it.
If you really want to do this...
You could try:
yourComboBox.SelectedItem = // the new Object that you want to set the comboBox to;
If the object is found, it will also automatically update the SelectedIndex.

keep combobox text synchronized with changed item text

I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?
I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.
Maybe I am missing something simple...
Edit
I tried binding, following the suggestion of SLak:
List<Label> labels;
MyComboBox.ItemsSource = labels;
The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:
labels[0].Contents = "new content";
then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.

How to display blank item in the combobox?

For a Combobox, I am getting a list of values from the System.Collections.ObjectModel.Collection.
I am assigning the values like this:
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
I don't want to see the first item in the list in the Combobox, unless I selected it.
How do I display a blank field in the Combobox when nothing is selected?
If I understand correctly you just need to reset the SelectedItem. Just set SelectedIndex to -1.
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
this.cmbSqlServer.SelectedIndex = -1;

How to set an item in ComboBox to "SelectedText" property of ComboBox dynamically in Load

In WinForm, I have a ComboBox. I am trying to do something like this.
When ComboBox has only 1 item, that item should be set as "Selected Text" for ComboBox,
and when it has items more than 1, first item should be set as "Selected Text".
I want this to be happened at the time of Form Load.
how can i do this?
If I understand it the right way, you want the first item of the combobox to be selected/shown in comboBox.
This is quite easy:
comboBox1.SelectedIndex = 0; //This will select the first item in the combobox (zero based numbering)
To set it right after the form is shown simply put it after
InitializeComponent();
of the appropriate form.
if combobox has only one item, then you can use below code
comboBox1.SelectedIndex =0;
if combobox has multiple item, and you need to select a particular item...
change only the index, index will start with 0, if you need to show second item, then the index will be 1
comboBox1.SelectedIndex =1;
You may use
if (ComboBox1.Items.Count>0) { ComboBox1.SelectedIndex=0 }

Combobox select item in dropdown list 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!!!:-)

Categories