Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP Q from a Newbie: Regular Forms 1

Status
Not open for further replies.

bobbarker

Programmer
Dec 20, 2001
83
GB
A question re object orientated programming from a newbie who wants to start doing things "the proper way"...

If I have a form that I want to use across numerous projects (e.g. a user login form with a username text box, password box and 'OK'/'Cancel' command buttons) what is the best way to go about this:
1. Copy an existing form from one project to another? (which I currently do)
2. Create a class module? (Class modules I have done to date do not seem to include form objects etc)
3. Create a custom control? (Can you make a form a custom control?)
4. Other ?...

Any suggestions would be gratefully appreciated.

Cheers.

 
Option 2, with whistles and bells.

Forms can be used with classes by making the form a member of the class. Something along the lines of

dim frmFront as frmFrontForm

That would give you a reference to a frmFrontForm that you could play with.

When, in your class, you need to show your form, you do something like this...

set frmFront = new frmFrontForm
frmFront.Show

And, when you're done with it,

set frmFront = nothing



On the other hand, is Form is really a class anyway (just happens to have a GUI), so you could expose a public methods off a form and then use that in all your projects.
I'd go with embedding a form in a class though, because the next stage from that is to build the class into a DLL, which is something you can easily use (and modify) in all your projects.

Hope this helps...

mmilan
 
Thanks mmilan

I've got the grips of what you are saying. I want to be able to call this form modally from forms within my project(s).

Here's what I have done...
1. Created frmLogin which includes 2 textboxes: txtUsername and txtPassword. An 'OK' button calls me.hide
2. I created a class module called clsLogin. Here's the code:
Code:
Option Explicit
Private mstrUsername As String 'local copy
Private mstrPassword As String 'local copy
Public Property Get Username() As String
    Username = mstrUsername
End Property
Public Property Let Username(ByVal vData As String)
    mstrUsername = vData
End Property
Public Property Get Password() As String
    Password = mstrPassword
End Property
Public Property Let Password(ByVal vData As String)
    mstrPassword = vData
End Property
Public Sub GetUsernamePassword(ByRef frmIn As Form)
    Dim frmLogin As frmLogin
    
    Set frmLogin = New frmLogin
    frmLogin.Show vbModal, frmIn
    mstrUsername = frmLogin.txtUserID.Text
    mstrPassword = frmLogin.txtPassword.Text
    Unload frmLogin
    Set frmLogin = Nothing
End Sub

I now want my parent form (called frmMain) to call this class. My code on this form is:
Code:
Dim oLogin As clsLogin
    
Set oLogin = New clsLogin
oLogin.GetUsernamePassword (frmMain)
MsgBox oLogin.Username & Chr(13) & Chr(13) & oLogin.Password
Set oLogin = Nothing

But "Login.GetUsernamePassword (frmMain)" returns a type mismatch error.

Any suggestions whether I am doing the right thing here would be appreciated.

Thanks.
 
Type mismatch error resolved. Should have been:
Code:
ologin.GetUserNamePassword frmMain
 
Code:
Public Sub GetUsernamePassword(ByRef frmIn As [COLOR=blue]Form[/color])

and you are passing it a reference to a type of form frmMain - type mismatch...

try changing the type in the GetUsernamePassword declartion to frmMain

or
Code:
dim f as form

set f = new frmMain
ologin.getusernamePassword f
may also work.

AS I am not at a VB PC the code above ain't tested!




Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I'm a tad busy at the mo... Writing a beastly import routine... In about an hour or so there should be a period of testing the blipping thing which involves not coding for about an hour, so I'll try and look at this then for you...

mmilan
 
Thanks mmilan. Appreciate your help.

My class module now works.
How do I create this as a DLL as per mmilans original suggestion.

My attempt has failed.

I created a ActiveX DLL. Added my class module

Code:
Option Explicit
Private mstrusername As String 'local copy
Private mstrPassword As String 'local copy
Public Property Get Username() As String
    Username = mstrusername
End Property
Public Property Let Username(ByVal vData As String)
    mstrusername = vData
End Property
Public Property Get Password() As String
    Password = mstrPassword
End Property
Public Property Let Password(ByVal vData As String)
    mstrPassword = vData
End Property
Public Sub GetUsernamePassword(ByVal frmIn As Object)
    Dim frmLogin As frmLogin
    Dim frmMain As Form
    
    Set frmLogin = New frmLogin
    Set frmMain = frmIn
    frmLogin.Show vbModal, frmMain
    mstrusername = frmLogin.txtUserID.Text
    mstrPassword = frmLogin.txtPassword.Text
    Unload frmLogin
    Set frmLogin = Nothing
    Set frmMain = Nothing
End Sub

and my Login form. And then compiled it.

I then create a standard EXE project and a reference to my DLL.

I added the following code:
Code:
Dim oLogin As New LoginForm.clsLogin
    
oLogin.GetUsernamePassword Form1

But " oLogin.GetUsernamePassword Form1" returns
Run-time error '373': The interaction between compiled and design environment components is not supported.
 
It didn't seem to like calling the owner form in my GetUsernamePassword method. I have simply changed it to
Code:
frmLogin.Show vbModal
and it appears to be OK.
Would appreciate anyones thoughts as to why this may have been a problem.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top