CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Button Behaviour

    I have a button called "butCancel"

    I have an event called "butCancel_Click"

    Code:
    Private Sub butCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            CancelAction()
        End Sub
    When I click on the button in design mode, I expect it to go to the "butCancel_Click" event

    It doesn't go there but rather, it opens up a NEW event called "butCancel_Click_1"

    Code:
       Private Sub butCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCancel.Click
    
        End Sub
    Now where is the logic behind this please ?

    Both events are in the same section of the form directly under each other and there is only one button in my project called "butCancel"

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Button Behaviour

    You need to add "Handles butCancel_Click" at the end of the declaration.

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Button Behaviour

    DSJ has it right.. here is why...

    In VB6 the Sub name was subject to the controls name .. IE. Clicking Button Click1 will call event Click1_Click...

    In .Net you can name the sub anything you like, as long as you assign it to an event.. using
    Code:
     Handles Object.Event
    hence clicking Button Click1 can call a sub named Close_And_Save_Everything_And_Exit simply by defining it
    Code:
    Private Sub Close_And_Save_Everything_And_Exit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click1.Click
    Gremmy
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Button Behaviour

    Further... you can add handlers via code with the AddHandler statement.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Button Behaviour

    There is a slight difference between Event and Event Handler. Event is something that happens when User does something (clicking a button, typing text, etc) in your application.

    Event Handler is a method that is used to write implementation of what is going to happen when that particular event is triggered.

    In .NET the terminology has become little too easy. You have an Event and an Event Handler. Event Handler can be given any name (not necessarily button1_click, it can be anything like mybutton_was_clicked or newMethod). All you need to make sure is that your code knows which method to call when an event is triggered, which is given by Handles or AddHandler keywords.

    Remember Events are actually implemented using Delegates in .NET. So it will be of great help if you learn Delegates also.

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Button Behaviour

    Thanks guys !

    Welcome to VB.Net school, George !

    Grade F

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Button Behaviour

    Quote Originally Posted by George1111
    Thanks guys !

    Welcome to VB.Net school, George !

    Grade F
    Lol .. Na... I'd give ya a 'D' grade...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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

Featured