A handler parameter can be used in both events and regular functions. To use a service, a client must connect to the server using ConnectService.
This question already has an answer here:
- Add events to controls added dynamically 2 answers
I have made a button using
along with other code, how can I detect if the created button has been clicked?And make it that if clicked the Form will close?
FroodleFroodle
marked as duplicate by Abdusalam Ben Haj, Omar, Cyclonecode, Steven Penny, jebMar 11 '13 at 0:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4 Answers
Bob HornBob Horn
You need an event handler which will fire when the button is clicked.Here is a quick way -
But it would be better to understand a bit more about buttons, events, etc.
If you use the visual studio UI to create a button and double click the button in design mode, this will create your event and hook it up for you. You can then go to the designer code (the default will be Form1.Designer.cs) where you will find the event:
You will also see a LOT of other information setup for the button, such as location, etc. - which will help you create one the way you want and will improve your understanding of creating UI elements. E.g. a default button gives this on my 2012 machine:
As for closing the Form, it is as easy as putting Close(); within your event handler:
NDJNDJ
if your button is inside your form class:
(might not be exactly
EventHandler
)and in your click method:
If you need to show a message box:
ppetrovppetrov
Create the
Button
and add it to Form.Controls
list to display it on your form:Create the button click method here:
ShaharyarShaharyar
Not the answer you're looking for? Browse other questions tagged c#click or ask your own question.
this is a little theoretical question.Imagine a device full of sensors. Now, in case a sensor x detects something, something should happen. Meanwhile, in case something else is detected, like two sensors detects two different things, then, this device must behave differently.
From webdesign (so javascript) I learned about Events, for example (using jquery)
$('.x').on('click', function(){})
or from angularjs $scope.watch('name_of_var', function())
.Is there any possibility to replicate this behaviour in C, without using complex libraries?
Thanks.
nicknick
3 Answers
I would assume you're owning an embedded system with access to interrupts or a major event loop in a separate thread, otherwise this isn't possible..
A basic model for event handling is here:
Sorry if this is slightly naive.. but this is the best I could think of at the moment.
Aniket IngeAniket Inge
A system I can think about is a subscriber-notifier model.You may have something that handles your sensors (for example, a thread that polls on it to see if something happened). When it detects something, the task should raise a mechanism in order to let the outer world be aware : this is the notification process.
On the other side, only the people who are interested in your sensor should be notified, thus, a subscription method should be here to take care of this.
On the other side, only the people who are interested in your sensor should be notified, thus, a subscription method should be here to take care of this.
Now, the hard part comes in. When the sensor handler notifies the world, it must NOT spend too much time doing so otherwise it might miss other events. Thus, it is compulsory to have a task (or thread) dedicated to the notifying process. On the other hand, the subscribers wish to have some of their data updated when such a notified event is received. This is obviously an asynchronous process and thus the subscribers will have to supply the notifier thread with a callback.
Finally, you should mark your events with timestamps, this way the receivers will know if the event they get is outdated and wether or not they should discard it.
The final thing may look like the piece of code below :
Finally, you should mark your events with timestamps, this way the receivers will know if the event they get is outdated and wether or not they should discard it.
The final thing may look like the piece of code below :
Data structures
I do not have the time to complete my answer right now, I will edit it later to give you the full example. But starting from the data structures, you should get some ideas. Hope this is a bit helpful anyhow.
ReritoRerito
For events you need event loop, which detects that the actual event (say, data from network) happens, and then generates the the software event structure and calls appropriate event handler, or in more complex systems, chain of event handlers, until a handler marks the event accepted. If there's no handler, or no registered handler accepts the event, event is ignored.
Often event loop is in a library, which has API for application to have event handlers, and send application specific events, in addition to any events the library itself may produce. One problem with event based applications is, it is often complicated to use two libraries which both want to have their own event loop, unless library developer took extra care to allow using other event loop than library's own.
Unless it is a very low level real time system, it's critical that event loop does not do busy wait. In Linux/Unix/Posix code, event loop typically works around select() or poll() system function. When there are no events, event loop calls this function with timeout matching time of next timer event (if there are timer events). In addition to timeout, select()/poll() will also return if any of specified file handles (often network or IPC sockets) are ready for reading/writing/error, as well as if there is an interrupt which is not otherwise handled. Then event loop code checks why the function returned, generates and dispatches necessary events, and when all is done, calls the select()/poll() function again.
Also important in event based systems is, event handler must not block, because it is function called by event loop, so event loop does not continue somewhere in the background, handler function call is part of event loop. So handler function must process only available data, preferably quickly, then store necessary state to continue later, and return to wait for next event. For operations that must block, another thread must be launched. Also for long computations, the computation must either be chopped to small pieces to allow event loop to run too, or computation must happen in another thread. The annoying 'Not responding' in GUI application title bar typically means just this: application programmer was lazy/incompetent and blocked event loop, so it can't react to OS events.
So, yes, it is quite easy to have event based system with C. Just have a loop with select()/poll() in it, define event types, create data structs for events, and have a list of function pointers to be called with a new event struct as parameter, for each event type.
hydehyde