Monday, February 16, 2009

A small functionality change.

Here's what can happen when a small functionality change is required in a fairly complex application. In this case, there are a few TabPages that are generated dynamically. Some of the TabPages have a 'Save Details' button.

The change: one of the TabPages now should NOT have a 'Save Details' button if the object is a 'new' object (not yet saved in the database). Easy!

Solution:

this.tabControl.Controls.Add(this.mySomethingTabPage);

HideOrShowSaveButton(this.mySomethingTabPage); // solution

...

public void HideOrShowSaveButton(TabPage tabPage)
{
//'save details' should not appear on the 'MySomething' tab page if
//the object is a new object

//find 'save details' button

ToolStripButton mySmallSaveButton = null;

foreach (Control c in this.mySomethingTabPage.Controls)
{
SomeDetails.SomeDetailsHeadingView pdhv = c as SomeDetails.SomeDetailsHeadingView;

if (pdhv != null)
{
foreach (Control c1 in pdhv.Controls)
{
ToolStrip headingToolStrip = c1 as ToolStrip;

if (headingToolStrip != null)
{
foreach (ToolStripItem item in headingToolStrip.Items)
{
if (item.Name == "SaveButton")
{
mySmallSaveButton = item as ToolStripButton;

//hide button on the 'someDetails' tab if new object
if (mySmallSaveButton != null)
{
if (Global.currentObject.IsNewObject)
{
mySmallSaveButton.Visible = false;
mySmallSaveButton.Enabled = false;
}
else
{
mySmallSaveButton.Visible = true;
mySmallSaveButton.Enabled = true;
}
return;
}
}
}
}
}
}
}
}

Had to hardcode the 'SaveButton' name which I would really want to avoid, but did not want to spend more time on that.

Thoughts: this application could have been designed a little better to make future changes not so painful.

by . Also posted on my website

No comments: