For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > May 2005 > Using "Include" Approach for Repetitive Code...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using "Include" Approach for Repetitive Code...
Vincent

2005-05-30, 8:55 pm

Hello Friends,

I am mainly a ASP web developer so I have been looking into the possibilty
of using an "include" style approach to adding code to my VB6 forms. In ASP
you can place <!--#include file="mypath/mypage.asp" --> at the top of any
document to prevent having to retype the same code in every document. Is
there something like this for VB6...? I have an app that uses several forms
and they all need the same large chunk of code in the General Declarations.

I am familar with making public subs, functions and other global data but I
have not found a solution to my problem using these approaches.

Thanks for any help. :)



Steven Burn

2005-05-30, 8:55 pm

Project > Add Module

Stick all of the code you need to duplicate in there :o)

e.g.

'// Module1
Option Explicit

Public bSomething As Boolean

'// Form1
Option Explicit
bSomething =3D True

'// Form2
Option Explicit
bSomething =3D False

'// Form3
Option Explicit
MsgBox bSomething

--=20
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

"Vincent" <vincent938@hotmail.com> wrote in message =
news:#QmnHWVZFHA.1868@TK2MSFTNGP14.phx.gbl...
> Hello Friends,
>=20
> I am mainly a ASP web developer so I have been looking into the =

possibilty=20
> of using an "include" style approach to adding code to my VB6 forms. =

In ASP=20
> you can place <!--#include file=3D"mypath/mypage.asp" --> at the top =

of any=20
> document to prevent having to retype the same code in every document. =

Is=20
> there something like this for VB6...? I have an app that uses several =

forms=20
> and they all need the same large chunk of code in the General =

Declarations.
>=20
> I am familar with making public subs, functions and other global data =

but I=20
> have not found a solution to my problem using these approaches.
>=20
> Thanks for any help. :)
>=20
>=20
>=20


YYZ

2005-05-30, 8:55 pm

"Vincent" <vincent938@hotmail.com> wrote in message
news:%23QmnHWVZFHA.1868@TK2MSFTNGP14.phx.gbl...
> Hello Friends,
>
> I am mainly a ASP web developer so I have been looking into the possibilty
> of using an "include" style approach to adding code to my VB6 forms. In
> ASP you can place <!--#include file="mypath/mypage.asp" --> at the top of
> any document to prevent having to retype the same code in every document.
> Is there something like this for VB6...? I have an app that uses several
> forms and they all need the same large chunk of code in the General
> Declarations.
>
> I am familar with making public subs, functions and other global data but
> I have not found a solution to my problem using these approaches.


Not sure exactly what code you want to reproduce, but if it is variables,
then create a class, and create an instance for each form -- then all your
variables will be specific to that form.

If you have code to use, then, as Steven suggested, put it into a module.
You may have to pass the form to use as a parameter to all those functions.
I do this a lot.

But without more explanation, I dont know which solution, if either, will
work for you.

Matt


Steven Burn

2005-05-30, 8:55 pm

Both of the methods suggested will work..... though personally, I tend =
to go with modules over classes as I find them easier to work with for =
some reason :o\

--=20
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

"YYZ" <none@none.com> wrote in message =
news:#HUvboVZFHA.1152@tk2msftngp13.phx.gbl...
> "Vincent" <vincent938@hotmail.com> wrote in message=20
> news:%23QmnHWVZFHA.1868@TK2MSFTNGP14.phx.gbl...
possibilty=20[color=darkred]
In=20[color=darkred]
the top of=20[color=darkred]
document.=20[color=darkred]
several=20[color=darkred]
data but=20[color=darkred]
>=20
> Not sure exactly what code you want to reproduce, but if it is =

variables,=20
> then create a class, and create an instance for each form -- then all =

your=20
> variables will be specific to that form.
>=20
> If you have code to use, then, as Steven suggested, put it into a =

module.=20
> You may have to pass the form to use as a parameter to all those =

functions.=20
> I do this a lot.
>=20
> But without more explanation, I dont know which solution, if either, =

will=20
> work for you.
>=20
> Matt=20
>=20
>=20


YYZ

2005-05-31, 3:57 am


"Steven Burn" <somewhere@in-time.invalid> wrote in message
news:%23BX6csVZFHA.2756@tk2msftngp13.phx.gbl...
Both of the methods suggested will work..... though personally, I tend to go
with modules over classes as I find them easier to work with for some reason
:o\

Oh, absolutely! However, the last part of Vincent's message mentioned that
he was familiar with making public subs, functions, gllobals, etc, and that
thye wouldn't work for him. That's the only reason I suggested the class
approach. :)

Matt


Vincent

2005-05-31, 3:57 am

Hey Steve and Matt,

Thanks for the input. Here is the code that goes into the General
Declarations:

Option Explicit

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As
Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X
As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal
hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)
As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long,
ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const SWW_HPARENT = (-8)
Private Const WM_MOVE = &H3
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Const WM_SIZE = &H5
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Sub ReleaseCapture Lib "user32" ()

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long,
lpRect As RECT) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long,
ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As
Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWndLock As
Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal
nCmdShow As Long) As Long
Private Enum ShowCommands
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_MAX = 10
End Enum

