React to a checkbox has changed its state in ObjectListView - c#

I have an ObjectListView (OLV) with 3 columns of checkboxes that enables and disables different calculations. Some react to the object in the OLV object but one checkbox column should also activate some recalculations outside the OLV.
I have looked in the cookbook, but I cant find any solution to my problem. The OLV does have CheckStateGetter and CheckStatePutter methods, but these are used during the change. I need to react after it has been changed. I also looked for a general purpose event like cellEdit, but a checkbox click is not an edit event.
Anyone know how to listen for checkbox changes after it has been done in ObjectListView?

It is not entirely clear to me what you are asking but maybe this helps.
objectListView1.SubItemChecking += delegate(object sender, SubItemCheckingEventArgs args) {
// The event arguments contain information about current check state, new check state, the source column and so on...
};
"one checkbox column should also activate some recalculations outside the OLV."
So check if the SubItemChecking source in the event arguments is the column of interest and you should get what you need.

Related

Handle multiple controls methods at once

I'm working on a form and I need to change the behaviour of some controls when some actions are given by the user. Since there are many controls to handle, meaning buttons, comboboxes, and others, do you know how I can handle methods for all those without handling one by one? Like a loop or something.
Like, for example, for comboboxes I need to overwrite the drawItem event with some code to change their appearance.
I can't find anything on google.
I did it, in the end. I set for the many comboboxes the same event handler/method, selecting all the comboboxes and then setting the handler name in the properties/events tab (in my case, in the DrawItem handler).
Then in the code the method comes by default with (object sender, DrawItemEventArgs e) so I set:
var combo = sender as ComboBox;
and with it I can generally get or set what I need

Winforms: Which event to fire? combobox.selectedindexchanged OR bindingsource.currentchanged

Assume we have a Combobox in a winforms application which its items bound to a Bindingsource.
I need to fire an event when the user changes the selected item in the combobox.
Should I handle the combobox.selectedindexchanged event or bindingsource.currentchanged event. Which is better in case of performance or anything else?
I've searched a lot to find an article or something about it, but can't find something straight and clear. I appreciate any suggestion or workaround.
UPDATE
I need to call a function base on the selected object ID after user select an Item from combobox. (ID is accessible from both Combobox1.SelectedValue and bindingSource1.Current.ID). In this case which event shouldI choose?
If you wish to handle event when end user selects any item in UI then you should go with combobox.selectedindexchanged because bindingsource.currentchanged can be triggered for number reasons such as mentioned here on MSDN blog, BindingSource.CurrentChanged Event so in case, you need to handle an event for any of the reason mentioned in MSDN then it will unnecessarily go through logic you might have coded for selection change event. Your code should be specific, while handling events.
If a ComboBox is bound to a BindingSource you usually leave it at that. If you do need to tap into events you're better off creating more properties in the Business Object. For example say you want to disable a button if the combobox is Index Value == 0, simply create a property in the Business Object that the Enabled property of the Button is bound to, eg:
public virtual bool IsFunctionEnabled
{
get { return (An_Items_SelectedIndex > 0); }
}
If you really need to do stuff in the ComboBox's selected index change event I would lean towards doing it in the Presentation Tier as I don't recommend mucking with binding source controls or their events.
The best solution is handling everything in your Business Objects and binding your controls via BindingSources. Doing any logic in the Presentation Tier makes it hard to test and changing anything in the BindingSources add a lot of testing.
After all, I am getting to know it should be better to use Combobox.SelectedIndexChanded event, because I am interacting with a user and looking for a response from UI. Although here the events do the same for me, but Bindingsource.CurrentChanged event can be used in case I wanted to track the current object changes from anywhere like a list change or something, not exactly the UI. Its better to use combobox events here I believe.

How to run a code before fired change check in a checkBox in WPF?

I mean, I've got a checkbox, and when it click on it.. I need to run a function to verify if can change IsChecked value or not.
It seems like a validation.
You can use the Checked (UnChecked/CheckedChanged) event(s) of the checkbox (depending on if its a WPF or a WinForms application):
CheckBox cb;
...
//WPF:
cb.Checked += (sender,e) => { if (!..check if new state is valid..) cb.Checked = !cb.Checked) };
cb.UnChecked += ...
//WinForms:
cb.CheckedChanged += ...
If the new checked state is not valid, it (un)checks the checkbox again and puts it in a valid state.
Another (and better) approach would be to disable the checkbox if a certain condition is not met (like a state of another control). It is confusing for the user if he or she checks a box and it's state is not changing without any feedback.
See MSDN for WPF / MSDN for WinForms for further informations.
The more WPF approach would be:
Define a binding on Checked property of checkbox
Assign binding Converter
In converter decide either the return value of binded boolean property have to be True or False
If you use WPF force you do not jump into "windows forms" like solutions, as much as it possible (depends on time you have, performance impact, complexity ...). Try to follow WPF rules.
The best approach (MVVM) would be to bind the Click event on the check box to a Command on the view model. This would allow you to intercept the check event and also disable the ability based on business logic. Please see my example post here on command binding.
http://tsells.wordpress.com/2010/06/23/command-binding-with-wpf-and-silverlight-net-4-0-with-m-v-vm/

Cancelling TextChanged events (ASP.NET)

I have a bit of an issue with something I'm working on in ASP.NET/C#. I am using a GridView to display all 'items' linked to an account, with 3 fields relating to the link, but also all files not linked to it. Next to all records is a checkbox so they can link/unlink the two, and modify the 3 fields relating to that link.
I am currently using the 'CheckedChanged' and 'TextChanged' events to update the entries, as opposed to looping through each row in the Grid, checking its current state and updating accordingly etc which would provide a lot of overhead.
Problem is,the events fire if the user clicks a 'Cancel' button. Is there a way to detect if this certain button has been Clicked, and stop the events from firing? Or do you guys have better ideas?
First of all, it sounds like your Cancel button's Clicked handler is assigned to your custom function somewhere. By default it shouldn't do anything.
However, you can use the Sender object to check the type of object that fired the event:
if (sender is Checkbox) {
// Do something useful
}
I have not tested this code.

What do I do If the value of a datagridviewcombobox is not in the datagridviewcomboboxcolumn datasource?

I've got a situation where the drop down list has to contain only 'active' objects. However this doesn't mean the current combobox value will be in the list (it was selected at a time that object was active, but is not active anymore).
As things are now, there's an exception thrown.. I think in the dataerror handler of the datagridview, that says the value is not in the list.
What I need to do is find some event that is fired when each datagridview row is populated, so I can check and see if the combobox value is in the combobox datasource... if not, I'd add it. That's the only solution I can see. But the question is, which event handler should I use for this?
Preferably, I'd like to be able to generalize this check and put it in a class that inherits from datagridview. This way, I won't have to worry about this problem ever again.
Any ideas?
Isaac
You could try the DataBindingComplete event. Other options include DataMemberChanged and DataSourceChanged.
It's a bit hard to tell which event would be the most appropriate without seeing some code.

Categories