Combobox selectedindex changed event is getting invoked even when index is not changed - selectedindexchanged

I have few items in combobox, i am selecting one item in that, selectindexchanged event is getting invoked. Then i am clicking on it and selecting the same item ( index is not getting changed) , but still selectedindexchanged event is getting invoked. Do we have any ways to avoid this? C# .Net ( WinForms)

Did you try debugging it to see where (if) the selected index has changed?
Else you can do a check in the event handler to see whether it's the same item selected or not, which you would do nothing if it is the same one.

Related

Datagridview checkboxes lose checks

In my Winforms app, I have a form that contains a datagridview with a checkbox column. If I check one or more checkbox items, then make a change to some other control on the form (outside of the datagridview), the checkboxes become unchecked. Any help would be appreciated. Thanks.
More details: The underlying binding for the checked items is working correctly, as the values are being stored and the checked states are correct when the form reloads. This is just a visual issue.
Furthermore, the checkbox value is never programmatically set to false in the code. False values only come from the binding or from being manually unchecked.
Redesigned the binding and now it works fine. Was previously maintaining a list of selections in the business object and checking checkboxes based on the selections. Now maintaining a "selected" property in the business object and binding appropriately.
Problem manifested as described due to nature of events generated by checkbox editing (apparently). Binding source change event not triggered when clicking the checkbox (since it was not bound) - only occurred after editing complete on another control.
Could you do an "Invalidate()" on the column to see if a redraw changes anything? Without code, we need to divide the problem arbitrarily.

Reset a ComboBox

I have a view that contains several ComboBox elements. For some reason old data remains from the previous time the view was opened and I would like to reset the combobox elements in the view every time it is opened. Is there a function that could that for me? I want it to be exactly how it is as if I rendered it the first time with the initial items. Would using setSelectedItem(vItem), setSelectedItemId(vItem), setSelectedKey(sKey), setShowSecondaryValues() help? If so, what do these keywords mean (selectedItem, selectedItemID, selectedKey, secondaryValues)?
Unfortunately you do not provide an example. Normally you bind your UI controls against a model, e.g. JSONModel. In this case the items of your ComboBox controls would be taken from the corresponding model. However, you can use method removeAllItems to achieve the desired behaviour.
UPDATE: Obviously the controls are bound and only the selection should be cleared.
Use setSelectedItem with value null to clear the selection. You could also use the binding to set the selected item automatically by using the selectedKey attribute, see example.

Have TreeView update its SelectedItem when programmatically changed

