Visual Basic class for object-oriented support

zhaozj2021-02-08  242

VB is the most doctrine of the disease is its object-oriented characteristics. In fact, VB is an object-based development tool. The classes established in VB are support inheritance. Below is an example: first build a new project, then add a new class module (Class Module), the class name is set to BaseClass. The following code is then added to BASECLASS: Public Sub BaseSub () 'Virtual Features, BaseSub implements End Sub in subclass and add two class modules, and class names are set to IMPCLASS and IMPCLASS2, and then write in the code window of the class. Enter: IMPLEMENTS BASECLASS 'Inherit Features The row code indicates that class ImpClass and ImpClass2 implement class BaseClass. Adding ImpClass window following code: Private Sub BaseClass_BaseSub () 'base class implementation of BaseSub method MsgBox End Sub adding the following code ImpClass2 the "Hello This is Imp inherited from BaseClass..": Private Sub BaseClass_BaseSub () MsgBox "Hello After the IS IMP2. Inherited from baseclass "End Sub After completing the above class code, open Form1, add a commandbutton above, write the following code in the click Click event: DIM XIMP AS New ImpClass Dim Ximp2 As New ImpClass2 Dim XBase as baseclass set xbase = XIMP 'polymorphic feature XBase.baseSub set XBase = XIMP2 XBASESUB SET XBASE = XIMP2 XBASE.BASESUB SET XBASE = Nothing set Ximp = Nothing set Ximp2 = Nothing running running, click CommandButton, the program will pop up the message box, displayed in Impclass and Messages set in ImpClass2. From the above code, you can see how the VB implements object-oriented features: inheritance, virtual, and polymorphism. It is just different from Java, C , Object Pascal, and VB hides many details. Q: How to block the shutdown button X? A: You can use the API function to gray it off in the form menu, because the menu is associated with the off button, which is not available.

Specific code 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 Subwww.applevb.com

转载请注明原文地址:https://www.9cbs.com/read-1192.html

New Post(0)