ComboBox SelectedIndexChanged event fired twice for items with same DisplayMember - c#

I have a combobox as below with 3 items ("abc", "abc" & "bbb").
The combobox also has AutoCompleteMode "SuggestAppend" & AutoCompleteSource "ListItems" properties. Now I want the text box to show the SelectedIndex of the combobox as below:
http://i.imgur.com/MJ4JdDN.png
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.SelectedIndex.ToString();
}
Everything seems fine until I select the 2nd "abc", the SelectedIndexChanged event will enter for the 1st time & display the index properly on the text box. But when the combobox lost focus, the SelectedIndexChanged event will fire again, causing the index to be displayed wrongly. I found that it only happens to items with the same value. Is there a way I can stop the event from firing twice?
http://i.imgur.com/gEw46xf.png
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Description"; //Some descriptive field to be shown in combobox
this.comboBox1.ValueMember = "Code"; //Unique code that user won't understand
this.comboBox1.SelectedIndex = -1;

put comboBox1_SelectedIndexChanged code in comboBox1_ValueChanged and there u'll get index in event arguments(sender i.e your comboBox cast it in combobox).
use comboBox1.SelectedIndex and u'll get the index.

Related

Windows Forms Combo Box Text doesn't update when I manually change the text in the SelectedValueChanged Event

I have the following code
private void cbAddTicketItem_SelectedValueChanged(object sender, EventArgs e)
{
string[] arr = cbAddTicketItem.Text.Split(' ');
cbAddTicketItem.Text = arr[0];
}
cbAddTicketItem is the combo box where the user is selecting from a list of items. The text of each item includes a description. I want to get rid of the description and just keep the value. Debugging shows that cbAddTicketItem.Text has the correct value but the text doesn't change on the form.
I think the issue is that either winforms is not firing the textChanged event, or it is overwriting it after my coded event runs.
You're making life rather hard work. It's easier if you do something like this:
var dt - new DataTable();
dt.Columns.Add("Disp");
dt.Columns.Add("Val");
dt.Rows.Add("Mark","1");
dt.Rows.Add("Luke","2");
dt.Rows.Add("John","3");
someCombo.DisplayMember = "Disp";
someCombo.ValueMember = "Val";
someCombo.DataSource = dt;
And then in some button click, let's say:
MessageBox.Show((string)someCombo.SelectedValue); //shows 2 if Luke is selected, etc

C# ComboBox DropDownStyle=Simple no working at runtime

Why runtime-created ComboBox drop-down is displayed when I put the property DropDownStyle=Simple? Is there some other property I miss in order not to show the drop-down?
The code is:
private void button3_Click(object sender, EventArgs e)
{
ComboBox cmb = new ComboBox();
cmb.Left = 100;
cmb.Top = 500;
cmb.DropDownStyle = ComboBoxStyle.Simple;
this.Controls.Add(cmb);
}
And the output:
The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle(v=vs.110).aspx
Setting it to Simple specifices that the list is always visible and that the text portion is editable: https://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle(v=vs.110).aspx.
If you don't want to show the list by default you should set the DropDownStyle property to either DropDown or DropDownList depending on whether you want the text portion to be editable:
cmb.DropDownStyle = ComboBoxStyle.DropDown;
In my opinion if you don't need to display the drop-down, you don't need a ComboBox.
You can disable your ComboBox setting
cmb.IsEnabled = false;
or I will use a read-only TextBox or something similar.

comboBox selected value change

I'd like to give the customer an option to choose a city from COMBOBOX, and once the city's chosen, a list of that city's streets should be in COMBOBOX2. I tried the following code and I got an error, during the first run, maybe someone can explain this to me?
private void Search_by_Apartment_Load(object sender, EventArgs e)
{
List<Cities> city = DAL.cities();
cmBxCity.DataSource = city;//Here he ran the second function, why?
cmBxCity.DisplayMember = "city";
cmBxCity.ValueMember = "cityID";
}
private void cmBxCity_SelectedIndexChanged(object sender, EventArgs e)
{
List<Streets> street = DAL.streets(Convert.ToInt32(cmBxCity.SelectedText));
// List<Streets> street = DAL.streets(Convert.ToInt32(cmBxCity.SelectedValue));
comBxStreet.DataSource = street;
comBxStreet.DisplayMember = "street";
//cmBxCity.ValueMember = "cityID";
}
The SelectedIndexChanged event is fired whenever the selected index is changed programatically or by the user.
As Davide Pirsa points out, when you change the DataSource of cmBxCity, you are programatically changing the selected index, hence firing the 'cmBxCity.SelectedIndexChanged' event at this line:
cmBxCity.DataSource = city;//Here he ran the second function, why?
One possible solution is to use the SelectionChangeCommitted event instead, which is only fired for changes made by the user.
when you assign the DataSource of the cmBxCity control its selectedItem changes from nothing to one item and this triggers the event handler cmBxCity_SelectedIndexChanged.
in the question you are talking about COMBOBOX and COMBOBOX2 but in the code there is only one control which is: cmBxCity.
shouldn't you show the streets in the second control called: cmBxStreet ?

DataGridViewCombBoxColumn cell value and different dropdown list

