Sign in to follow this  
Instigator

C# and Events

Recommended Posts

Instigator    150
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]

Share this post


Link to post
Share on other sites
Telastyn    3777
(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).

Share this post


Link to post
Share on other sites
savagemonitor    100
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.

Share this post


Link to post
Share on other sites
Instigator    150
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this