|
-
Sep 10th, 2000, 11:47 AM
#1
Thread Starter
Member
I have to make more than 30 forms in an application with different contents but the form properties eg size, color, startup postion etc. are same.
is there any way that i make one form and then use the copies of same form with some link to master form (original) ie if i make any change to master form like color or any thing else, that will apply automaticaly to copies of that form.
please note that the contents of all these forms are entirely different.
thanx in advance for any help from VB MASTERS !
-
Sep 10th, 2000, 11:53 AM
#2
You can load different instances each time.
Code:
Dim frm As New frmMaster
frm.Show
-
Sep 10th, 2000, 11:55 AM
#3
I think the only way to do this is during run-time. Add a BAS module to the project and copy this code to it:
Code:
Public Sub ShowForm(frmToShow As Form, frmTemplate As Form)
With frmTemplate
frmToShow.BackColor = .BackColor
frmToShow.Move .Left, .Top, .Width, .Height
frmToShow.WindowState = .WindowState
End With
frmToShow.Show
End Sub
When you want to load a form call ShowForm and pass the form you want to load as the first argument and the template form as the second.
Good luck!
-
Sep 10th, 2000, 11:57 AM
#4
Originally posted by Megatron
You can load different instances each time.
Code:
Dim frm As New frmMaster
frm.Show
That won't work since...
Originally posted by amer
please note that the contents of all these forms are entirely different.
-
Sep 10th, 2000, 12:13 PM
#5
Thread Starter
Member
i guess that Joacim is right!
Joacim, i heard a lot about classes, can i handle it by using classes, if YES how can i creat class.
thanx n regards
-
Sep 10th, 2000, 07:30 PM
#6
Addicted Member
i don't think using classis is the best way to do this, what Joacim Andersson said is the best way, just add a new form and make it as template, u don't use it for any thing, but when u want to change the colors of your forms or size, just change them in the template form,,,
-
Sep 11th, 2000, 03:24 AM
#7
The template form doesn't necassaraly have to be a new form. You could pass your main form as the template argument to the procedure.
-
Sep 11th, 2000, 04:44 AM
#8
Originally posted by amer
Joacim, i heard a lot about classes, can i handle it by using classes, if YES how can i creat class.
Sure you can do this by creating a class! But as Mih_Flyer already noted; it might not be the best way of doing it.
That's because you can't set properties of classes during design time but for a form you can. So it would be easier to use a template form.
A form also acts very much like classes.
Now that I've mentioned that let us see how you can implement it like a class.
You could do this by either adding a private class to your project or by adding an ActiveX DLL project so that you make a project group.
I think the earlier is a little bit simplier though so lets start out that way.
On the Project menu click on Add Class Module.
A new Class Module window named Class1 will then appear. They look very much like a standard module but acts differently.
Rename the class to CFormTemplate by changing the Name property in the Property List Window.
Now you want to add properties to this class. The properties you are interested in is:[*]BackColor[*]Top[*]Left[*]Width[*]Height[*]WindowState
You might come up with other properties that you want to add to the Form Template but I leave that as an exercise for you 
There are two ways of creating properties either like property procedures or as public variables. Property procedures is to prefer though since you don't know when a public variable is changed. With property procedures you can validate if the new property value is valid or not which you can't do with a public variable.
A property procedure is actually two procedures; a Property Let (or Property Set if the property value actually is an object instead of a simple value; Font and Picture is examples of Object properties) and a Property Get procedure.
The Property Let is called when you assign a new value to the property and the Property Get is called when you want to retrieve the value.
So add the following code to the class module:
Code:
Option Explicit
'These are private variables to hold the
'property values.
Private m_BackColor As OLE_COLOR
Private m_Left As Integer
Private m_Top As Integer
Private m_Width As Integer
Private m_Height As Integer
Private m_WindowState As FormWindowStateConstants
Public Property Let BackColor(NewValue As OLE_COLOR)
m_BackColor = NewValue
End Property
Public Property Get BackColor() As OLE_COLOR
BackColor = m_BackColor
End Property
Public Property Let Left(NewValue As Integer)
m_Left = NewValue
End Property
Public Property Get Left() As Integer
Left = m_Left
End Property
Public Property Let Top(NewValue As Integer)
m_Top = NewValue
End Property
Public Property Get Top() As Integer
Top = m_Top
End Property
Public Property Let Width(NewValue As Integer)
m_Width = NewValue
End Property
Public Property Get Width() As Integer
Width = m_Width
End Property
Public Property Let Height(NewValue As Integer)
m_Height = NewValue
End Property
Public Property Get Height() As Integer
Height = m_Height
End Property
Public Property Let WindowState(NewValue As FormWindowStateConstants)
m_WindowState = NewValue
End Property
Public Property Get WindowState() As FormWindowStateConstants
WindowState = m_WindowState
End Property
Now you probably want these properties to have default values from the beginning so that if you don't set one value the default is returned.
A class module have two events; Initialize and Terminate. The Class_Initialize event is raised when you first create an object from the class. This is a perfect place to add the default values to the properties.
So add the following code to that event:
Code:
Private Sub Class_Initialize()
m_Height = 3600
m_Width = 4800
m_WindowState = vbNormal
m_BackColor = vbButtonFace
m_Top = 0
m_Left = 0
End Sub
Now it's time to add a Method that you call when you want to load a form. This Method is very simular to the procedure I gave you in my first post. The difference is that you only have to pass the form name of the form that you want to load and not a template form since the class it self is the template. I also added two optional arguments that you can use if you want the form to be modal and to state the owner form. These are the same as the arguments as the Show method of a Form has.
Copy and paste the following code to the class module:
Code:
Public Sub ShowForm(frmToShow As Form, _
Optional Modal As FormShowConstants = vbModeless, _
Optional OwnerForm As Form)
With frmToShow
.Move m_Left, m_Top, m_Width, m_Height
.WindowState = m_WindowState
.BackColor = m_BackColor
If OwnerForm Is Nothing Then
frmToShow.Show Modal
Else
frmToShow.Show Modal, OwnerForm
End If
End With
End Sub
Now the class is all done! The only thing to do now is to create an object from the class.
Since you have to be able to use this object through out the project it have to be declared as Public.
If you don't already have a BAS module in your project add one now and declare the object in it in the following manner:
Code:
Public objTemplate As CFormTemplate
Since you now going to use this object to load your forms you must change the Startup object of your project.
Click on ProjectName Properties on the Project menu and change the Startup Object to Sub Main.
Now that you have change that you have to add the Main sub. You write that in the BAS module like this:
Code:
Public Sub Main()
'initialize the object
Set objTemplate = New CFormTemplate
'set the property values of the template object
'change this to whatever you want to have
With objTemplate
.BackColor = vbRed
.Height = 8000
.Width = 10000
'center all forms
.Top = (Screen.Height - .Height) \ 2
.Left = (Screen.Width - .Width) \ 2
'this call isn't really necassary because
'vbNormal is the default value
.WindowState = vbNormal
.ShowForm Form1 'Change this to the name of your main form
End With
End Sub
Now every time you want to load a new form simply call the ShowForm method of the CFormTemplate class. Here's an example:
Code:
objTemplate.ShowForm Form2, vbModal, Form1
Best regards and good luck!
-
Sep 11th, 2000, 04:48 AM
#9
Addicted Member
you don't even have to make a template form, you can just define public variables as:
BKColor, TxtColor,,,,etc.
and in the event load of every form change the properties according to those variables..i think this is easier.
-
Sep 11th, 2000, 05:00 AM
#10
Conquistador
i think joacim's first form example is easier
then there is not as much code also!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|