Sunday, June 17, 2007

Visual Basic 6.0 Tips and Tricks


Visual Basic 6.0 Tips and Tricks



  1. Conditional Compilation to bypass Form_Paint Event

    The following code uses a conditional compilation trick such that the form's paint event will be triggered according to the value of the constant cTRIGGERPAINT. This is very useful in debugging forms that have code written in the paint event. For many times, during debugging, we may think of some events in our VB apps, that if it wont trigger this time. We may face it very difficult if an unnecessary event trigger during our debugging and consumes a good part of our valuable time. Hereby I am sharing a tip to avoid such condtion. For me, in the app I used to debug have code written in the Form_Paint event in all forms. This event prevents us to debug as it will be triggered every time the form is activated. A solution for this is to use conditional compilation. see the example below




    #Const cTRIGGERPAINT = 0 'The paint event runs according to the value set here. You can also try with variables

    #If cTRIGGERPAINT = 1 Then
    Private Sub Form_Paint()
    'your code
    End Sub
    #End If



  2. Code to determine whether in Visual Studio Debug mode

    It will be a useful thing sometimes if we want to determine whether we are executing the project from Visual Studio IDE or from its exe. This can be done using a trick. You may know about the Debug.Print method that is commonly used for debugging. The code will be executed only in debug mode (Visual Studio execution).This can be made useful for the purpose. We shall try to generate an error in the debug.print part. If it is running under Visual Studio, then the error can be tapped. Try the code first in IDE and then make exe.


    Function IsExecutedbyVisualStudioIDE()
    On Error Resume Next

    Debug.Print 1 / 0 'generates error.... but runs only in Visual studio.
    If Err <> 0 Then IsExecutedbyVisualStudioIDE= True

    End Function








Technorati :

No comments: