|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI believe there's an old saying, "Clothes maketh the man", and this definitely applies to software too. No matter how clever and hard-working your applications are, if they're not wearing the latest well-designed clothes, people will think they are poorly written and out of date. While working on a new project recently, I did a quick search on the web to see what the competition was up to. One of the features they had, which I didn't, was an Outlook-style menu bar, commonly referred to simply as an Outlook Bar. Deciding I wanted one too, I thought about writing my own. Searching the Code Project, I found two useful articles, Marc Clifton's An Outlook Bar Implementation, and Outlook XP bar by ACorbs. Both these articles were interesting and thought-provoking, and I downloaded each of them for a bit of a poke around, as one does! I then found myself in a bit of a dilemma. I didn't want to simply copy someone else's code, but at the same time, I didn't want to spend hours and hours reinventing the wheel. I remembered a comment made by my manager at a previous job, many years ago. "What are we trying to do here? Achieve technical excellence, or get a job done?" Realising that Marc and ACorbs had done the former, I decided to go for the latter approach, and started doing some lateral thinking. The eureka moment!I proceeded to rummage around in the standard Windows Forms toolbox, and began to idly play around with splitters and panels. While doing this, I came across the Well, almost... Obviously, all I'd really done was stack three buttons one on top of the other, but they did resize themselves with the panel, which was decent of them. At this point, I realised I was going to have to write some code. Some codeThe only code I really needed is in the void ButtonClick(object sender, System.EventArgs e)
{
// Get the clicked button...
Button clickedButton = (Button)sender;
// ... and it's tabindex
int clickedButtonTabIndex = clickedButton.TabIndex;
// Send each button to top or bottom as appropriate
foreach (Control ctl in panel1.Controls)
{
if (ctl is Button)
{
Button btn = (Button)ctl;
if (btn.TabIndex > clickedButtonTabIndex)
{
if (btn.Dock != DockStyle.Bottom)
{
btn.Dock = DockStyle.Bottom;
// This is vital to preserve the correct order
btn.BringToFront();
}
}
else
{
if (btn.Dock != DockStyle.Top)
{
btn.Dock = DockStyle.Top;
// This is vital to preserve the correct order
btn.BringToFront();
}
}
}
}
// Determine which button was clicked.
switch (clickedButton.Text)
{
case "Cars":
CreateCarList();
break;
case "Outlook Shortcuts":
CreateOutlookList();
break;
case "Zip Files":
CreateZipList();
break;
case "Miscellaneous":
CreateMiscList();
break;
}
// Without this, the buttons will hide the items.
listView1.BringToFront();
}
Basically, when any of the buttons on the panel is clicked, we save the clicked button and its At this stage, I should point out that to make this work, it's vital that the TabIndices of the buttons are set at design time to the correct order. In the supplied example, the "Cars", "Miscellaneous", "Zip Files" and "Outlook Shortcuts" buttons have their Back to the main plot. Having set the
In an attempt to correct this, I tried a few things, such as walking the panel's controls in reverse order, and altering the Finally...The last piece of the jigsaw was another standard Windows Control, the Hopefully, this article will be of interest, and possibly show how sometimes it's useful to look at a problem from a few different angles before getting your code editor out!
|
||||||||||||||||||||||