I want to bind a ComboBox with checked items from CheckedListBox based on selection made by user.
This is how I bind ComboBox:
private void LoadFOCOutlets()
{
ArrayList outletList = new ArrayList();
Outlet objOutlet = new Outlet();
for (int i = 0; i < customCheckListBoxOutletList.CheckedItems.Count; i++)
{
objOutlet = (Outlet)customCheckListBoxOutletList.Items[i];
outletList.Add(objOutlet);
}
objOutlet.OutletID = 0;
objOutlet.OutletName = "Select Outlet";
outletList.Insert(0, objOutlet);
cmbFOCOutlets.DataSource = outletList;
cmbFOCOutlets.DisplayMember = "OutletName";
cmbFOCOutlets.ValueMember = "OutletID";
cmbFOCOutlets.DropDownStyle = ComboBoxStyle.DropDownList;
}
So, every time when a user check a new item, it should re-bind the ComboBox. The above code works fine.
But which event of CheckedListBox can I use to re-bind the ComboBox after a new item has been checked? I tried using ItemCheck Event. But It doesn't count the current selection.
Any help will be very much appreciated.
Try this event
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//Your code here
}
(Or)
private void CheckedListBox1_ItemCheck(object sender, EventArgs e)
{
//Your code here
}
Refer This
The following is excerpted from
Which CheckedListBox event triggers after a item is checked?
Which CheckedListBox event triggers after a item is checked?
(answer #3)
softburger states:
I tried this and it worked:
(seems to work for me, too)
private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox clb = (CheckedListBox)sender;
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
// Now you can go further
CallExternalRoutine();
}
The idea is, as mentioned in many posts, CheckedListBox has an ItemCheck event, but no ItemChecked event.
To get around this,
the ItemCheck handler assignment is briefly suspended(within the ItemCheck handler routine itself (!?)),
during which time the CheckedListBox's SetItemCheckState method is invoked for the newly checked item, (which should place the item in the CheckedListBox's CheckedItems collection)
and then the ItemCheck handler is then reassigned.
i.e.
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
and now you can (finally) get all theCheckedListBox Checked Items from its CheckedItems collection. (great hack, if you ask me)
Related
I'd like to disable item in ComboBox that is in a DataGridview cell.
I already know how to disable(or seems disabled) items in a ComboBox, using the DrawItem event and SelectedIndexChanged event but there is no similar event in DataGridViewComboBoxCell or DataGridViewComboBoxColumn.
So my question is, how to disable any item in ComboBox that is in a DataGridView?
In ComboBox I can modify items display that need to be disabled like this:
But can't do the same functionality in DataGridView:
I think the simplest option for you would be to handle the EditControlShowing event, and then handle the ComboBoxes SelectedIndexChanged event and do what you already know how to do.
When you setup the DataGridview in code, you can do this:
dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
And then implement the handler like:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Both of these lines are essential, otherwise you will be handling the same event twice in some conditions
combo.SelectedIndexChanged -= combo_SelectedIndexChanged;
combo.SelectedIndexChanged += combo_SelectedIndexChanged;
}
}
Finally, the SelectedIndexChanged event is handled exactly the way you want to:
void combo_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox thisCombo = sender as ComboBox;
if (thisCombo != null)
{
Debug.Print(thisCombo.Text);
}
}
I have a CheckedListBox. I want to know when the user checked or unchecked an item. I tried using ItemCheck event but it fires even when an item is programmatically checked. How can I detect this?
Using the ItemCheck event handler is the correct method for detecting when the user ticks or un-ticks an item in the CheckedListBox. And yes, it will also fire when the item is checked/unchecked programmitically.
If you don't want the event fired when you set/unset items programmatically, you should remove the event handler before hand.
Assuming your event handler looks like this:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Debug.Print("Checked");
}
else if (e.NewValue == CheckState.Unchecked)
{
Debug.Print("Un-Checked");
}
}
Before you set/unset items programmatically, you should add the line:
this.checkedListBox1.ItemCheck -= this.checkedListBox1_ItemCheck;
and after the items have been set/unset you in code, re-add the event handler with:
this.checkedListBox1.ItemCheck += this.checkedListBox1_ItemCheck;
I have two listboxes: listBox1, listBox2.
If i select the item in first listBox1, item of the same index must be automatically selected in listBox2.
So, If i select item 1 in listbox1 then, item 1 selected automatically in listbox2 and so on.
Not: I found some examples but not work.
private void listBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
{ listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex; }
Edit:
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Here's a sample that you may want to explore more, try to add ListBoxto your form (in this sample 3 listboxes) it should look like the following:
And here's the source that would select the same index whenever you click on an item on it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeListBoxes();
}
private void InitializeListBoxes()
{
//Populate listboxes
listBox1.Items.Add("Apple");
listBox1.Items.Add("Orange");
listBox1.Items.Add("Mango");
listBox2.Items.Add("Milk");
listBox2.Items.Add("Cheese");
listBox2.Items.Add("Butter");
listBox3.Items.Add("Coffee");
listBox3.Items.Add("Cream");
listBox3.Items.Add("Sugar");
//Subscribe to same events
listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged;
}
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
listBox1.SelectedIndex = listBox.SelectedIndex;
listBox2.SelectedIndex = listBox.SelectedIndex;
listBox3.SelectedIndex = listBox.SelectedIndex;
}
}
What happens is on the InitializeListBoxes you subscribe to the same event which would trigger the SelectedIndexChanged event, and select appropriate item from each of the ListBox.
to solve your problem, you can use a pattern called Observer: https://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx
Basically, you will have to create a notifier method in the listboxes that you want to notify. When you select an item in listBox1, you will call the notifier method of listBox2.
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Fastest and easiest way can be via MouseDown event:
private void lstBoxes_MouseDown(object sender, MouseEventArgs e)
{
ListBox lstBox = (ListBox)sender;
lstBx1.SelectedIndex = lstBox.SelectedIndex;
lstBx2.SelectedIndex = lstBox.SelectedIndex;
lstBx3.SelectedIndex = lstBox.SelectedIndex;
}
i understand listbox.selectedItem will get me the current item thats seletected in a listbox... how do i get if the current item has been changed. so i want to know if the user's selection has changed from what it was to a different item.
Subscribe to the SelectedIndexChanged event like this:
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
The listener is:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Add code in the listener above to react to the event.
You can listen for SelectedIndexChanged event, this is Windows.Forms, naturally.
I want to handle the event when a value is changed in a ComboBox in a DataGridView cell.
There's the CellValueChanged event, but that one doesn't fire until I click somewhere else inside the DataGridView.
A simple ComboBox SelectedValueChanged does fire immediately after a new value is selected.
How can I add a listener to the combobox that's inside the cell?
The above answer led me down the primrose path for awhile. It does not work as it causes multiple events to fire and just keeps adding events. The problem is that the above catches the DataGridViewEditingControlShowingEvent and it does not catch the value changed. So it will fire every time you focus then leave the combobox whether it has changed or not.
The last answer about CurrentCellDirtyStateChanged is the right way to go. I hope this helps someone avoid going down a rabbit hole.
Here is some code:
// Add the events to listen for
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
// This fires the cell value changed handler below
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// My combobox column is the second one so I hard coded a 1, flavor to taste
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1];
if (cb.Value != null)
{
// do stuff
dataGridView1.Invalidate();
}
}
You can also handle the CurrentCellDirtyStateChanged event which gets called whenever a value is changed, even if it's not commited. To get the selected value in the list, you would do something like:
var newValue = dataGridView.CurrentCell.EditedFormattedValue;
This is the code, which will fire the event of the selection in the comboBox in the dataGridView:
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
cmbcolumn.Name = "cmbColumn";
cmbcolumn.HeaderText = "combobox column";
cmbcolumn.Items.AddRange(new string[] { "aa", "ac", "aacc" });
dataGridView1.Columns.Add(cmbcolumn);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item != null)
MessageBox.Show(item);
}
I have implemented another solution, that seems more responsive (e.g.. quicker and less clicks) than Mitja Bonca's above. Although sometimes the combobox closes to quickly. This uses the CurrentCellDirtyStateChanged and CellMouseDown callback
private void myGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (myGrid.CurrentCell is DataGridViewComboBoxCell)
{
myGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
myGrid.EndEdit();
}
}
private void myGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (myGrid.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewComboBoxCell)
{
myGrid.CurrentCell = myGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
myGrid.BeginEdit(true);
((ComboBox)myGrid.EditingControl).DroppedDown = true; // Tell combobox to expand
}
}
ComboBox cmbBox = (ComboBox)sender;
MessageBox.Show(cmbBox.SelectedValue.ToString());