“User-defined type not defined” error in VB 6 unde

2019-04-22 00:08发布

问题:

I am using Windows 7 and my project is in VB 6.0. I am getting errors while I am executing my program. It shows the error:

User-defined type not defined.

Here is my code:

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
    Select Case Button.Key
        Case "trace": Call mntrace_Click
        Case "snrplot": Call mnSnrplot_Click
        Case "skyplot": Call mnskyplot_Click
        Case "nmea": Call mnNmea_Click
        Case "navigation": Call mnNavigation_Click
        Case "survey": Call mnSurvey_Click
        Case "pause/start": Call mnpause_Click
        Case "save": Call mnsave_Click
        Case "print": Call mnprint_Click
        Case "offline": Call mnoffline_Click
    End Select
End Sub

How can I solve this error?

回答1:

The compiler is automatically highlighting the first line of the function declaration for you when the error appears. That means the error occurs somewhere within that line. Sometimes that's not as helpful as you'd like, but in this case, it manages to tell you quite a lot.

Specifically, the only "user-defined type" (really, the only "type" at all) that appears in the function declaration is MSComctlLib.Button. What the compiler error message is telling you here is that it doesn't know what a MSComctlLib.Button is. It therefore assumes it's a "user-defined" type because it often doesn't know what the user is talking about. :-)

Either way, the fix is simple: you need to tell the compiler what an MSComctlLib.Button is. In this case, it guessed wrong in assuming that it is a user-defined type. It's actually a button control provided in the Microsoft Windows Common Controls Library. To tell VB 6 about this control, you need to add the corresponding component to your project. Follow these steps:

  1. From the "Project" menu, select "Components".

  2. In the dialog box that appears, scroll about 2/3 of the way down the list to the M's. Place a check by both the "Microsoft Windows Common Controls 6.0" and "Microsoft Common Controls-2 6.0" items. (Don't worry if yours have a different service pack designation.)

         

  3. Click the OK button. If you're quick, you'll see some additional controls being added to your toolbox. These are the controls provided by the component libraries that you just added. Among those controls is one called Button.

Finally, try to compile and run your project again—everything should be fine this time, because now the compiler knows what the MSComctlLib.Button type is. In case you still don't, it's a button that appears on your toolbar. The toolbar control is provided by the Common Controls library, and it includes a type that defines an individual button appearing on that toolbar.



回答2:

Sounds like you are missing a reference to an object library.

(Have you executed it without errors elsewhere?)



回答3:

I think you just copied and pasted that code from elsewhere. Normally, if you wanted to reference MSComctlLib, you will normally do first the steps stated by Cody Gray here before you can access the Type Library.



标签: windows-7 vb6