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
Related
I need your help
as you can see from the screenshot image
I am trying to make a font dialog where i have:
a Label to test the change that will happen when I
click in the checkbox .as you can see I have 3 checkboxs.
the problem is that I cant make all the checkboxs work together
so the text changed to Bold,Italic and underline..help me please
.....
my Form design
i try to use if else statement
and also switch one and still I don't know how to do it that is why i m here
It's a bit unclear, as others have noted, but.. if you are wanting to combine the results of checked and/or unchecked options to display the results of a user's font styling choices?
THEN:
1.) double-click in your V.S. form designer one of the checkboxes, this will create an event handler in your form's C# code file. The function created will be something like checkbox1_checked(object sender, EventArgs e)
2.) In the event handler function that Visual Studio has scaffolded for you, write all the code necessary to consider the .Checked state of all checkbox controls and update your Label1 control appropriately (depending on your goal, this may require custom .NET painting logic that is too much for a beginner to think about)
3.) Go back to the other controls on your design surface, now select them.. keeping an eye on your lower-right 'Properties' panel, (make sure its switched to the 'events' tab - i.e. click the 'lightning bolt' button). Ensure that each of the other radio buttons that do not yet have an event handler get mapped: Find the row in the Properties panel that shows the word "click" (err.. 'checked' event?). When you click into the white space on the row, next to the word 'checked', it will allow you to select the pre-existing "checkbox1_checked" event handler than you created in step 1.
UPDATED:
4.) the end goal is to wire all checkbox controls to the same event handler function in your form's code-behind.. if you double-click each checkbox at design time.. you'll create separate event handlers.. but with some learning (or following my directions above) you can point them all to the same event handler.
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.
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.
How do you block user input (ie. changing the selected index) on a listbox without changing the text colour, like if you were to just set the enabled property to false on the control?
This is for WinForms.
I assume you want a default selection from the ListBox that the user won't be able to change? My favorite approach to this issue is by Disabling the user from accessing the ListBox. You can do this by adding a GotFocus event on the ListBox and setting the Focus to another control whenever the ListBox gets focus. Something like this:
private void listBox1_GotFocus(Object sender, EventArgs e) {
this.Select();//set the form as the active control or even this.Focus();
}
Best option is to not do that -- instead, in this situation, only populate the one item you want displayed/selected.
Otherwise, you'll need to:
- remember the current selection
- handle OnSelectedIndexChanged
- reset the selection (handling potential recursion)
I'm developing a WP7 app and have a listbox with generated buttons all supposed to lead to somewhere specific. I can't figure out how to know which button was pushed at runtime. The list gets generated from a collection of objects with a couple of attributes in each. One of those attributes contain a value that I need to get to be able to know where to send the user.
So my desired process is that the user clicks on an item in the listbox, passing the value of the attribute in the object the button was generated from, to a click handler which sends the user to the right place.
Any suggestions?
I presume your ListBox contains a ItemTemplate which constructs a Button for each of the items bound to your list? if this is the case, within your Click event handler you need to inspect the DataContext of the button that was clicked:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var myObject = btn.DataContext;
}
As an aside, if you are using this for navigation, a ListBox will not give you very good performance. See the following blog post for an alternative:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
Check the sender property of the OnClick event handler for the click handling.
Alternatively you may want to handle the SelectionChanged event of the ListBox and then query the contents of the SelectedItem.