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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual Studio 2005/VB.net - can I copy a form? 1

Status
Not open for further replies.

meryls

Technical User
Nov 20, 2003
62
US
I need to create many forms that will have similar fields. each form being connected to a different data source. For example:

Form A is connected to database table A which has Code, Name, Description and Note
Form B is connected to database table B which has Code, Name, Description, Note and Value X and Value Y.
and so on...

I want each of my forms to look the same, in that each should have the label and field for Code in exactly the same place, label and field for Name in the same place, label and field for Description in the same place, etc. Fields that are different will be placed and aligned in a similar fashion.

I thought I could create Form A, connected to Table A, save it, then do a "save as" to make a copy of Form A, as Form B, change the data source to Table B and add the new or changed fields. (Like I could do in many other applications, e.g., Word, Crystal reports, Photoshop, etc.)

I have about 20 of these.

Can this be done? When I use "save as" if VisualStudio just renames my existing form and doesn't duplicate it.

Thanks for your help!
Meryl


 
Copy paste works. And inheritance should also work as long as the form doesn't get to complicated, else the designer will not like you anymore (but notepad will ;-))

Christiaan Baes
Belgium

My Blog
 
Christiaan ,

Thanks for your reply. I tried the copy and paste, but as soon as I try to open the copied form, I get a whole set of error messages, starting and ending with these:

Failed to parse method 'InitializeComponent'. The parser reported the following error 'Error HRESULT E_FAIL has been returned from a call to a COM component.'. Please look in the Task List for potential errors.
Microsoft.VisualStudio.Design.Serialization.CodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
.
.
.
.
Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32.fReload)

Thanks for your help!
Meryl
 
Yeah you get these because it "forgets" to update the class name to the filename. So you have to change those manually. Both in the vb code-file as in the vb designer-file.


Christiaan Baes
Belgium

My Blog
 
In solution explorer, select the form you want to copy, right click and select copy. Then select the solution name, right click and select paste. The pasted form will then be called something like: copy of Form1.vb

Rename the form and then open it in CODE view NOT FORM view.
At the top, it will have something like: Public Class Form1 and Form1 will probably have a wavy green underline. Change this to Form2 (and don't forget to save), and all should be well.

Hope this helps.

[vampire][bat]
 
[rofl]

You type faster than me - yes and before you say it old age

... but in 2008 you don't need to edit the designer (it refactors itself)

2005 must be really primitive it it doesn't also [wink]

[vampire][bat]
 
Is that possible to create some procedure that act like CSS in HTML files?
Code:
for each control in me.controls
if typeof control is textbox then
control.backcolor=??
control.forecolor=??

elseif....

elseif...

end if
next



________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Yeah it could but then I would use inheritance instead, actually I would use WPF instead ;-).

Christiaan Baes
Belgium

My Blog
 
Zameer, as chrissie, yes it will work - but you would need to use a recursive routine if you wanted to also change the properties of controls in a container control such as a panel.

[vampire][bat]
 
Yeah.. something like this..
Code:
AllControls = New Collection
Code:
      Dim CTL As Control
        Dim Container As Object

        For Each CTL In frm.Controls
            Allcontrols.Add(CTL)
        Next

        For Each Container In frm.Controls
            If Container.HasChildren = True Then
                For Each CTL In Container.Controls
                    Allcontrols.Add(CTL)
                Next
            End If
        Next

can show a sample of Inheritance?

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
That won't pick up all nested controls - you would need something more like this:

Code:
Public Class Form1

	Private AllControls As New List(Of Control)

	Private Sub GetAllControls(ByVal ctrl As Control)

		For Each c As Control In ctrl.Controls
			AllControls.Add(c)
			If c.HasChildren Then
				GetAllControls(c)
			End If
		Next

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		GetAllControls(Me)

	End Sub

End Class


Hope this helps.

[vampire][bat]
 
meryls,

You could also create your one form and then create new instances of that form. That way you only ever have to update one form if you make changes. It creates more codeing because you have to create all the databindings as well, but if the tables are all the same (column names) then that makes it a little better.

-I hate Microsoft!
-Forever and always forward.
 
To Chrissie,
I have tried WPF with VB Express 2008.
Not understand completely. All the
controls that are available in Forms are not there
in WPF windows. Databinding is complicated than form app.
Designing is very easy( May be I am wrong on all these statements.)

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Zameer, I've been struggling to get my head around WPF as well. When I've got some more time I'll work through it slowly - until then WinForms!

[vampire][bat]
 
Thanks Chrissie, I think I have seen that before. I will try to read

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top