In WPF I have a TreeView control where a particular item can be selected either by the user selecting the item directly in the tree view or by clicking on a screen control. The tree view is displaying a list of elements that are being displayed on a user defined form, basically a form designer application.
Here is my problem. When the user clicks on a screen control it calls a method that returns the TreeViewItem that represents the element. It then sets the IsSelected property to true for this element. It correctly changes the visual indicator in the TreeView and it raised the SelectedItemChanged event in the TreeView. This is all good.
However, it appears that somewhere behind the scenes the TreeView still thinks the previous item is selected. Here is why I have come to this conclusion. If I select ElementA by clicking on it in the TreeView is it correctly selected. If I then select ElementB by clicking on the screen control and programmatically setting the IsSelected property for the ElementB TreeViewItem it appears to have selected it correctly. Now if I select ElementA again by clicking on it in the TreeView it does nothing. The SelectedItemChanged event is not raised and the reverse selection box that indicates the selected item stays on ElementB. If I click on ElementB in the TreeView it also does not raise the SelectedItemChanged event, however it does not appear to update the internal flag since if I then click on ElementA on the TreeView it processes it correctly and raises the event.
The only workaround that I have found for this is in the SelectedItemChanged event handler to call the Focus method for the now selected TreeViewItem. If I do this I get the expected behaviour when I select screen controls and programmatically change the selected TreeViewItem.
This is not an acceptable solution though as it creates focus change flicker. When I select items on my form window the focus goes to the TreeView control and then back to the form, causing flicker and slight delay.
Anyone have any indeas.
Update
As requested here is some code. Here is my method of my Explorer window which is the manager of the TreeView in question.
public bool SelectItemByName(String controlName)
{
bool fReturn = false;
TreeViewItem tviToSelect = FindItemByName(_tviMaster, controlName);
if (tviToSelect != null && _tviSelectedItem != tviToSelect)
{
tviToSelect.IsSelected = true;
// Make sure the selected item is visible in the TreeView by expanding all of the parent nodes
ExpandAllParents(tviToSelect);
tviToSelect.BringIntoView();
fReturn = true;
}
return fReturn;
}
Every element has a unique identifier that I use as a cross reference between different areas of the interface. When you click a screen control it uses its identifier to find the cooresponding TreeViewItem in the TreeView. Then this code sets it as selected.
Then in my SelectedItemChanged event handler I had to include the following line.
_tviSelectedItem.Focus();
This fixes my initial issue but introducing the unwant screen flicker.
To recap, I select ElementA in the TreeView directly, then select one or more other elements in the form designer which in turn calls SelectItemByName to programatically set the selected item. All visual indicators show that this worked. In the TreeView the highlighted item changes to the new item that is selected. After selecting any number of elements through the form designer interface if you select ElementA by clicking on it directly in the TreeView it does nothing. It does not get highlighted and it does not fire the SelectedItemChanged event. If you inspect the SelectedItem and SelectedValue properties of the TreeView they all correctly coorespond to the item that was programmatically selected. However, the control somewhere appears to think that ElementA is still selected and doesn't recognize that the selection is changing.
I cannot believe that other people haven't run into this. It appears to be a significant flaw in the TreeView contol in WPF. Not sure if WinForms has the same issue or not.
Each TreeViewItem has an IsSelected property, and I suspect the old one isn't getting set to false. Try setting it to false whenever you set the new item to true.
var currentItem = treeView.ItemContainerGenerator
.ContainerFromItem(treeView.SelectedItem) as TreeViewItem;
currentItem.IsSelected = false;
If that doesn't work, try setting focus on the newly selected item at the same time as when you select it. Don't forget that WPF also has two focus scopes: Logical Focus and Keyboard Focus. You may need to set both.
treeViewItem.Focus(); // Sets Logical Focus
Keyboard.Focus(treeViewItem); // Sets Keyboard Focus

Problem with combobox dropdown

I have a problem regarding combobox dropdown. Once a dropdown is opened, if I want to move focus to other control (say a textbox), I need to click twice because on first click, the combo dropdown is closed and then on second click, the textbox gets focus. How should I fix this? Please help.
You could listen to the DropDownList.SelectedIndexChanged event, and in the event handler set focus to the next control, either by setting TextBox.Focus(), or by calling System.Windows.Forms.Control.SelectNextControl()
I think this would be 'non standard' behaviour for what its worth. Its quite normal to expect the user to tab or select the next control after using a drop down.
Edit: Sorry, in a WPF ComboBox the equivalent event is SelectionChanged, but on reflection you'd be better using OnDropDownClosed. This would mean you only move the focus specifically after using the drop down rather than just whenever the value changes.

Applying a DataGridViewComboBoxCell selection change immediately

When I change a value in a DataGridViewComboBoxCell the new value is not immediately applied until that cell leaves focus.
Is there a way to have the new value applied immediately?
If you handle the EditingControlShowing event on the DataGridView, you can attach an event handler to the underlying ComboBox's SelectedIndexChanged event (or SelectedValueChanged, or any other ComboBox event). It will fire immediately whenever the ComboBox value changes, and you can do whatever you want with the new value.
There's example code for this in the MSDN docs for DataGridViewComboBoxEditingControl.
DataGridView.CommitEdit Method
This might be of some use to you as well. Handle the CurrentCellDirtyStateChanged event, check for Dirty, and Commit the edit. Then you can use the CurrentCell property to access the value that was selected (assuming it was validated).
DataGridView1.EndEdit()
Ignore this text, answer must be at least 30 characters

Resources