Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force ComboBox UserChanged event.

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
0
0
GB
I have a comboBox on one of my forms and when I set the text of the comboBox from another method it does not fire the UserChange event.

Is there any way I can force this event to fire so that the component_UserChange method will be invoked?



Thanks for any help.
 
Put the code of the change event in a new void statement then call from both events?
 
I have gotten it working by explicitly callin the UserChanged event method like this:

Code:
this.combobox_UserChanged(combobox, null)

I'm not sure if it's OK to have System.event.arg set to null though?

Thanks
 
Are you doing a derived combobox and added a UserChanged event, and you want to know how to trigger UserChanged event?
 
Basically yes. The UserChanged event is fired if the user selects a new item from the drop down. Based on this selection other ares of the screen are loaded with new data. However, there is a part of my application where I pass in the value of the combobox and have to do:

Code:
combobox.setText("....");

but this does not fire the UserChanged event so I want to explicitly call it. For now I have done as above:

Code:
this.combobox_UserChanged(combobox, null)

Thanks
 
Inside setText() method, just raise the UserChanged event again the same way you fired it on the overriden SelectedIndex/Value events (or some Selected-X event).

But if your design of the combobox does not really need to raise the UserChanged event, your solution suffices. And, about the event args param, if the type is System.EventArgs, yes it's safe to pass null, (try also EventArgs.Empty).
 
It's ok to pass null?

What if you are using the eventargs in your method, because you assume they are automatically given?

In your case, you will probably be ok. What you should really be doing though, is breaking your event code out into a different method. Then your event and other locations of code can call the method, not the event's method!
 
JurkMonkey raised good points about event args. However, based on your code, which is likely to have been implemented on the client (eg. Form)
Code:
this.combobox_UserChanged(combobox, null)
I suppose you do not need the event argument. I was only saying it's okay to pass null because it will compile and run.

I did mention...
And, about the event args param, if the type is System.EventArgs, yes it's safe to pass null, (try also EventArgs.Empty).
Looking at .Net convention, most event handlers with System.EvenArgs type of event args are so because they do not need to pass any information back to the client, eg. Button.Click, Form.Load. But, events that do need to send back some pieces of information subclass System.EventArgs to strongly-typed ones and add the additional information, like MouseEventHandler for Control.MouseX events, DataRowChangedEventHandler for DataTable.RowChanged event.

(However, this may not be true if the control passes an strongly-typed/subclassed EventArgs despite the event arg being a System.EventArgs, aka polymorphism. Very inconvenient though).

Seeing that you used the System.EventArgs as the data type for UserChanged event arg, based on the above convention, and the control I assume is intended for your application only, I stated (my opinion only) that it's safe to pass null or System.EventArgs.Empty.

My 2cents. Hope this helps. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top