Expand intellisense to your custom function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    Expand intellisense to your custom function

    Now I am quite sure that any of you who have programmed in the VBA editor is absolutely in love with the intellisense, and how it can help you write code faster and how it can help you remember what values are acceptable for the function.

    For those not familiar with what Intellisense is:
    Intellisense is the feature that detects what you are typing and suggests AutoCompletion, as well as lists the number and type of arguments and whether they are optional and have a default value.

    I think the example most of us are probably familiar with is the msgbox. If you for example start to type msgbox in your code, or in the immediate pane you will see the following:
    [imgnothumb]http://bytes.com/attachments/attachment/6014d1328002982/intellisense-msgbox-example-1.jpg[/imgnothumb]
    The example shows that you are currently working on the Promt parameter of the msgbox function. If you are familier with reading the display, you will see that Promt is not optional but the remaining parameters are (Since they are enclosed in square brackets []. You will also see that the next parameter is of type vbMsgBoxStyle and it has a default value vbOkOnly.

    If you type a "," you will proceed to working/writing the next paramater Buttons. Neatly enough the VBA editor will show a dropdown of the acceptable values, as shown below:
    [imgnothumb]http://bytes.com/attachments/attachment/6015d1328002982/intellisense-msgbox-example-2.jpg[/imgnothumb]
    If you start typing the editor will highlight the first acceptable option that matches what you have allready typed.


    Creating your own intellisense:
    Now if you write a simple function you will still see most of the information that you see in the first of the above msgbox examples. But if you also want to provide the coder (or yourself) with the list of acceptable values, you can even do that too.

    First we need to create an Enumeration which we do in a seperate module:
    Code:
    Public Enum cmDisplayImage
        cmNoImage = 0
        cmCritical = 16
        cmExclamation = 48
        cmInformation = 64
    End Enum
    Info: The example provided is me working on a custom messagebox, therefore I have prefixed both the enumeration and the constants with cm for Custom Messagebox.

    Then when I write my function, I specify the parameter type to be of cmDisplayImage as shown below:
    Code:
    Public Function CustomMessageBox(strCaption As String, Optional cmImage As cmDisplayImage = cmNoImage) As Integer
    I have specified that the second parameter should be of type cmDisplayImage and it has a default value saying no image.

    When I use it, it looks like this:
    [imgnothumb]http://bytes.com/attachments/attachment/6016d1328004329/intellisense-custommessagebo x.jpg[/imgnothumb]
    If you have any questions please don't hesitate to ask. If you have question about the actual text of this article please post/reply here. If you have questions about implementing it yourself, I suggest you start a new seperate Question and link to this article.
    Attached Files
    Last edited by TheSmileyCoder; Jan 31 '12, 10:11 AM.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Thank you. I really need to put public enumerations to work for me.

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      Wow, this is extremely helpful. I didn't know that you could do that. So if you declare a parameter as the name of an enum, what data type is that?

      And thank-you Jim for posting to it so that I would notice the article.

      Comment

      Working...