C# combobox appropriate event type - c#

I have a combobox and with this I have a associated event
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
}
My comobox is populated with two items a and b
I am setting combobox8.selectedItem = x where x= a or b. My event only fires if I select a from b or b from a. It does not fire if I again select a from a.
How can I do it and what's the appropriate event to deal with it ?
Moreover I am doing it all programmatically.

It makes sense that the event doesn't fire again. The selected item doesn't change. Depending on what you actually want, there are a lot of events you can utilize. You might start with Click, or DropDown, or DropDownClosed, for instance.

It won't fire because Selected Index did not change...
Take a look at msdn documentation for a list of comboBox events:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox_events.aspx
You'll find out you can use more one depending on what you want to achieve (leave,lostfocus, [...])

Because the index did not change, the event is not fired. Since you need this processing when you are refreshing the form programmatically, call the appropriate code programmatically as well:
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
ProcessComboBoxInput();
}
private void RefreshFormProgrammatically()
{
// Refresh the form here...
ProcessComboBoxInput();
}
private void ProcessComboBoxInput()
{
// Process the comboBox8 here...
}

Because its selected index changed event. From a to a nothing has been changed. You can try onclick event.

Related

Is it possible to fire EditValueChanged event in CheckedComboBoxEdit control immediately after the item has been checked

I need to update the edit value of the CheckedComboBoxEdit control immediately after the item has been checked
The MouseUp event can be used to trigger EndEditwhich will commit the change slightly faster.
I used this to solve my simular issue when working with a CheckBox within a DateGridView. As by default the event to submit changes in a DataGridView only fires when leaving the cell.
You should subscribe Popup event of CheckedComboBoxEdit, find CheckedListBoxControl and subscribe ItemCheck event. Like this:
void _orgStructEntitesCheckedComboBoxEdit_Popup(object sender, EventArgs e)
{
var popup = (IPopupControl)sender;
var control = popup.PopupWindow.Controls.OfType<PopupContainerControl>().First().Controls.OfType<CheckedListBoxControl>().First();
control.ItemCheck += control_ItemCheck;
}
void control_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
{
var checkedListBoxControl = (CheckedListBoxControl)sender;
var current = checkedListBoxControl.Items[e.Index];
}
Use e.Index to get current item changed.
More information here and here.

Executing an action only when I select an item form the combo box

I have a ComboBox and in the form's load event it's populated with data. Now when I select an Item form the ComboBox, it should perform some actions. So I know few events which can be used in this case like
SelectedIndexChanged
SelectedValueChanged
etc.
But the problem is those events are raised even when setting the DataSource of the ComboBox and selecting a default index etc. when the form is loaded.
ComboBox1.DataSource = dt;
ComboBox1.SelectedIndex = -1;
What am I trying to do is that I just want to execute an action only when I select an item form the combo box. Is there a mouse event that could be used in this case?
The comboBox.SelectionChangeCommitted Event seems to do that.
Otherwise you could set a boolean value before you bind the datasource which you can use inside the event to ignore it.
private bool blnIgnoreEvent = false;
// in Form_load
blnIgnoreEvent = true;
ComboBox1.DataSource = dt;
blnIgnoreEvent = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!blnIgnoreEvent)
{
// go ahead
}
}
I don't believe there is another event that better handles what you'd like to do. XIVSolutions has a neat work-around for the event firing when you bind the data source: How to prevent selectedindexchanged event when DataSource is bound?
Also, since SelectedIndexChanged works for all cases, why not just handle the first?
if (ComboBox1.SelectedIndex == -1)
{
return;
}
If -1 corresponds to a value you'd like to be able to select, just use a private field to store some bool that you check to determine whether or not it is the first time the action has been executed.

Avoid calling indexchanged event when filling datasoruce

I'm filling a combobox with the datasource property in a c# winform app. In the other hand, I'm firing up an action with the SelectedIndexChanged of the same combo. The problem is that whenever the combo is filled with datasource the SelectedIndexChanged is called and I just want this event to be called when the user in fact does a selection.
Is there a way to avoid calling this event when filling the combo?
This is some of my code
//Filling the combo with some data
combo_cliente.DataSource = clientes;
combo_cliente.DisplayMember = "NomComp";
combo_cliente.ValueMember = "IDPersona";
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
// Here is the action to be triggered when user perfoms a selection
}
Thanks
Maybe unsubscribe and then subscribe again:
combo_cliente.SelectedIndexChanged -= combo_cliente_SelectedIndexChanged;
combo_cliente.DataSource = clientes;
combo_cliente.SelectedIndexChanged += combo_cliente_SelectedIndexChanged;
im assuming you assigned the event handler with the designer so they are bound when the control is instantiated. alternatively you could assign them in code after populating the controls.
Attach your event handler in your code behind instead of doing it on your aspx page and do it affer you have finished loading your control.
you need to add a blank record as the first of your combobox. Then in your code, you can write this;
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
if!(comboBox1.SelectedValue.ToString()== string.Empty)
{
//Here is the action to be triggered when user perfoms a selection
}
}

ListView ItemChanged puzzle with MessageBoxes - ItemSelectionChanged called twice

Here is the problem: I have a Windows Forms application that I'm developing, and in one segment I'm using a ListView control.
What I'm trying can be simply stated as: on event ListViewItemSelectionChange show a MessageBox for user to confirm the change, if not confirmed change to let's say the first item. This change to the first item would again fire ListViewItemSelecionChange, so I unregister and re-register the event handler method, so everything should be good, right?
What actually happens is that the handler method is called twice (actually ListView should fire two events on Selection change, one for deselect, other for newly selected item, but I have an e.IsSelected statement at the beginning to catch only selected items, so actually you could say that there are four events fired).
The problem is, if I generated the first event with mouse click on ListView item, and I've unsubscribed before programatically changing to the first item, what generates the second event firing? Is it some focus change because of the MessageBox call? Is there any way to prevent the second event to fire?
I have a simple example solution here, it can't be more simlified (25 SLOC), so if you can, please take a look. Note that commenting the line "if (ShowMessageBox())" stops the second event from firing, is this some focus change problem?
http://www.filedropper.com/listviewtestwithmsgbox
Edit: the relevant code:
private void listViewWithSelection1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// listview actually generates two ItemSelectionChanged events,
// one for deselect of a item, and another event for a newly selected item (which we want here).
if (e.IsSelected)
{
if (ShowMessageBox())
Button1_Click(null, EventArgs.Empty);
label1.Text += "item selected ";
}
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void Button1_Click(object sender, EventArgs e)
{
// change ti first ListView item
listView1.ItemSelectionChanged -= listViewWithSelection1_ItemSelectionChanged;
listView1.Items[0].Selected = true;
listView1.ItemSelectionChanged += listViewWithSelection1_ItemSelectionChanged;
}
Hmm, can you describe how the selection is being changed to begin with? If it's by the user clicking to select an item, perhaps catch the Click or DoubleClick event rather than the ItemSelectionChanged event? I have this snippet I'm using on a program currently. If the user double clicks the list box (listView, in your case), do something with the selected item.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void listView1_Click(object sender, EventArgs e)
{
if (ShowMessageBox())
listView1.TopItem.Selected = true;
label1.Text += "item selected ";
}
}
Edited to include relevant code.
One way to do this is to have a flag which says should the on change code run.
In your ListViewItemSelecionChange code you check the value of the flag and run code accordingly.

How do you respond ONCE to a user selecting a range in a ListView control?

I'm trying to handle the user changing which items are selected in a listbox (by updating information about what's selected), but if you select a range (using shift+select), it actually fires a separate 'ItemSelectionChanged' event once for EACH item that was selected/deselected, i.e. if you selected 100 items, you get 100 events, and the first time the event handler's called, it seems to have no way of knowing that there's more to come.
Is there a way to not respond until the process of selecting/deselecting items is complete?
There is no SelectedItemsChanged event, I'm guessing you mean SelectedIndexChanged. What you can do is leverage the Control.BeginInvoke() method. The delegate target starts running when the UI thread goes idle again, after all the events have been fired. Make it look like this:
bool listUpdated = false;
private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (!listUpdated) {
this.BeginInvoke(new MethodInvoker(updateList));
listUpdated = true;
}
}
private void updateList() {
listUpdated = false;
// etc...
}

Categories