Showing posts with label Form Inheritance. Show all posts
Showing posts with label Form Inheritance. Show all posts

Tuesday, February 12, 2008

Visual Inheritance C#


Often we come across the necessity of having a same design for the whole project like that of a web application.In web application thats made possible with the css.In windows applications we use of Visual Inheritance for that.

Today we'll discuss about such a visual inheritance with examples.

We'll see one form of visual inheritance in which we make a baseform which remains common for all the forms in the application.We can add all the needed controls to it and switch the visibility on and off with the help of properties.


For our example,our initial requirement is to design a baseform on which the whole application depends.

Create a new project and open a new form.Name it BaseForm.

This form contains 4 controls,
1.A panel
2.A Save Button
3.An Update Button
4.A Reset Button

When we design a form as base form,the controls , properties and events of the controls will be hidden from the derived form.Include the following code to make the necessary control properties and events available in the derived form.

public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
}

///
/// To make the controls visible and invisible
/// in the derived form
///

#region ButtonVisibilitySettingsRegion
public bool SaveButtonVisible
{
get { return _saveButton.Visible; }
set { _saveButton.Visible = value; }
}

public bool UpdateButtonVisible
{
get { return _updateButton.Visible; }
set { _updateButton.Visible = value; }
}

public bool ResetButtonVisible
{
get { return _resetButton.Visible; }
set { _resetButton.Visible = value; }
}
#endregion

///
/// To enable the Panel Docking facility
///

public DockStyle PanelIsDocked
{
get { return _basePanel.Dock; }
set { _basePanel.Dock = value; }
}

///
/// To make the control click events available
/// in the derived form
///

#region SettingsUpEventsForTheControls
protected virtual void OnSaveButtonClicked(object sender, EventArgs e)
{
}

protected virtual void OnUpdateButtonClicked(object sender, EventArgs e)
{
}

protected virtual void OnResetButtonClicked(object sender, EventArgs e)
{
}
#endregion
}

Now build the project.Now add another form either an inherited form and give the base form as this or a normal form and change the

public partial class ChangePropertyForm : Form

to

public partial class ChangePropertyForm : BaseForm

The derived form that I designed looks like ,

Here in this form the Save,Update,Reset Comes automatically on changing the Form to BaseForm.Now if we have a look at the properties of the ChangePropertyForm Page,we see in misc there are some new properties,the ones created in the baseform.The properties looks like,

Here we can try changing the visible properties of the buttons and the docking properties.The response is seen right then.

Now to add the click events to the buttons,we need to write the following code in the derived form.

public partial class ChangePropertyForm : BaseForm
{
public ChangePropertyForm()
{
InitializeComponent();
}

private void _browseButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
_filePathText.Text = ofd.FileName;
}

protected override void OnSaveButtonClicked(object sender, EventArgs e)
{
File.WriteAllText(_filePathText.Text, _fileContentText.Text);
MessageBox.Show("Saved");
}

protected override void OnUpdateButtonClicked(object sender, EventArgs e)
{
File.AppendAllText(_filePathText.Text, _fileContentText.Text);
MessageBox.Show("Updated");
}
}

In the examples we can see that the click event in BaseForm is declared as protected virtual.Please make sure that if there is any chance that the event wont be used in the derived form then do not include that virtual keyword.
And one more thing to be remembered is,in the baseform I had clicked the buttons to generate its events first.Then in the code page I renamed the event names and its access types.Then again went to the design page and manually set the method names to the corresponding events.

Now compile the project and run. Wow!! we have successfully completed one visual inheritance example.Now start expanding the concepts and explore the possibilities in visual inheritance..Hey!! dont forget to let me know if you come across any problems..