User checked item in CheckedListBox - c#

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;

Related

Disable item in Combobox in DataGridViewComboBoxCell

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

Bind ComboBox Dynamically with CheckedListBox Items?

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)

Item realized event does not fire

Quick question about the Item realization event in WP8.
Here is my event registration which I call in the ctor for the View.
EpisodeList.ItemRealized += EpisodeList_ItemRealized;
Also here is my EventHandler
private void EpisodeList_ItemRealized(object sender, ItemRealizationEventArgs e)
{
if (!vm.Loading && EpisodeList.ItemsSource != null && EpisodeList.ItemsSource.Count >= _offset)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
if ((e.Container.Content as Medium).Equals(EpisodeList.ItemsSource[EpisodeList.ItemsSource.Count - _offset]))
{
//Ask Messenger to notify the ViewModel To Load More Items
Messenger.Default.Send<MainPageLoadMoreEpisodesMessage>(new MainPageLoadMoreEpisodesMessage());
}
}
}
}
My problem is that the event handler fires a few times but then it never fires again, I have no clue why I event tried to register the event handler again after loading was complete, I am unable to get the event handler to fire again.
LongListSelctor Item Realized & Item Unrealized event fires when longlistselector scroll down or top, it take 20 element to realize at once, to fire it again it need more than 20 item in list. It don't need to register twice.

c# how do i get check if a selected item in a listbox has changed. ?

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.

In C# WPF, why is my TabControl's SelectionChanged event firing too often?

I have a tabbed GUI with each tab containing a Frame. In one of these Frames there is a DataGrid. When the user selects this tab, I need my datagrid sorted, so I'm using the TabControl SelectionChanged event to trigger the sort. However, this event triggers every time an item is selected from the DataGrid, even though the tabs themselves remain untouched.
I've tried number of different events:
GotFocus for a TabItem
RequestBringIntoView for a TabItem
but they all seem to suffer from this problem. What is causing this?
The TabControl.SelectionChanged is the same event as a ComboBox.SelectionChanged
It originates from Selector.SelectionChanged.
So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl, which is causing this "firing too often" issue.
Mark your event as handled in your SelectionChanged of your ComboBox/ListBox/ListView/any other Selector you use in your DataGrid like so:
private void MyComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = true;
}
And this inconvenience will go away ;).
private void tabControlName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl) //if this event fired from TabControl then enter
{
if (tabItemName.IsSelected)
{
//Do your job here
}
}
}
If you have added a handler with AddHandler in a parent element, all selection changes will fire the SelectionChanged-event. In this case, you can give your TabControl a name and then check in the EventHandler if the name of the OriginalSource is the name of your TabControl.
Another good approch is adding a handler to the tabControl.Items.SelectionChanged:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ItemCollection view = tabControl.Items;
view.CurrentChanged += new EventHandler(view_CurrentChanged);
}
void view_CurrentChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Maybe is not the xamly way, but is less pain as it only fires when an item is changed.

Categories

Resources