TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Visual Basic: Button Code Doesn't Respond
 
   
 Posted: 23 February 2002
 
   
 
 Applies to: v6.0, possibly others
 
   
 
Audience: Beginner
 
       
   

Question: I've added code to the Click() event of a button, but it doesn't run when the button is clicked. Why not?

Answer: Have you recently changed the name of your button, possibly from the default name (e.g. Command1) to something more descriptive (e.g. cbnWhatever)?1 If so, then it's possible you've broken the link between your code and your interface.

To illustrate:

  1. Create a new Standard EXE Project by choosing File | New Project and then choosing OK when the New Project dialog box appears.

  2. Place a button on the form.

    Tip: Remember that you can quickly place objects on your form by double-clicking the appropriate icon in the Toolbox.

  3. Create a new Click() event for your button by double-clicking the Command1 button.

  4. Type the following code:

         MsgBox ("Hello, world")
    1. Run the application by pressing F5.

    2. Confirm that your code works by choosing your button (either by clicking it or pressing Space).

    3. Choose the OK button and then close your application (either by pressing Alt+F4 or by clicking the Close button in the upper-right corner of the Form1 window).

    4. Select the button in the form design window and then use the Properties list to change the (Name) property to cbnHello.

    5. Run the form and then choose the button. Notice that nothing happens.

    6. Exit your application and then double-click your button.

    7. Note that Visual Basic creates a new Click() subroutine for cbnHello.

    8. If you're familiar with other development tools and are new Visual Basic, this can be a bit of a surprise. For example, when you rename objects in Borland Delphi, it automatically renames any existing event handlers to correspond to the new name of the object they're attached to. Visual Basic doesn't do this.2

      Note for those with Delphi experience: Visual Basic does, however, remove empty event handlers. Note that it does this when the application is compiled (either using File | Make EXE or when you run the application from the Visual Basic IDE). Remember that Delphi also does this when you save your files.3

      To fix the problem, either manually rename Command1_Click() to cbnHello_Click or use standard editing techniques to move your code to the new subroutine.

      Lessons Learned

      Two lessons come to mind from this experience:

      1. When learning new development tools, be prepared for different ways of thinking. In this case, a Delphi programmer may expect Visual Basic to sport similar conveniences. It does, but somewhat differently than you expect.

      2. When developing Visual Basic applications, it's best to name your objects before adding code to their event handlers.

      Footnotes:

      1 - This is a common technique for creating self-documenting code. The idea is to give objects names appropriate for the uses they're put to. In this article, for example, naming the button cbnHello provides two pieces of information to the person reading the source code: 1) the object is a command button and 2) it's intended to display the standard "Hello, World" message commonly used to teach programming languages.

      2 - On the off-chance that this is read by anyone on the VB development team or with input into the next release (e.g. beta-testers), this would be a nice feature to see added to the product, either as a direct feature or a user-controlled preference.

      3 - This would also be a nice addition to the product.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]