First, the subject probably doesn't use the correct terms but I'm not sure
what it's called in VB.
I'm writing a media player app. The subroutine that handles the "open file"
button contains an If statement to see if a file was already playing and if
so executes some code to stop the previous file from playing among other
things. There's also a "stop" button on the form that contains the exact
same code. Obviously it would be easier if there was just one subroutine
containing the code and that sub would be called from the If in the open
routine and from the stop routine. My question is, is there a way to make
the program think the "Stop" button was pressed? I.E. is there anything
statement I can put in the If statement in the "open file" sub that will
"trigger" just the same as if the "stop" button had been pressed? If so, are
there any caveats to faking a button click from within the program versus
just putting the code in a sub and calling it from both events?
--
Thanks,
Ricky W. Hunt
freendeed 22 3041
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in
news:PINOc.220130$Oq2.52920@attbi_s52: I can put in the If statement in the "open file" sub that will "trigger" just the same as if the "stop" button had been pressed? If so, are there any caveats to faking a button click from within the program versus just putting the code in a sub and calling it from both events?
You can just call the sub like:
MyButtonPressEvent(Me, nothing)
The only cavet is your event arg parameter will be empty - unless of course
you pass the eventarg as well.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in
news:PINOc.220130$Oq2.52920@attbi_s52: I can put in the If statement in the "open file" sub that will "trigger" just the same as if the "stop" button had been pressed? If so, are there any caveats to faking a button click from within the program versus just putting the code in a sub and calling it from both events?
You can just call the sub like:
MyButtonPressEvent(Me, nothing)
The only cavet is your event arg parameter will be empty - unless of course
you pass the eventarg as well.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Ricky,
What kind of "buttons" are we talking about?
Rather then calling an event handler directly, as there may actually be
multiple event handlers attached to the button. You can simply call
Button.PerformClick.
Hope this helps
Jay
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:PINOc.220130$Oq2.52920@attbi_s52... First, the subject probably doesn't use the correct terms but I'm not sure what it's called in VB.
I'm writing a media player app. The subroutine that handles the "open
file" button contains an If statement to see if a file was already playing and
if so executes some code to stop the previous file from playing among other things. There's also a "stop" button on the form that contains the exact same code. Obviously it would be easier if there was just one subroutine containing the code and that sub would be called from the If in the open routine and from the stop routine. My question is, is there a way to make the program think the "Stop" button was pressed? I.E. is there anything statement I can put in the If statement in the "open file" sub that will "trigger" just the same as if the "stop" button had been pressed? If so,
are there any caveats to faking a button click from within the program versus just putting the code in a sub and calling it from both events?
-- Thanks, Ricky W. Hunt freendeed
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OR*************@TK2MSFTNGP12.phx.gbl... Rather then calling an event handler directly, as there may actually be multiple event handlers attached to the button. You can simply call Button.PerformClick.
That's exactly what I was looking for. Thanks.
Ricky,
Although the sample from Jay goes probably in most situations, keep in mind
that this is only with the click, where you ask for the key press this does
not real function because you do not know which key.
As Lucas stated when you want to transfer the event you are in trouble with
the PerformClick.
See this sample beneath I made special for this. In the first buttons is an
emulate of a click event, which is an event with almost no event
information.
The second button fakes a key press here you can see that a key press is
something else than a click.
That button2 is called by button1 by the way, so do not misunderstand that.
You can see that in the sample because when button1 is clicked it says that
it was button2
\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Button2.PerformClick()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim mypress As New KeyPressEventArgs("D"c)
TextBox1_KeyPress(sender, mypress)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _
TextBox1.KeyPress
MessageBox.Show("I am clicked by key " & _
e.KeyChar & " in " & _
DirectCast(sender, Control).Name)
End Sub
///
I hope this helps a little bit?
Cor
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Og**************@TK2MSFTNGP11.phx.gbl... \\\ Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Button2.PerformClick() End Sub
Thanks Cor. I got that above part to work earlier and it does just what I
wanted. I just wondered if that was standard procedure or do people prefer
to put the common code in a sub and just call it from the two places. I can
see that if you go firing of clicks programmaticaly you could start a whole
chain of events you might not want.
Ricky,
I have seen so much discussion about this in this newsgroup (without a real
conclusion) that it as with most with VBNet (why it fits most of us probable
better than the what more ancient languages) you can do it in more ways.
Therefore my idea about this is, do what you prefer, the same as that you
are not always using the same words in your native language.
However do it consequent.
I hope this gives an idea.
Cor Thanks Cor. I got that above part to work earlier and it does just what I wanted. I just wondered if that was standard procedure or do people prefer to put the common code in a sub and just call it from the two places. I
can see that if you go firing of clicks programmaticaly you could start a
whole chain of events you might not want.
Ricky,
I prefer & recommend using Button.PerformClick, using "common code in a sub
and just call it from the two places" or using the same event handler for
multiple events.
I would not call, nor recommend calling, the event handler code directly as
Cor & Lucas showed.
In case you did not realize it you can actually have the same event handler
handle two events. Or the same event can be handled by more then one event
handlers
You can use AddHandler & RemoveHandler statement to connect multiple event
handlers, or you can list multiple controls on the Handles clause of an
event handler.
Either:
AddHandler Button1.Click, AddressOf Button_Click
AddHandler Button2.Click, AddressOf Button_Click
Or Cor's specific example could be simplified to:
Private Sub Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
Dim mypress As New KeyPressEventArgs("D"c)
TextBox1_KeyPress(sender, mypress)
End Sub
Hope this helps
Jay
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:_M1Pc.68277$eM2.55841@attbi_s51... "Cor Ligthert" <no**********@planet.nl> wrote in message news:Og**************@TK2MSFTNGP11.phx.gbl... \\\ Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Button2.PerformClick() End Sub
Thanks Cor. I got that above part to work earlier and it does just what I wanted. I just wondered if that was standard procedure or do people prefer to put the common code in a sub and just call it from the two places. I
can see that if you go firing of clicks programmaticaly you could start a
whole chain of events you might not want.
> I would not call, nor recommend calling, the event handler code directly
as Cor & Lucas showed.
To fake a key press Jay
Can you show me how you do that and not faking a button click?
I would recommnend this faking a button click. However a keypress???????
Cor
Typos and maybe not clear enough I would not call, nor recommend calling, the event handler code directly
as Cor & Lucas showed.
To fake a key press Jay?
Can you show me how you do that while faking a button click?
(faking keypress char D as I did)
I would recommend faking a button click with a performClick.
However a keypress???????
Cor
Jay,
Reading it again, Or Cor's specific example could be simplified to:
Private Sub Button_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click Dim mypress As New KeyPressEventArgs("D"c) TextBox1_KeyPress(sender, mypress) End Sub
Hope this helps
What did you simplified in that, it is exactly the part as I showed how to
fake a keypress there is nothing changed?
The other part was to show that a performclick as you recommended would not
go in this situation.
Cor
Cor,
You had two event handlers Button1.Click and Button2.Click where the first
simply called the second. I used that two show that the OP could have used a
single event handler for both buttons.
Hope this helps
Jay
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:up****************@TK2MSFTNGP11.phx.gbl... Jay,
Reading it again,
Or Cor's specific example could be simplified to:
Private Sub Button_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click Dim mypress As New KeyPressEventArgs("D"c) TextBox1_KeyPress(sender, mypress) End Sub
Hope this helps What did you simplified in that, it is exactly the part as I showed how to fake a keypress there is nothing changed?
The other part was to show that a performclick as you recommended would
not go in this situation.
Cor
Cor,
I would not "fake a keypress".
The keypress is invoking a command.
The button is invoking a command.
In both cases I would simply "invoke the command", where the "command" is
either a command object via the Command Pattern, or a method in the current
object.
I'm *not* saying "faking a keypress" is bad or anything, I am saying I would
take a more pure OO approach.
If I really needed my program to see the "D" key when I pressed the button,
I would use SendKeys Private Sub Button_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
SendKeys.Send("D")
End Sub
However I would only use SendKeys if another "more appropriate" method was
not available, such as Control.SelectNextControl.
Again I am not saying you are wrong, I am stating alternatives.
Hope this helps
Jay
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:e6***************@tk2msftngp13.phx.gbl... I would not call, nor recommend calling, the event handler code directly
as Cor & Lucas showed.
To fake a key press Jay
Can you show me how you do that and not faking a button click?
I would recommnend this faking a button click. However a keypress???????
Cor
Jay,
I used that button only to start the sample action not to show how a button
was working, I definitly would not do it this way. Did you look at the
subject by the way that is where I am all the time refering too.
Cor
Jay, I would not "fake a keypress".
See my other comment.
Cor
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eO****************@TK2MSFTNGP09.phx.gbl... Ricky,
I have seen so much discussion about this in this newsgroup (without a
real conclusion) that it as with most with VBNet (why it fits most of us
probable better than the what more ancient languages) you can do it in more ways.
I would hazard to guess the confusion is usually from programmers who knew
BASIC but not VB or have never programmed a Windows app. We seem to
understand what we want but don't know the current syntax (which seems to
change with every version...LOL). Thanks.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OT*************@TK2MSFTNGP12.phx.gbl... Ricky, I prefer & recommend using Button.PerformClick, using "common code in a
sub and just call it from the two places" or using the same event handler for multiple events.
I didn't know if this was even available when I asked but it seemed like
something people might want to do. It didn't even dawn on me that the event
handling routines are just subs that I could call at will (will null
parameters). I'm new to Window's programming (though I've programmed since
the late '70s and professionally since the '80s - on mainframes) so I wasn't
sure if anything happened when I called the routines or not. I'm still
trying to digest all the events that spawned even from a simple button
click. I would not call, nor recommend calling, the event handler code directly
as Cor & Lucas showed.
That approach appeals to me because (as I said above) I'm still not sure
what all events are created (and in what order) when do a normal click and I
didn't want half-a-dozen (or more..) things going off when I just wanted the
one. Is all the PerfromClick does is call the subroutine? If so, it would be
fine. Certainly it doesn't fire off mousevers and everything else just like
I had physically pressed the button does it?
In case you did not realize it you can actually have the same event
handler handle two events. Or the same event can be handled by more then one event handlers
Yes. I've learned this. Though in the case I was talking about I needed the
one button to do more than just what the other button did so I needed an
event handler for both.
You can use AddHandler & RemoveHandler statement to connect multiple event handlers, or you can list multiple controls on the Handles clause of an event handler.
Either:
AddHandler Button1.Click, AddressOf Button_Click AddHandler Button2.Click, AddressOf Button_Click
Or Cor's specific example could be simplified to:
Private Sub Button_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click Dim mypress As New KeyPressEventArgs("D"c) TextBox1_KeyPress(sender, mypress) End Sub
Hope this helps Jay
Yes. Thanks.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl... Cor, I would not "fake a keypress".
I guess I really meant "fake a button press" but I guess the ideas the same.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl... I'm *not* saying "faking a keypress" is bad or anything, I am saying I
would take a more pure OO approach.
I've always been an organized programmer and used subroutines, flowcharts,
etc. So when I first heard about OOP (possibly in the late 80's/early 90's)
I thought "what's the big deal?" (but I really didn't grasp it all). Even
though I'm usually very A.R. (anal-retentive...not sure if the hyphen is
correct...) I thought it was overkill. I must admit now though that I've
used it some and figured out what it's doing I think it's great and much
easier to use than anything I've done before. Especially how easy it is to
call third-party functions that I really have no idea how they work. All I
have to know is the syntax and, boom, it's working.
Ricky, one. Is all the PerformClick does is call the subroutine? If so, it would
be fine. Certainly it doesn't fire off mousevers and everything else just
like I had physically pressed the button does it?
Control.PerformClick, simply raises the Click event. It does not invoke any
of the mouse events.
Raising an Event calls the one or more event handlers (subroutines) that are
handling that event.
I believe SendKeys will cause all the keyboard events to occur. Which IMHO
would be a good thing.
Hope this helps
Jay
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:KqjPc.232123$Oq2.100075@attbi_s52... "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:OT*************@TK2MSFTNGP12.phx.gbl... Ricky, I prefer & recommend using Button.PerformClick, using "common code in a sub and just call it from the two places" or using the same event handler
for multiple events.
I didn't know if this was even available when I asked but it seemed like something people might want to do. It didn't even dawn on me that the
event handling routines are just subs that I could call at will (will null parameters). I'm new to Window's programming (though I've programmed since the late '70s and professionally since the '80s - on mainframes) so I
wasn't sure if anything happened when I called the routines or not. I'm still trying to digest all the events that spawned even from a simple button click.
I would not call, nor recommend calling, the event handler code directly as Cor & Lucas showed.
That approach appeals to me because (as I said above) I'm still not sure what all events are created (and in what order) when do a normal click and
I didn't want half-a-dozen (or more..) things going off when I just wanted
the one. Is all the PerfromClick does is call the subroutine? If so, it would
be fine. Certainly it doesn't fire off mousevers and everything else just
like I had physically pressed the button does it?
In case you did not realize it you can actually have the same event handler handle two events. Or the same event can be handled by more then one
event handlers
Yes. I've learned this. Though in the case I was talking about I needed
the one button to do more than just what the other button did so I needed an event handler for both.
You can use AddHandler & RemoveHandler statement to connect multiple
event handlers, or you can list multiple controls on the Handles clause of an event handler.
Either:
AddHandler Button1.Click, AddressOf Button_Click AddHandler Button2.Click, AddressOf Button_Click
Or Cor's specific example could be simplified to:
Private Sub Button_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click Dim mypress As New KeyPressEventArgs("D"c) TextBox1_KeyPress(sender, mypress) End Sub
Hope this helps Jay
Yes. Thanks.
On Sat, 31 Jul 2004 14:07:43 GMT, Ricky W. Hunt wrote: Obviously it would be easier if there was just one subroutine containing the code and that sub would be called from the If in the open routine and from the stop routine.
Why not just do as you said in your post and put the code to stop playback
in a separate subroutine and call that from both places:
Private Sub StopPlayback()
If bPlaying Then
'Code to stop playback here
End If
End Sub
Private Sub OpenFile()
StopPlaying
'Other code here
End Sub
Private Sub StopButton_Click(...) Handles StopButton.Click
StopPlayback
End Sub
You can call the PerformClick method but if there are more than one handler
attached, they will all be executed. This is the most logical way, IMHO.
Regards,
--
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:qv******************************@40tude.net.. . On Sat, 31 Jul 2004 14:07:43 GMT, Ricky W. Hunt wrote:
Obviously it would be easier if there was just one subroutine containing the code and that sub would be called from the If in the open routine and from the stop routine.
Why not just do as you said in your post and put the code to stop playback in a separate subroutine and call that from both places:
That was an option. I'm just learning VB though so I wanted to see what
methods were available and preferable. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fakhar |
last post by:
Hi All,
How can I check fake requests on my webpage. I am asking for email address
as input and I wounder if anyone write a program to send fake...
|
by: cheezebeetle |
last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they...
|
by: Bryan Parkoff |
last post by:
I work emulator project using 32 Bit emulated processor. The problem is
that it attempts to allocate 4GB memory because some people want to use...
|
by: Itai |
last post by:
Background:
I have four Web Form pages with respective C# code behind files,
all in the same project:
localhost/vpath1
Page1.aspx...
|
by: mwebel |
last post by:
Hi,
is is also a C question but the program im writing is in C++ and it
applies as well so:
i have a program in the "main(int argc, const char...
|
by: Konstantin Andreev |
last post by:
Hello.
Some time ago I asked in this conference, - How to use an ONLINE BACKUP to restore database onto another system? - but got no answers....
|
by: Fabio |
last post by:
Hi all
There is a way to exec a function asynchronously?
I need to exec a function that is too slow but for me can be executed in the...
|
by: Julien |
last post by:
Hello all,
I would like to do something like:
def called(arg)
if arg==True:
!!magic!!caller.return 1
def caller(arg)
called(arg)
|
by: deepikashalini |
last post by:
In javascript i call a function at the time of to press an enter key. number of times i press a key the process will be activated. hoow to i ignore...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
| |