One Method fired from different controls

crewchiefpro

New member
Joined
Dec 21, 2016
Messages
4
Location
Illinois and San Diego
Programming Experience
10+
I will warn you upfront, I am a 25 year developer of FoxPro and Visual FoxPro.

In my forms I could create a Method that I could fire from any other control's events. Didn't matter if it was a Click() Event, DoubleClick(), Valid() etc.

I am new to C# forms. Is there a way to call a method from another control or create a single method that can be called similar to what I am used to?


Don Higgins
Crew Chief Pro Drag Racing Software
 
If you want to do this in the designer, you can select one or more controls, open the Properties window, click the Events button and then double-click the event of interest. That will generate a method and register it as a handler for that event of all the selected controls. You can also select one or more controls and then use the drop-down for an event in the Properties window to select an existing method with the appropriate signature as a handler for an event.

If you want to do this in code, you can type the control name and field name followed by the '+=' operator (and a space, I think) and Intellisense will offer to auto-complete with an appropriate method name, then offer to create the method. You can then register that same method as a handler for other events using the same '+=' operator. Note that the designer option above simply generates similar code in the designer code file.

Just note that you won't necessarily be able to use the same method for every event. The 'e' parameter is a different type for different events. For instance, the Validating event gets a CancelEventArgs while the Click event just gets an EventArgs. If you don't need to access any event-specific members of the 'e' parameter then you can just use a method where 'e' is declared as type EventArgs.
 
Wow, didn't know it was this involved. In VFP we just put this in whatever event we wanted:

ThisForm.CalculateET()

We created ONE method called CalculateET for example and put whatever code we wanted in this method.

I just thought a new language could do something that a 25 year old language could....
 
Wow, didn't know it was this involved.
It's not involved. It's very simple. It takes a second or two in the designer and probably less in code. It just sounds involved when written out fully.
In VFP we just put this in whatever event we wanted:

ThisForm.CalculateET()

We created ONE method called CalculateET for example and put whatever code we wanted in this method.
You can write a method, put whatever you want in it and call it in C# too. That method is not going to be invoked by magic when you click a Button or the like though. You have to actually tell the application to invoke the method when a particular event is raised. You can do that with a couple of clicks or a few keystrokes. Hardly onerous.
I just thought a new language could do something that a 25 year old language could....
It can, and it can do a few more things besides. It won't necessarily do everything in the same way as that 25 year old language though, partly simply because it's different and partly to facilitate other features.

Let me be upfront and say that I'm not a fan of people moaning about some language they're learning not being the same as some other language they already know. If you are interested in learning C# then I'm happy to help you do so. It is what it is and does what it does. If you prefer VFP or something else then by all means use it but I, for one, have no real interest in contrasting the two here. If you do then I may not stick around.
 
OK thanks for the info. I meant no disrespect and just wanted to explain the ways in which I was used to calling one method from various places. I know that C# is more powerful and there will be a learning curve.

So back to the original question what code do I write in an event to fire a method. For example if I was calculating something that needed to update every time Enter or Tab was pressed in one of the fields that controlled the calculation. Can I setup a method and call it like This.CalculateET();

The books and beginner classes don't really cover much of the object oriented stuff.

Next Question, is C# what I should be learning if I will be porting my racing software to PCs, Tablets, and Phones?
 
crewchiefpro;7183So back to the original question what code do I write in an event to fire a method.[/QUOTE said:
You don't write code in an event. Like many, I think that you're confusing an event and an event handler. Consider a Button in a form. You create a handler for the Button's Click event in the form. The event handler is a method and it's a member of the form. The Button raises its Click event and the form's method, i.e. the event handler, is executed. Any work you want done when the Button is clicked goes in that event handler. If you want to call another method then do so. If you want to get or set a property then do so. If you want to perform a calculation then do so.
For example if I was calculating something that needed to update every time Enter or Tab was pressed in one of the fields that controlled the calculation. Can I setup a method and call it like This.CalculateET();
If you want to do something when a keyboard key is pressed then you would handle the PreviewKeyDown, KeyDown, KeyPress or KeyUp event of the appropriate control. Which event you choose depends on the specifics. KeyPress relates to characters while the others relate to keyboard keys. I'll let you read about the others to determine the differences.

If you want to do the same thing for keys or characters regardless of which control has focus then you can handle the event of the form instead of individual controls. Just be sure to set the form's KeyPreview property to true.
Next Question, is C# what I should be learning if I will be porting my racing software to PCs, Tablets, and Phones?
For future reference, please start a new thread for a question on a new topic. One thread per topic and one topic per thread. Now that your question is here though, the answer that C# is a good choice for that. It can be used to write web applications, Windows applications and apps for Windows 10, Windows 10 Mobile and also Android and iOS.
 
Back
Top Bottom