In VB 2008, can you transfer parameters from previous form to a new form?

razz

Well-known member
Joined
Oct 11, 2008
Messages
67
Programming Experience
Beginner
I am using VB 2008 and need to setup all forms to identical parameters. So far I have been setting up each form individually (i.e. font, color, size etc.).
Is there a way to transfer parameters from one form to another?
 
You can set those properties in an empty "base" form and have all the other forms inherit from that base form.
 
Sorry JuggaloBrotha, I am very new to all this stuff and I don't know how to accomplish what you said to do. Please elaborate. Thank you for your time.
 
Create a form to use as a "Template" or base form. Just an empty form with the properties set how you would like. Notice I said properties as you mentioned font, color size, etc. In programming, the term parameter is synonymous with argument, a value that is passed to a routine whereas a property is a characteristic of an object.

To have a new form inherit from the base form, in Solution Explorer, right-click the project, select Add, and then select New Item. In the Add New Item dialog box, select the Windows Forms category (if you have a list of categories) and then select the Inherited Form template. Set the name and click Add. In the Inheritance Picker dialog box, select your base form as the form to inherit from and click OK. This creates a form that derives from the form.

To have an existing form use the properties defined in the "template" or base form, you need to change the inherits statement for the form from the standard System.Windows.Forms.Form to your new form. To do this, you'll need to show all files. In the solution explorer, click the 'Show all files' icon at the top. The forms will now have a node expanding '+' to the right of their names in the solution explorer. Expand this node for the form you want to have inherit the base form and you'll see a file called formname.designer.vb where formname is the name of the form. Open the designer file and look for Inherits System.Windows.Forms.Form. Change that to Inherits BaseFormClassName; BaseFormClassName being the name of your "template" or base form. I'm assuming you're using either VS 2005 or 2008 as stated in your profile. You wouldn't have the designer file in previous versions and would instead change the inherits statement in the only code page for that form.

If you want to add controls to the base form so that the other forms inherit those controls and also want to be able to modify the properties of those controls in forms that inherit the base form, set the Modifiers property of those controls to Protected. Otherwise, set the Modifiers property to Private.
 
In the Add New Item dialog box, select the Windows Forms category (if you have a list of categories) and then select the Inherited Form template. Set the name and click Add. In the Inheritance Picker dialog box, select your base form as the form to inherit from and click OK. This creates a form that derives from the form.
I get to this part and there is no "Inherited Form Template" (if there is, I can't see it). I am using the Express Edition of VB 2008, does that make a difference?
 
Although I usually use VS 2008 Pro, I also have VB 2008 Express installed and the Inherited form template is available for me; see the attached screenshot. This was the first time I used Express so I didn't install anything special, but I'm not sure if having Pro installed on the same machine makes a difference. I have VB 2008 Express Version 9.0.30428.1 SP1Beta1.

If all else fails, you can always modify the designer file, first add a normal form. That will give you a more behind the scenes look anyway.
 

Attachments

  • AddInheritedFormExpress.jpg
    AddInheritedFormExpress.jpg
    95.9 KB · Views: 38
This is how the VBx 2008 "Add new item" dialog looks without the Pro extensions installed ;)

vbx08.png

As mentioned you can add a regular form, go into Designer code and change the Inherits statement. Editing Designer generated code is normally strictly no-no, but for this it's the only way I found with Express. To access Designer code click the Show All icon in Solution Explorer and expand your form node, you should see the subnode formname.Designer.vb.
 
Thank you both of you for all your help, I really appreciate it. I am very close to getting there. Presently I am in the designer code area.
I see the line I need to modify as per Blue line below:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class TemplateForInfo
Inherits System.Windows.Forms.Form

My form is named as per Red above.

Now, to be sure I don't screw up, I need the code that I need to insert in place of Blue above.

Thank you guys!
 
If you have a template form named TemplateForm you can put "Inhertits TemplateForm" there.
 
Still didn't work for me. I think I'll forget about it and just create each form. Thank you for trying guys!
 
Back
Top