event-handling

Avoid duplicate event subscriptions in C#

对着背影说爱祢 提交于 2019-12-18 11:06:36
问题 How would you suggest the best way of avoiding duplicate event subscriptions? if this line of code executes in two places, the event will get ran twice. I'm trying to avoid 3rd party events from subscribing twice. theOBject.TheEvent += RunMyCode; In my delegate setter, I can effectively run this ... theOBject.TheEvent -= RunMyCode; theOBject.TheEvent += RunMyCode; but is that the best way? 回答1: I think, the most efficient way, is to make your event a property and add concurrency locks to it

Passing arguments to an event handler

余生颓废 提交于 2019-12-18 10:23:12
问题 In the below code, I am defining an event handler and would like to access the age and name variable from that without declaring the name and age globally. Is there a way I can say e.age and e.name ? void Test(string name, string age) { Process myProcess = new Process(); myProcess.Exited += new EventHandler(myProcess_Exited); } private void myProcess_Exited(object sender, System.EventArgs e) { // I want to access username and age here. //////////////// eventHandled = true; Console.WriteLine(

Javascript , event handler is always called, even if the event is not raised

白昼怎懂夜的黑 提交于 2019-12-18 09:39:01
问题 i have the following code which extends the JQuery and adds a method to the JQuery: $.fn.attachWithMessage = function () { $(this).focusin(showMessage()); } function showMessage() { alert('hi'); } so I can use that code as follows : <input type="text" name="name" id="textbox" /> $(document).ready(function () { $("#textbox").attachWithMessage (); }); when I load the page for the first time, a message box shows up with ('hi') message. even if I didn't click in the text box. I also tried the

Overwritten “this” variable problem or how to call a member function?

断了今生、忘了曾经 提交于 2019-12-18 09:37:30
问题 I have this class where I am using a combination of jQuery and Prototype: var MyClass = Class.create({ initElements: function(sumEl) { this.sumEl = sumEl; sumEl.keyup(this.updateSumHandler); }, updateSumHandler: function(event) { // Throws error here: "this.updateSum is not a function" this.updateSum(); }, updateSum: function() { // does something here } }); How can I call this.updateSum() after all? 回答1: You need to use closures. initElements: function(sumEl) { this.sumEl = sumEl; var ref =

Blur event triggered on focus

空扰寡人 提交于 2019-12-18 09:28:55
问题 I am using the following code jsFiddle: function Field(args) { this.id = args.id; this.name = args.name ? args.name : null; this.reqType = args.reqType ? args.reqType : null; this.reqUrl = args.reqUrl ? args.reqUrl : null; this.required = args.required ? true : false; this.error = args.error ? args.error : null; this.elem = document.getElementById(this.id); this.value = this.elem.value; this.elem.addEventListener('blur', this, false); this.elem.addEventListener('focus', this, false); } //

Find all event handlers for a Windows Forms control in .NET

岁酱吖の 提交于 2019-12-18 09:07:19
问题 Is there a way to find all event handlers for a Windows Forms control? Specifically statically defined event handlers? 回答1: Windows Forms has strong counter-measures against doing this. Most controls store the event handler reference in a list that requires a secret 'cookie'. The cookie value is dynamically created, you cannot guess it up front. Reflection is a backdoor, you have to know the cookie variable name. The one for the Control.Click event is named "EventClick" for example, you can

How to use Task<T> raising an event and waiting for event to be finished

随声附和 提交于 2019-12-18 09:05:37
问题 I have the following scenario: Client who is requesting a webservice to start public bool Start(MyProject project, string error) A web service who receives the call from the client in a method public event EventHandler<StartEventArgs> startEvent; public bool Start(MyProject project, string error) { Task<bool> result = StartAsync(project, error); return result.Result; } protected virtual void OnStart(StartEventArgs e) { // thread safe trick, snapshot of event var se = startEvent; if (se !=

JavaFX TextField how to prevent key from getting typed in keypressed event

自闭症网瘾萝莉.ら 提交于 2019-12-18 09:05:08
问题 I need to detect the numpad key presses to do some stuff. here is the code: textField.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { if(event.getCode().isKeypadKey()) { //do stuff here... event.consume(); } } }); lets say user presses '2' on the numpad, although the event is consumed, the charachter '2' will appear in the textfield. but i dont want that. Any ideas how to prevent this? whats more interesting is that if i throw a dummy exception

How can I get an actual EventHandler delegate instance from an event in VB.NET?

核能气质少年 提交于 2019-12-18 08:29:34
问题 In C#, I could do something like this: EventHandler handler = this.SomeEvent; ...which would allow me to, for example, do: Delegate[] attachedHandlers = handler.GetInvocationList(); In VB.NET, I can't seem to figure out how to do a similar thing. This doesn't work: Dim handler As EventHandler = Me.SomeEvent ...due to the following error: Public Event SomeEvent(sender As Object, e As EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. But

JavaFX 2 event dispatching to underlying nodes

拟墨画扇 提交于 2019-12-18 08:26:40
问题 Is there a correct way to solve the problem with event propagation between two sibling panes? For example we have StackPane with 2 panes inside. StackPane p = new StackPane(); Region p1 = new Region(); Region p2 = new Region(); p.getChildren().addAll(p1, p2); p2 in this example capture mouse events and p1 can't react on it even if event is not consumed. Is there a correct way to propagate event to p1 if it not consumed by p2? setMouseTransparent not solve my problem because I need that both