By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

One Method - Multiple Events

P: 6
I would like to handle multiple events with one method. The events are KeyDown and SerialPort.PinChanged.
My problem is that these events have different signatures. The method is simple as follows:

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
---------------------------------
"The body of the Method
---------------------------------
End Sub

Any Suggestions?
Thanks
Apr 14 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 5K+
P: 8,434
Why not simply have both event procedures call the same Sub?
Apr 14 '07 #2

tifoso
P: 41
If you have on a usercontrol and need the events visible in some container use RaiseEvent and handle them properly

CIao
Apr 14 '07 #3

P: 6
I have 2 events that I wish to use.
The KeyDown event and the SerialPort.PinChanged event.
My procedure controls 16 relays and toggles them up to 125 times in a predetermined sequence. Various sequences are stored in an Array.
If it were a simple procedure I would just duplicate it. I have to stay in the 1 process to maintain the sequence.

I have 2 events that interact with the process. A key event and an input through the Serial Port --- a switch closure.

As I said before the KeyDown Event has a different signature than the SerialPort.PinChanged Event so both events can't call the same Sub.

Any other thoughts?
Apr 14 '07 #4

Expert 5K+
P: 8,434
I didn't say to have both events call the same sub. I said to have both event procedures call the same sub. Not quite the same thing.

Here's another though. What if you were to have a continuous loop running. The two events you mentioned could simply set some values/flags which would be continually checked inside the loop. (I guess you could call the loop a state machine).

In other words, break the direct link between the events and the processing - the processing is already going, watching for data. The two event procedures just supply the data.
Apr 15 '07 #5

P: 6
Your right having the 2 events calling the same sub it the right way to handle it. I don't know were my head was...fixated on both events firing the method. That is the most direct and should have been the most obvious.

I did devise another more clumsy method. I used the KeyPressed event set the DTR true and detect this PinChange OR-ed with my original CD PinChange. This works also.
Apr 16 '07 #6

P: 6
Your right having the 2 events calling the same sub it the right way to handle it. I don't know were my head was...fixated on both events firing the method. That is the most direct and should have been the most obvious.

I did devise another more clumsy method. I used the KeyPressed event set the DTR true and detect this PinChange OR-ed with my original CD PinChange. This works also.

Forgot to add that I jumpered DTR to DSR so setting DTR would be detected as a PinChange on the SerialPort.
Apr 16 '07 #7

Expert 5K+
P: 8,434
So you basically had one event trigger the other? Sneaky. :)
Apr 16 '07 #8

P: 6
I thought I had things under control.
Either event trigger will call the Method. The Method closes some relays via the serial port and starts a Timer. When the Timer times out the Timer Event opens the relays via the same serial port.

This works perfectly when the method is called from the keyboard using the KeyPressed event. But when I call the same method using a SerialPortPinChanged event the Timer event does not occur.

I found that if I have a Msgbox("something") called at the end of the PinChanged call the Timer will open the relays. I have had to invoke some cross thread commands because the textboxes were not created in the same thread as the PinChanged event.

Putting the Msgbox in makes it work!!
Hope this is complete enough to understand what I am attempting to do.
Any ideas?
Apr 20 '07 #9

P: 6
Found the problem with the Timer.
I was using the Windows Forms Timer. It is limited to use in a single thread.
I changed to System.Timers. With this change the SerialPortPinChange would call the methods correctly.
Apr 22 '07 #10

Expert 5K+
P: 8,434
Found the problem with the Timer.
I was using the Windows Forms Timer. It is limited to use in a single thread.
I changed to System.Timers. With this change the SerialPortPinChange would call the methods correctly.
Hey, that's great news. :)

I'm glad you found the answer, because it had me scratching my head.
Apr 22 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.