I've a very trivial requirement which makes me go nuts. I've a DataGridView in windows forms application. This contains one databound ComboBox Column. I'm using DisplayMember and ValueMember properties of that combobox.
Now my requirement is ComboBox should show the list of DisplayMembers in drop down list but when user selects one item from it, I should display the part of that DisplayMember in the combobox cell visible to the user. For example.
My display member list looks as below:
"Cust1 - Customer 1"
"Cust2 - Customer 2"
"Cust3 - Customer 3"
and when user selects any one of them from the above list (Say user selected 'Cust2 - Customer 2') then I need to display the value in the combobox column cell as only "Cust2" instead of complete DisplayMember text.
This DisplayMember list is a combination of two fields from the datasource bound to it i.e. First part points to CustomerCode field and second part points Customer name. I need to display only CustomerCode in the ComboBox cell after user selects one item from the drop down list.
How can I do this? Or should I come up with my own control which will have a different AutoCompleteCustomSource and display member value. Even I'm confused with that approach too.
Update: As no one has come up with any solution to my problem. Now I'm starting a bounty for that, also if anyone can suggest me other way to implement the same functionality, it would be great.
I've even tried to come up with my own control and tried to work on simple combobox to display a different value than the selected dropdown list, even that didn't work. Is there any other way to implement this? Any tips and tricks are greatly appreciable.
#Anurag: Here is the code which I've used.
Created a datagridview in the design mode. Created one column of type 'DataGridViewComboBoxColumn' that and named it as CustomerColumn.
In the designer file it looks like below:
private System.Windows.Forms.DataGridViewComboBoxColumn CustomerColumn;
This is the entity class which I've used for datasource
public class Customer
{
public int Id { get; set; }
public string CustCode { get; set; }
public string CustName { get; set; }
public string NameWithCode { get; set; }// CustCode - CustName format
}
In the form load event I'm doing the following:
CustomerColumn.DataSource = GetCustomers();
CustomerColumn.DisplayMember = "NameWithCode";
CustomerColumn.ValueMember = "Id";
I'm answering my own question because I've implemented my own solution to this by using custom control.
This custom control is created by keeping a textbox above combo box in such a way that only drop down button of combobox is visible.
Now I've created a custom column in datagridview deriving the DataGridViewEditingControl from my usercontrol.
I've added a property in Usercontrol which will take drop down list source from the control which is hosting datagridview.
Now in the EditingControlShowing event I'm setting this property as below
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridView2.CurrentCell.ColumnIndex.Equals(0) && e.Control is UserControl1)
{
var uscontrol = e.Control as UserControl1;
uscontrol.DropDownListSource = source;
}
}
This drop down list source is used in the usercontrol to set the autocompletesource to the textbox and datasource to the combobox as below:
Whenever I set the DropDownDataSource I'm firing an event in the usercontrol which will do the following. This is to ensure that every time EditingControlShowing event occurs for this column in DataGridView, this source is updated for textbox and combobox in usercontrol.
private void DropDownSourceChanged(object sender, EventArgs eventArgs)
{
textBox1.AutoCompleteCustomSource = DropDownListSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.DataSource = DropDownListSource;
}
Now whenever user starts typing in the textbox autocomplete source will display dropdown list with 'NameWithCode' values and if user selects one of them then I'll set it to the Text propery overidden in my usercontrol which will be used for the cell value in the DataGridView. Now based on the Textbox text (which is NameWithCode) I can get the code part and set it to the text property.
If user uses combobox dropdown button to select the item then I'll get the combobox selected text and set it in the Textbox which is ultimately used by the cell for getting value.
This way I could achieve the solution I want.
#Homam, solution also works but when I change the ComboBox's DropDownStyle to allow the user to type the value in the combobox it behaves weirdly and not getting up to the mark solution for my requirement. Hence I used this solution.
I know that this is not perfect solution, but I looked for a better one and I didn't find, so I went to a workaround
I did the following:
when the user open the ComboBox, I change the DisplayMember to "NameWithCode"
when the user close it, I return it to "CustCode"
You can Access to the ComboBox control by DataGridView.EditingControlShowing event for the DataGridView.
The code:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
var comboBox = e.Control as ComboBox;
comboBox.DropDown += (s1, e1) => comboBox.DisplayMember = "NameWithCode";
comboBox.DropDownClosed += (s2, e2) =>
{
// save the last selected item to return it after
// reassign the Display Member
var selectedItem = comboBox.SelectedItem;
comboBox.DisplayMember = "CustCode";
comboBox.SelectedItem = selectedItem;
};
}
Note: You have to start the DisplayMember with "CustCode"
Good luck!
Each time at the offensive of event dataGridView1_EditingControlShowing there is addition of new handlers for events comboBox.DropDown and comboBox.DropDownClosed. It results in the increase of number of these handlers and their repeated calls. This code decides this problem.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
var comboBox = e.Control as ComboBox;
comboBox.DropDown += comboBox_DropDown;
comboBox.DropDownClosed += comboBox_DropDownClosed;
}
private void comboBox_DropDown(object sender, System.EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
comboBox.DropDown -= comboBox_DropDown;
comboBox.DisplayMember = "NameWithCode";
}
}
private void comboBox_DropDownClosed(object sender, System.EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
comboBox.DropDownClosed -= comboBox_DropDownClosed;
var selectedItem = comboBox.SelectedItem;
comboBox.DisplayMember = "CustCode";
comboBox.SelectedItem = selectedItem;
}
}

how to do: what i pick on combobox1 will show on combobox2?

how to do: what i pick on combobox1 will show on combobox2 ?
is it possible ? (in C#)
thank's in advance
You need to subscride second combobox on the SelectedIndexChanged event of the first combobox and change value when event triggers. Also you need to make sure that both combobox have several items or you will need to add missing items to second combobox dynamically.
Event handler example:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object selectedValue = this.comboBox1.SelectedValue;
this.comboBox2.SelectedValue = selectedValue;
}

Categories

Resources