Results 1 to 8 of 8

Thread: [2008] Events

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    [2008] Events

    how do I make it do an event when a key is pressed like this

    Code:
     If e.KeyChar <> ChrW(13) Then
                btnEquals_Click()
            End If
    Anothe quick questions what does this "<>" do I have seen it a few times but dont get it

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Events

    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:
    Code:
    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
    The <> operator means "not equal to" so it's the opposite of "=". You can also use
    Code:
    If Not e.KeyChar = ChrW(13) Then

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] Events

    Just thought I would let you know this because someone just told me. You can do this instead:

    Code:
    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
    The perform actually executes it apparently

  4. #4

  5. #5
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: [2008] Events

    Quote Originally Posted by ngreenwood6
    how do I make it do an event when a key is pressed like this

    Code:
     If e.KeyChar <> ChrW(13) Then
                btnEquals_Click()
            End If
    Anothe quick questions what does this "<>" do I have seen it a few times but dont get it
    "<>" = 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.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Events

    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:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         WriteXMLbtn_Click(Me, e)
    3. End Sub
    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.
    So all in all, a pretty pointless post eh.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Events

    You can just pass a new EventArgs object if the type is not the same (for example for painting)
    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
    This (and also passing "e" directly) however makes the EventArgs pretty useless!

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Events

    Quote Originally Posted by chris128
    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:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         WriteXMLbtn_Click(Me, e)
    3. End Sub
    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.
    So all in all, a pretty pointless post eh.
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width