Private Declare Function ExcludeClipRect Lib "gdi32" (ByVal hDC As Long, _
ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As
Long
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, _
ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
Private Declare Function GetClipRgn Lib "gdi32" (ByVal hDC As Long, _
ByVal hRgn As Long) As Long
Private Declare Function OffsetClipRgn Lib "gdi32" (ByVal hDC As Long, _
ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function DrawFrameControl Lib "user32" (ByVal hDC As Long, _
lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) As Long
Private Declare Function DrawIconEx Lib "user32" (ByVal hDC As Long, ByVal
xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As
Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, hbrFlickerFreeDraw
As Long, ByVal diFlags As Long) As Long
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC
As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal
wFormat As Long) As Long
Private Const DT_BOTTOM = &H8
Private Const DT_CENTER = &H1
Private Const DT_LEFT = &H0
Private Const DT_VCENTER = &H4
Private Const DT_TOP = &H0
Private Const DT_SINGLELINE = &H20
Private Const DT_RIGHT = &H2
Private Const DT_WORDBREAK = &H10
Private Const DT_CALCRECT = &H400
Private Const DT_WORD_ELLIPSIS = &H40000

Private Type DRAWITEMSTRUCT
CtlType As Long
CtlID As Long
itemID As Long
itemAction As Long
itemState As Long
hwndItem As Long
hDC As Long
rcItem As RECT
itemData As Long
End Type

Public Enum SysMet
SM_CXSCREEN = 0
SM_CYSCREEN = 1
SM_CXVSCROLL = 2
SM_CYHSCROLL = 3
SM_CYCAPTION = 4
SM_CXBORDER = 5
SM_CYBORDER = 6
SM_CXDLGFRAME = 7
SM_CYDLGFRAME = 8
SM_CYVTHUMB = 9
SM_CXHTHUMB = 10
SM_CXICON = 11
SM_CYICON = 12
SM_CXCURSOR = 13
SM_CYCURSOR = 14
SM_CYMENU = 15
SM_CXFULLSCREEN = 16
SM_CYFULLSCREEN = 17
SM_CYKANJIWINDOW = 18
SM_MOUSEPRESENT = 19
SM_CYVSCROLL = 20
SM_CXHSCROLL = 21
SM_DEBUG = 22
SM_SWAPBUTTON = 23
SM_RESERVED1 = 24
SM_RESERVED2 = 25
SM_RESERVED3 = 26
SM_RESERVED4 = 27
SM_CXMIN = 28
SM_CYMIN = 29
SM_CXSIZE = 30
SM_CYSIZE = 31
SM_CXFRAME = 32
SM_CYFRAME = 33
SM_CXMINTRACK = 34
SM_CYMINTRACK = 35
SM_CXDOUBLECLK = 36
SM_CYDOUBLECLK = 37
SM_CXICONSPACING = 38
SM_CYICONSPACING = 39
SM_MENUDROPALIGNMENT = 40
SM_PENWINDOWS = 41
SM_DBCSENABLED = 42
SM_CMOUSEBUTTONS = 43
SM_CMETRICS = 44
End Enum

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As
Long) As Long

Private Const WM_GETSYSMENU = &H313
Private Const WM_NCPAINT = &H85
Private Const WM_DRAWITEM = &H2B
Private Const WM_ACTIVATE = &H6
Dim IsInFocus As Boolean
Dim xad As Long

Implements ISubclass
Private m_emr As EMsgResponse

Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As
Long
Private Const COLOR_ACTIVECAPTION = 2
Private Const COLOR_INACTIVECAPTION = 3
Private Const COLOR_INACTIVECAPTIONTEXT = 19
Dim colorActive As Long
Dim colorInActive As Long
Dim gColor1 As Long, gColor2 As Long
Dim gAngle As Long
Private mGradient As New clsGradient

The "Implements ISubclass" seems to not want to go into a bas file.

Thanks again for any help. :)




Vincent

2005-05-31, 3:57 am

Me again. :)

Here's the project I am working with:
http://www.planetsourcecode.com/vb/...d=8445&lngWId=1

The main form has a truck load of code in it. I am trying to not have to
add the code to each form in my project but have a centralized area. I hope
this makes more sense.

Thanks again. :)


YYZ

2005-05-31, 3:57 am

"Vincent" <vincent938@hotmail.com> wrote in message
news:OdqRbZYZFHA.796@TK2MSFTNGP10.phx.gbl...
> Hey Steve and Matt,
>
> Thanks for the input. Here is the code that goes into the General
> Declarations:


Okay, at first glance, all those API calls and constants and types should go
into a module as public -- I have a module in almost every project called
modAPI.bas, and it contains a bunch of this stuff.

As for the Implements ISubclass, I believe that that is the way of
subclassing from VBAccelerator.com -- if so, then I don;t think you can put
that into a module. But, if you got rid of the rest, then adding in
Implements ISubclass wouldn't really be a problem, right?

