Form, Form1 and TextBox

1. Form, Form1 and TextBox are all classes.
2. Form is the class of Form1.
3. For any class, we can do
    Dim objectName As className
    Set objectName = New className
4. But why I can do that only with From1?
learnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amebaCommented:
Controls are special type of classes. You cannot create NEW Control without some kind of container (e.g. Form, UserControl, ...). And you have to do it through containers Controls collection.
Syntax is (in VB6 only):
Private Sub Form_Click()
    Dim oControl As Control
    Set oControl = Me.Controls.Add("VB.TextBox", "Ctl1")
    oControl.Move 100, 100
    oControl.Visible = True
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
learnAuthor Commented:
To  ameba:

OK, I will try your code.

But how about class Form? I can not do
Dim objectName As Form
Set objectName = New Form
0
amebaCommented:
Private Sub Form_Click()
    Dim objectName As Form
    Set objectName = New Form1
    objectName.Move Me.Left + 100, Me.Top + 100
    objectName.Show
End Sub

or use Forms Collection:
Private Sub Form_Click()
    Dim x As Form
    Set x = Forms.Add("Form1")
    x.Left = Me.Left + 100
    x.Show
End Sub
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

caraf_gCommented:
Classes can have all sorts of properties that define whether and how they can be instantiated.

The "Form" class must be defined as Public but not Creatable, and therefore you can use the Form Class in a declaration

Dim objX As Form

But not in a "new" statement

Set objX = New Form

The reason for this is that the Form class is only used to define an interface for all "Form" objects. In other words, VB automatically implements this interface for you when you create a new form type class, or add a form to your project.

I don't know how much you know about interfaces so it is hard to write an explanation that fits your level of experience...
0
learnAuthor Commented:
To caraf_g:

Thank you for your help.

I think know nothing on interface :-( Can I find something to read for that?

And can I say
1. TextBox in the Tool Box is a class and Text1 in the form is an object (instance) of class TextBox?
2. Form1 is an object (instance) of class Form?
3. Form1 itself is a class?
0
amebaCommented:
1. & 3. Yes
2. if you instatiate it, then Yes.
0
caraf_gCommented:
ameba's said it.

Form1 is a nasty one, because of the way VB handles it.

The sad thing is that VB allows you to do the following (for example):

Form1.Show

Compare this with the following:

String = "hello"
Long = 5

The latter looks wrong, surely it should be
Dim AVariable As String
AVariable = "hello"

And the same should be true for Form1. Form1 is only your definition of a form - a "class". To actually create a form you should instantiate it.

But VB allows you to use the definition itself as if it were an actual object variable. That's what causes so much confusion.

A very quick and incomplete mention of "interfaces".

To use an interface, do the following:

1 Create a "dummy" class, with empty shells for all its properties and methods, e.g. clsI01

Option Explicit
Public Property Let SomeProperty(NewValue As SomeType)
'No code at all here!
End Property
Public Property Get SomeProperty() As SomeType
'no code
End Property
Public Sub DoSomething()
'...
End Sub
Public Function GetSomething() As SomeType
'...
End Function
<etcetera>

Now you can use this class as an interface for new classes. To do that, use the Implements keyword:
clsImplements01


Option Explicit
Implements clsI01

Try and compile this, it won't let you! Because you're implementing clsI01, you now MUST provide all the properties and methods of the interface. To do that, use the underscore notation, very similar to event procedures.

Please note that where the original procedures in clsI01 are Public, the new procedures in clsImplements01 are Private

Private Sub clsI01_DoSomething()
'this is where you DO put code
End Sub

You can also add new properties and procedures that are not part of the interface
Public Sub DoSomethingElse()
'...
End Sub

This is how you use it.

Dim objX As clsImplements01
'instantiate, etc...

'This will work
objX.DoSomethingElse

'But this won't
objX.DoSomething


To use the interface:
Dim objY As clsI01
Set objY = objX 'is OK
'or...
Set objY = New clsImplements01

objY.DoSomethingElse 'will not work because you're only using the clsI01 interface
objY.DoSomething 'will work now.


And this only scratches the surface of it. Check out the VB help on the Implements keyword, and on Interfaces.
0
learnAuthor Commented:
To caraf_g:

Wow, not easy for me..I will study your code carefully and will try to run it ans guess what is that for :-)

Thank you so much for you help!
0
amebaCommented:
Thanks for your points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.