For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > Newbie question: Create multiple forms from template









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie question: Create multiple forms from template
cefrancke@yahoo.com

2005-12-18, 7:57 am

I have designed a generic "DataInput" form that I would like to re-use
depending on what menu item the user has chosen.


Say I have a menu with three items, where each menu item needs to
provide a form for data display and input.
Menu items: DataInputA, DataInputB and DataInputC (this is the text in
the menu)


Now if the user chooses DataInputA, I would like to create a form based

on the generic "DataInput" form (class) I already designed.


Then, at some point before displaying the new form, I want to set all
the control text, data, etc properties that are relevent to the menu
choice.


I figure, on the menu item click event, call a method to create the new

form based on the generic class. I send the title bar text, based on
menu choice.


private void subSystemsToolStripMenuItem_Click(object
sender,
EventArgs e)
{
CreateDataInput("SubSystems");
}


private void CreateDataInput(String frmTitleBarText)
{
frmDataInput FormDataInput = new frmDataInput();
FormDataInput.MdiParent = this;
FormDataInput.Text = frmTitleBarText;


//Before I show the form, I need to set all the control

properties on the new form.


FormDataInput.Show();
}


Being new, I'm not sure if this approach is a good practice or even
practical.
If this approach is not too unusual, I'm not sure where to set the
properties of the new form.


Should this be in the contructor of the new form? If so, I could call a

method, from the constructor to determine what menu item is clicked and

use a Select block for each situation.
How would I pass this info to the constructor?


Or if not in the constructor, I can call a method before the Show
method (see the above code snippet) to decide how to set up the new
form, but how do I get to the "private" controls of the new form. (I
got an error saying, the control is inaccessable due to its protection
level).


Any sample code, snippets or a reference would be a big help.


TIA

Bruce Wood

2006-01-10, 4:09 am

I would approach the problem this way. If this doesn't work for you,
let me know why, and I'll try to propose something else.

First, design your template form with only the controls / information
that are common to all three specialized forms. If there are any
controls that the specialized form will need to change (for example
adding new menu items to a menu) then set that control (for example the
menu itself) to "protected" in the control's property sheet.

Second, derive the three more specific forms from the template, like
this:

public class DataInputA : DataInput
{
...
}

You should be able to work with DataInputA in the designer and put any
special controls on it that it needs in order to display what it needs
to display / accept whatever input it requires.

Now, you need to populate an instance of DataInputA with some
information, and then get results back from it. Create _properties_ on
DataInputA that allow you to set values. You will probably not need one
property for each value. If, for example, DataInputA allows the user to
see a Customer, then it's enough to have one Customer property in your
DataInputA class:

public Customer Customer
{
get { return this._customer; }
set
{
this._customer = value;
if (this._customer == null)
{
... clear all fields on the form ...
}
else
{
... set all fields on the form with data from the new
customer in this._customer ...
}
}
}

As for the result, usually you can just use DialogResult, which is part
of every form. If you need a more sophisticated result, create a
property to return that.

You should _not_ pass stuff on the constructor: if the form has no
empty constructor then the Designer can't work with it. It's better to
have a property.

You also cannot make the template an "abstract" class or the Designer
will not be able to work with it. If you never want anyone to be able
to instantiate the template class directly, just make its constructor
"protected" instead of public.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com