C# and Events

Started by
2 comments, last by Instigator 15 years, 8 months ago
I need to call a method every time the user note.. key word "USER" changes the text of a comboBox. However, if I have a method that changes the text of my combobox(for some other unrelated purpose) it still goes to the EVENT. WHY, is this actually happening? And how do I fix this? I created an event handler for my 2 comboboxes by placing the following in my 'form_load' Event().
cboClientList.TextChanged += new EventHandler(ComboListTextChanged);
cboHouseList.TextChanged += new EventHandler(ComboListTextChanged);


and in my event handler I check to see which combobox called the event like so,

void ComboIndexChanged(Object sender, EventArgs se)
        {
            if ((ComboBox)sender == cboClientList)
            {
             // code for the combobox1 text changed
            {
            if ((ComboBox)sender == cboHouseList)
            {
             // code for combobox2 text changed
            }
        }


This is all good for when a user changes the text of the cboHouseList and cboHouseList objects (by clicking in the combo box and changing the text) BUT again, if I have something like so in another method

cboHouseList.text = ""


the event still gets fired. How can I prevent this command from calling the event handler? Please any help would be greatly appreciated! NOTE: using .net 2.0 [Edited by - Instigator on August 20, 2008 6:20:27 PM]
Advertisement
(to my knowledge) You can't. And why would you want to?

Text being changed should trigger the event every time the text is changed. If you want to trigger an event every time the user edits text, then set the event there (likely some event triggered via a gain/lose focus watcher).
Every solution that I can think of seems bad and convoluted. The easiest thing that I could think of would be to remove the handlers while changing the text programatically then add the handlers back when the changes are done. That way you'd never handle the event when you didn't want to.
Yea okay, I think I've got an idea. Instead of using the textchanged event for the user.. why not use a keypressevent. :)

Thanks for the ideas. And hopefully the turns out okay.

This topic is closed to new replies.

Advertisement