|
tips: Does the Visual Basic class support inheritance?
The most ridiculous thing about VB is its object-oriented characteristics. In fact, VB is an object-based development tool. In VB
The established class supports inheritance. The following is an example:
First create a new project, then add a new class module (Class Module), the class name is set to BaseClass.
Then add the following code to BaseClass:
Public Sub BaseSub()'Virtual characteristics, BaseSub is implemented in subclasses
End Sub
Then add two class modules, set the class names to ImpClass and ImpClass2 respectively, and write in the code window of the class:
Implements BaseClass'Inheritance characteristics
The above line of code shows that the ImpClass and ImpClass2 implement the BaseClass.
Add the following code in the ImpClass window:
Private Sub BaseClass_BaseSub()'Implement the BaseSub method in the base class
MsgBox "Hello. This is Imp. inherited from BaseClass"
End Sub
Add the following code in ImpClass2:
Private Sub BaseClass_BaseSub()
MsgBox "Hello. This is Imp2. inherited from BaseClass"
End Sub
After completing the above class code, open Form1, add a CommandButton on it, in the Click event of the button
Write the following code:
Dim xImp As New ImpClass
Dim xIMp2 As New ImpClass2
Dim xBase As BaseClass
Set xBase = xImp'polymorphic characteristics
xBase.BaseSub
Set xBase = xIMp2
xBase.BaseSub
Set xBase = Nothing
Set xImp = Nothing
Set xIMp2 = Nothing
Run the program, click CommandButton, the program will pop up message boxes successively, displayed in ImpClass and ImpClass2
The set message.
From the above code, you can see how object-oriented features are implemented in VB: inheritance, virtualization, and polymorphism. Just the same
Unlike Java, C++, Object Pascal, VB hides many implementation details.
Question: How to block the close button X in the form?
Answer: You can use the API function to gray out the Close item in the form menu, because the menu is associated with the close button, so close
The button will also be unavailable. The specific code is as follows:
Option Explicit
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" _
(ByVal hMenu As Long, ByVal wIDEnableItem As Long, _
ByVal wEnable As Long) As Long
Const SC_CLOSE =&HF060
Private Sub Form_Load()
Dim hMenu As Long
hMenu = GetSystemMenu(Me.hwnd, 0)
RemoveMenu hMenu,&HF060,&H200&
Debug.Print EnableMenuItem(hMenu, SC_CLOSE, 1)
End Sub |
|