how do I make it do an event when a key is pressed like this
Anothe quick questions what does this "<>" do I have seen it a few times but dont get itCode:If e.KeyChar <> ChrW(13) Then
btnEquals_Click()
End If
Printable View
how do I make it do an event when a key is pressed like this
Anothe quick questions what does this "<>" do I have seen it a few times but dont get itCode:If e.KeyChar <> ChrW(13) Then
btnEquals_Click()
End If
For a button's Click event you can use the Button.PerformClick() method. There aren't much other examples like this, so a better way is usually to have the actual event call a second method, which you also call in other ways:
The <> operator means "not equal to" so it's the opposite of "=". You can also useCode:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1ClickMethod()
End Sub
Private Sub Button1ClickMethod()
'Do what button1 click should do here
End Sub
'... In another (keypress) sub
If e.KeyChar <> ChrW(13) Then
Button1ClickMethod()
End If
Code:If Not e.KeyChar = ChrW(13) Then
Just thought I would let you know this because someone just told me. You can do this instead:
The perform actually executes it apparentlyCode:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'action goes here
End Sub
'... In another (keypress) sub
If e.KeyChar <> ChrW(13) Then
Button1.PerformClick()
End If
Yes, I just told you in the post above :p
However, this only works for a few events and only for a few controls, so I decided to give you a more complete answer which you can use for any event.
"<>" = not Equal to. 1<>2 (1 is not equal to 2).. hope it is clear. You can use button1_PerformClick() as described in Previous Post.Quote:
Originally Posted by ngreenwood6
You can also do this to call an actual event (but like Nick has said, calling a sub/function is a better way to do it)
Say I have a button named "WriteXMLbtn" I could use this to click it from the form load event:
Of course that only works if you have already defined the event handler and I image you would run into problems if the "e" argument was not the same type for both the original event and the event being called. There's also the RaiseEvent command but it looks like just does the same thing as just typing the event out as in my example.vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WriteXMLbtn_Click(Me, e) End Sub
So all in all, a pretty pointless post eh.
You can just pass a new EventArgs object if the type is not the same (for example for painting)
This (and also passing "e" directly) however makes the EventArgs pretty useless!Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WriteXMLbtn_Paint(Me, New System.PaintEventArgs)
End Sub
But this is not the usual way of doing this... It's much more logical to just stick it all in a separate method (sub) and call that.
You don't "call" events. Events are "raised", hence the RaiseEvent keyword. What you're doing there is calling a method that just happens to be an event handler. There's a big difference between calling an event handler explicitly and raising an event. If you actually raise the event then every method that is registered as an event handler gets invoked. In your code just the one you actually call will get executed. In most cases there is only one handler for an event but that certainly doesn't have to be the case. It's important that people understand the difference between an event and an event handler, even if it doesn't really make much difference in this specific case.Quote:
Originally Posted by chris128