Try that and come back if you have any problems.

Remember, change all the Privates to Publics. That sounds dirty.

Matt


Vincent

2005-05-31, 3:57 am

Thanks for the help, Matt. :)

I have added all the code to a mod and changed the Private to Public
(Nothing dirty here) :) . I added the "Implements ISubclass" to the main
form in the general declarations but I receive an error:
Compile Error
Object module needs to implement 'MsgResponse' for interface 'ISubclass'.

Thanks.
V


Michael Cole

2005-05-31, 3:57 am

Vincent wrote:
> Thanks for the help, Matt. :)
>
> I have added all the code to a mod and changed the Private to Public
> (Nothing dirty here) :) . I added the "Implements ISubclass" to the
> main form in the general declarations but I receive an error:
> Compile Error
> Object module needs to implement 'MsgResponse' for interface
> 'ISubclass'.
>
> Thanks.
> V


For each of the methods in form that are prefixed ISubclass_, just open each
of them in turn so that the property and end property exist. They just
simply need to be created.

--
Regards,

Michael Cole


YYZ

2005-05-31, 3:55 pm

"Michael Cole" <noone@hansen.com> wrote in message
news:et01n%23ZZFHA.2756@tk2msftngp13.phx.gbl...
> Vincent wrote:
>
> For each of the methods in form that are prefixed ISubclass_, just open
> each
> of them in turn so that the property and end property exist. They just
> simply need to be created.


In case you didn't understand Michael's instructions, in VB, in the dropdown
on the left side at the top of the code window, choose "ISubclass" -- then
in the combo on the right, click on each -- you have have 3 procedures
created. You DO need to code some things in there, I believe -- here's what
I have, at a minimum for the form that implements ISubclass


Private Property Let ISubclass_MsgResponse(ByVal RHS As
SSubTimer6.EMsgResponse)
'
End Property

Private Property Get ISubclass_MsgResponse() As SSubTimer6.EMsgResponse
ISubclass_MsgResponse = emrConsume
End Property

Private Function ISubclass_WindowProc(ByVal hWnd As Long, ByVal iMsg As
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'
End Function

Now, if you are going to subclass then you will do most of your work in the
_WindowProc function. But I bet your original form has these 3 methods, and
you'll probably want to copy them from there.

Matt


Ken Halter

2005-05-31, 3:55 pm

"Steven Burn" <somewhere@in-time.invalid> wrote in message
news:%23BX6csVZFHA.2756@tk2msftngp13.phx.gbl...
Both of the methods suggested will work..... though personally, I tend to go
with modules over classes as I find them easier to work with for some reason
:o\

I'm the opposite. To me, classes are easier. For one thing, there's no need
to worry about variable, procedure or API declarations stomping on each
other if they're private to the class (I guess you can do that with modules
too but...) Regardless, I tend to put stuff in class modules. I like the
ability to instantiate them only when needed.


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..


Steven Burn

2005-05-31, 3:55 pm

Thats my main problem lol........ I tend to forget to instantiate =
them..... with modules I don't have to worry about it <vbg>

--=20
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message =
news:u4bf9HfZFHA.3840@tk2msftngp13.phx.gbl...
> "Steven Burn" <somewhere@in-time.invalid> wrote in message=20
> news:%23BX6csVZFHA.2756@tk2msftngp13.phx.gbl...
> Both of the methods suggested will work..... though personally, I tend =

to go=20
> with modules over classes as I find them easier to work with for some =

reason=20
> :o\
>=20
> I'm the opposite. To me, classes are easier. For one thing, there's no =

need=20
> to worry about variable, procedure or API declarations stomping on =

each=20
> other if they're private to the class (I guess you can do that with =

modules=20
> too but...) Regardless, I tend to put stuff in class modules. I like =

the=20
> ability to instantiate them only when needed.
>=20
>=20
> --=20
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Sign up now to help keep VB support alive - =

http://classicvb.org/petition
> Please keep all discussions in the groups..=20
>=20
>=20


Veign

2005-05-31, 3:55 pm

Plus why load code into memory that may only be used once during an
applications lifetime or for a very short time.

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/


"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:u4bf9HfZFHA.3840@tk2msftngp13.phx.gbl...
> "Steven Burn" <somewhere@in-time.invalid> wrote in message
> news:%23BX6csVZFHA.2756@tk2msftngp13.phx.gbl...
> Both of the methods suggested will work..... though personally, I tend to

go
> with modules over classes as I find them easier to work with for some

reason
> :o\
>
> I'm the opposite. To me, classes are easier. For one thing, there's no

need
> to worry about variable, procedure or API declarations stomping on each
> other if they're private to the class (I guess you can do that with

modules
> too but...) Regardless, I tend to put stuff in class modules. I like the
> ability to instantiate them only when needed.
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Sign up now to help keep VB support alive - http://classicvb.org/petition
> Please keep all discussions in the groups..
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com