473,835 Members | 1,676 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
13 9790
Hi,
"Scott Roberts" <sc***********@ no-spam.intelebill .com> wrote in message
news:Ou******** ******@TK2MSFTN GP14.phx.gbl...

"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.


Yes, that was what I meant, terrible combination of words, gave a
completely different meaning to the sentence.
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.


So for you the handler is just a wrapper to another method?

What if the event invoke some other changes in the form?
cheers,

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


Nov 17 '05 #11
> 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,
At the very least, it makes for code that is difficult to read. The test of
a practice is to imagine what the result would be if it were repeated a
sufficient number of times. In addition, it has parameters which are
irrelvant to anything but the delegate it is assigned to. This also adds
confusion to the code. An event handler's purpose is to handle an event.
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.
No, the purpose of the sender parameter is to match the signature of the
delegate it is assigned to. It is a Microsoft convention to add a sender
parameter to an EventHandler delegate, and sometimes it is a necessity, bot
often it is not. The convention exists for more than one purpose.

A property name may be any token that satisfies the grammatical rules for a
property name. None of those rules specify anything about casing. Camel
Case, Hungarian Notation, lower case, it is not specified in the grammar.
But Microsoft has a set of conventions for naming fields and properties that
they apply strictly throughout the organization. Again, there is a purpose
for this. It makes code easier to read and debug.

The C family of languages specifies nothing about the number and type of
white space characters between statements. You can type a whole slew of them
on the same line if you wish, to say nothing of indentation. But Microsoft
and other companies have strict sets of rules regarding this. Again, why?

If you program long enough, and eat enough of your own (and other people's)
dog food, you will find out. Of course, you could always skip the
indigestion, and take my word for it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** **********@TK2M SFTNGP14.phx.gb l... 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 #12

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:uN******** ******@TK2MSFTN GP14.phx.gbl...
So for you the handler is just a wrapper to another method?
In general, yes. Every time I write an event handler longer than 5 lines of
code I end up wanting to re-use it in other parts of the application (other
forms, etc.). Since an event has a specific signature it's often difficult
to reuse it (e.g. with a different type of component).
What if the event invoke some other changes in the form?


As Jon pointed out, my assumption that "real work" excludes "GUI work" was
wrong. If the "real work" is related to modifying other GUI controls then it
makes sense to have the "RealWorkMethod " in the GUI.
Nov 17 '05 #13
Hi,
"Scott Roberts" <sc***********@ no-spam.intelebill .com> wrote in message
news:Oo******** ******@TK2MSFTN GP12.phx.gbl...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote
in message news:uN******** ******@TK2MSFTN GP14.phx.gbl...
So for you the handler is just a wrapper to another method?


In general, yes. Every time I write an event handler longer than 5 lines
of
code I end up wanting to re-use it in other parts of the application
(other
forms, etc.). Since an event has a specific signature it's often difficult
to reuse it (e.g. with a different type of component).


I generally use the handler when the name of the method as well as the
action being performed is clear to anybody reading the code; and at the
same time this action can be triggered by other means.
What if the event invoke some other changes in the form?


As Jon pointed out, my assumption that "real work" excludes "GUI work" was
wrong. If the "real work" is related to modifying other GUI controls then
it
makes sense to have the "RealWorkMethod " in the GUI.


We are in the same track, I was thinking exclusively in UI more especific
in win apps.
I was talking with a friend of mine about this subject and what is the best
strategy for UI programming so this thread is interesting for that reason, I
can see what other people think and do about it.

Real work usually include both think, UI updates and backend operations (
refresh a client's orders in a listview based on a client being selected in
the combobox).

BTW, one question, what strategy you follow to name the controls in a form?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 17 '05 #14

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
8215
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
9083
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
1055
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
4040
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
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10804
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10517
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
10558
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
9343
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...
0
5631
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...
1
4430
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
3
3086
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.