473,804 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call an method that is an eventhandler

jac
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code twice.
tkx
jac
Nov 17 '05 #1
13 9787
Just to make sure I understand you... somewhere in your code you’ve got
something to the effect of:

this.comboBox1. SelectedIndexCh anged += new
System.EventHan dler(this.combo Box1_SelectedIn dexChanged);

setting up an EventHandler for when SelectedIndexCh anged is fired... and you
are curious if you can call your equivalent of comboBox1_Selec tedIndexChanged
on your own? Absolutely!

Two things to take note of... first up, the first argument passed into an
event handler is a reference to the sender... if you are not using this
reference you can simply pass in null, otherwise be sure to pass in a
reference to the ComboBox in question.

Second... EventArgs. Some handlers use them, some don’t, if yours doesn’t
you can pass in null or EventArgs.Empty .

Brendan
"jac" wrote:
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code twice.
tkx
jac

Nov 17 '05 #2
jac,

You could just call the event handler like a normal function.

Or are you saying you want to call all the event handlers attached to
that control? If that is the case, you can't do it, as only objects can
fire their own events.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"jac" <ja*@discussion s.microsoft.com > wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code
twice.
tkx
jac

Nov 17 '05 #3
Yes, it's possible as long as you provide the expected parameters (usually
sender and EventArgs). However, it would be cleaner to put the relevant code
in a separate method and call that method from within the event handler
rather than putting the code in the event handler itself. That way, you
could simply call that method from anywhere in your code.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"jac" <ja*@discussion s.microsoft.com > wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code
twice.
tkx
jac

Nov 17 '05 #4
Hi,

Yes you can, just call combo_selectedi ndexchanged ( null, null );
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"jac" <ja*@discussion s.microsoft.com > wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code
twice.
tkx
jac

Nov 17 '05 #5
Generally speaking, and for good design, you should not call an Event
Handler directly. Instead, create a method that the Event Handler can call,
as well as any other method that needs it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"jac" <ja*@discussion s.microsoft.com > wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code
twice.
tkx
jac

Nov 17 '05 #6
Hi,
Why it's not?
An "event handler" is just a method as any other, in the same way that you
can have the same method hooked to more than one controls you can call it
yourself,
IMO that is the reason why the sender parameter is there in the first
place, so the method can have a reference to who called it.

Having another method that both the event handler and your code call to
perform the work do have sense IF the logic is different in any case,
otherwise I can't see how it makes it clearer.You are only removing the
parameters and you end with two methods:

void control_handle( ... )
{
do_the_real_wor k();
}

void do_the_real_wor k(){}
IMO that is not clearer at all.
Now, in 2.0 with anonymous methods it's a complete different history, you
can declare the control_handle as a anonymous method and the code will be
clear indeed.
What you folks think about the above?
Do you write two methods like the above?
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:Oc******** ******@tk2msftn gp13.phx.gbl...
Generally speaking, and for good design, you should not call an Event
Handler directly. Instead, create a method that the Event Handler can
call, as well as any other method that needs it.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"jac" <ja*@discussion s.microsoft.com > wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexch anged.

But somewhere in my code want to do excecute the same code that is
writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexch anged on the form). So, I don't have to call that code
twice.
tkx
jac


Nov 17 '05 #7

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** **********@TK2M SFTNGP14.phx.gb l...
IMO that is the reason why the sender parameter is there in the first
place, so the method can have a reference to who called it.
The "sender" parameter is there so you know which *component* fired the
event. This is so that multiple *components* can share a single event
handler.
Having another method that both the event handler and your code call to
perform the work do have sense IF the logic is different in any case,
otherwise I can't see how it makes it clearer.You are only removing the
parameters and you end with two methods:

void control_handle( ... )
{
do_the_real_wor k();
}

void do_the_real_wor k(){}
IMO that is not clearer at all.
IMO, you should never code "real work" in an event handler - period. All
"real work" belongs in a method that is not inside the GUI in the first
place.
What you folks think about the above?
Do you write two methods like the above?


Yes. Two methods.
Nov 17 '05 #8
Scott Roberts wrote:

<snip>
IMO, you should never code "real work" in an event handler - period. All
"real work" belongs in a method that is not inside the GUI in the first
place.


You're making three assumptions which may be invalid:

1) That the event being handled is a GUI event
2) That the real work is not GUI work (if it's GUI work, it's
reasonable for it to be done in the GUI code)
3) That the event handler is defined in GUI code (it may not be,
even if it happens to be hooked up to a GUI event)

Jon

Nov 17 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Scott Roberts wrote:

<snip>
IMO, you should never code "real work" in an event handler - period. All
"real work" belongs in a method that is not inside the GUI in the first
place.


You're making three assumptions which may be invalid:

1) That the event being handled is a GUI event
2) That the real work is not GUI work (if it's GUI work, it's
reasonable for it to be done in the GUI code)
3) That the event handler is defined in GUI code (it may not be,
even if it happens to be hooked up to a GUI event)


The original post was related to combox events. In general, you are correct.
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11881
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class MyButton:System.Windows.Form.UserControl
1
2663
by: Jim Witt | last post by:
Ref: Win2000, .NET 1.1 I want to place series of buttons on a form, my own button class. Would like to create descriptor array to describe buttons - each needs: text, x and y position, and method invoked when button clicked. I've been able to create descriptor with 'text, x, y' just fine (no brainer). However, I designate button click method as follows: this.button.Click += System.EventHandler(MyMethod);
5
4313
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the exe. The button clicking part is working fine, but I just cannot get the method call. Here is my program snippet. **************************************************************
12
13152
by: ritchiebuckley | last post by:
I've been doing asp.net for so long now that I have forgotten about windows form programming so excuse this (seemingly) basic question. I have a form A which then opens up form B. I do stuff on form B after which I hit a close button. I want to run a method on form A as I close form B. I don't need to pass data back (though I like to know how this is done), just run a refresh type method back on form B. I've got bogged down in...
10
8212
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will obviously change once the program's fully built to actually do something, but for testing, it works). The only code I added to the service is below:
1
9082
by: Sagaert Johan | last post by:
Hi I use an ocx on my form ,when i use a buttons click event to call a method on the ocx i have no problem. I made a Socket communication class that invokes an eventhandler through a delegate. If i call the ocx method in that handler i get that error.
2
1054
by: NET_NET_2003 | last post by:
Hi, I have programmed a module, but I have a small problem. I need to call a method but after the data has been sent to the client (or when the client is disconnected). Which event can I use? and is this possible? Thanx
3
4036
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
3
1509
by: =?Utf-8?B?TW9oYW4gQmFidSBE?= | last post by:
Hi, Here is my problem. I am calling a web service asynchronously as follows Registred the event handler as follows _Data.GetDataCompleted += new MyNameSpace.GetDataCompleted EventHandler(GetControlDataCompleted);
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.