Creating DropDownControl In C#



 
Introduction

Well, what is DropDownControl?
 
Actually, it is a form having BorderStyle: None, and which can be shown consisting of as many controls as needed for specific operations. Nowdays, all the popular software uses DropDownControl to improve their software quality. Take a look at the Paint application in Windows. Click on the File option. It drops the list of controls, like New,Open,Save,Print etc. Alternatively, open Microsoft Word. See the following image that shows text effects in DropDownControl.

 

In this article, we will create a DropDownControl for a click on a button.

So, how does it work?

Well, it's so simple. Display a DropDownForm when clicking on Button. Once form is shown, invoke the Parent Form events, like Move, GotFocus, Activated, Click etc. When these events are generated, just close the DropDownForm.
 
Procedure 
 
Step 1 : Start Visual Studio and create a new Windows Forms Application in C#. The created form's name could be Form1.

Step 2 : Add another form with the name DropDownForm and set the following properties to it. 
                 
 NameDropDownForm
ControlBox  false
FormBorderStyle  None
 ShowIcon false
 ShowInTaskbar false
 StartPosition Manual
 
Now, Design your form. Add the components to DropDownForm that you wish. I have added Paint application like components. I have designed my form with many Panels and Buttons. You can add custom controls to design a form.
See the above images.

Download the source code.

Now, you have designed your form.
 
Step 3 : Add Form Load event to DropDownForm and set it to activate. 
  1. protected override void OnLoad(EventArgs e)  
  2. {  
  3.     base.OnLoad(e);  
  4.   
  5.     this.Activate();  
  6. }  
Now, move to the Form1.
 
Step 4: Declare the DropDownForm object (I am using _dropDownForm) and Boolean variable for identifying if the DropDownForm is displayed or disposed.
  1. public Boolean isShown = false;  
  2.   
  3. DropDownForm _dropDownForm;  

Step 5 : Drag and drop the Button onto Form1. Add Click events to it.  
  1.       private void button1_Click(object sender, EventArgs e)  
  2.       {  
  3.           if (isShown == true)  
  4.           {  
  5.               _dropDownForm.Close();  
  6.               isShown = false;  
  7.           }  
  8.           else if (isShown == false)  
  9.           {  
  10.               _dropDownForm = new DropDownForm(this);  
  11.               _dropDownForm.Location = new Point(this.Location.X + button1.Location.X + 8,  this.Location.Y + button1.Location.Y + 30);  
  12.               _dropDownForm.Show();  
  13.               isShown = true;  
  14.               _dropDownForm.LostFocus += new EventHandler(_dropDownForm_LostFocus);  
  15.           }  
  16.       }  
  17.   
  18. private void _dropDownForm_LostFocus(object sender, EventArgs e)  
  19.       {  
  20.           if (isShown)  
  21.           {  
  22.               _dropDownForm.Close();  
  23.               isShown = false;  
  24.           }  
  25.       }  
What's this?
  • If isShown is equal to true, then close the DropDownForm.
  • If it is false, then initialize the object by calling its class name.
  • Set its location under your added button by adding Parent Form locations & button height.
  • Then, show the Form & set isShown to true.
  • Then, add LostFocus event to the object of DropDownForm.
  • If focus is lost, just close the displayed form and set isShown to false. 
Step 6 : Now, add Move, GotFocus, Click, Activated events to the Form1 or Parent Form and add the following code to all these events.
  1. protected override void OnMove(EventArgs e)  
  2. {  
  3.     base.OnMove(e);  
  4.     if (isShown)  
  5.     {  
  6.         _dropDownForm.Close();  
  7.         isShown = false;  
  8.     }  
  9. }  
  10.   
  11. protected override void OnGotFocus(EventArgs e)  
  12. {  
  13.     base.OnGotFocus(e);  
  14.     if (isShown)  
  15.     {  
  16.         _dropDownForm.Close();  
  17.         isShown = false;  
  18.     }  
  19. }  
  20.   
  21. protected override void OnClick(EventArgs e)  
  22. {  
  23.     base.OnClick(e);  
  24.     if (isShown)  
  25.     {  
  26.         _dropDownForm.Close();  
  27.         isShown = false;  
  28.     }  
  29. }  
  30.   
  31.   
  32. protected override void OnActivated(EventArgs e)  
  33. {  
  34.     base.OnActivated(e);  
  35.     if (isShown)  
  36.     {  
  37.         _dropDownForm.Close();  
  38.         isShown = false;  
  39.     }  
  40. }  
Step 7 : Debug your project and make any changes to your DropDownForm for design. 
 
DropDownControl with Tab 
 
  
 
 

Do you know what exactly is the RibbonBar?
 
Actually, it is a TabControl, we all know. You can see that old Microsoft Office packages, like 2007,2010 etc. provided the RibbonBar with DropDownControl. Here, we already created our own DropDownControl. Now, it's the time to display DropDownControl with Tab. This can be achieved by setting first tab of TabControl (means index 0) to fixed for File option. Perform all the above actions except Step 5. Replace Step 5 to the following:
 
Step 8 : Add TabControl to Form1 and Add MouseDown event to it. 
  1. private void tabControl1_MouseDown(object sender, MouseEventArgs e)  
  2. {  
  3.     Rectangle rect = tabControl1.GetTabRect(0);  
  4.   
  5.     if (rect.Contains(e.Location))  
  6.     {  
  7.         Size size = this.Size;  
  8.   
  9.         if (isShown == true)  
  10.         {  
  11.             _dropDownForm.Dispose();  
  12.             isShown = false;  
  13.   
  14.             tabControl1.SelectedIndex = OldSelTabIndex;  
  15.         }  
  16.         else if (isShown == false)  
  17.         {  
  18.             _dropDownForm = new TabDropDownForm(this);  
  19.             _dropDownForm.Location = new Point(this.Location.X + rect.X + 8, this.Location.Y + rect.Y + 30);  
  20.             tabControl1.SelectedIndex = OldSelTabIndex;  
  21.             _dropDownForm.Show();  
  22.             isShown = true;  
  23.             _dropDownForm.LostFocus += new EventHandler(_dropDownForm_LostFocus);  
  24.         }  
  25.     }  
  26.     else  
  27.     {  
  28.         OldSelTabIndex = tabControl1.SelectedIndex;  
  29.     }  
  30. }  
  31.   
  32.   
  33. private void _dropDownForm_LostFocus(object sender, EventArgs e)  
  34. {  
  35.     if (isShown)  
  36.     {  
  37.         _dropDownForm.Close();  
  38.         isShown = false;  
  39.     }  
  40. }         
Where OldSelTabIndex is the int type to hold the last selected index of tabControl, if the position of the mouse pressed button is within the first tab of tabControl, then it displays the DropDownForm; otherwise set OldSelTabIndex to tabControl SelectedIndex. Once DropDownForm is shown, set the isShown to true & set tabControl SelectedIndex to OldSelTabIndex. Download the source for better understanding.
 
Congratulations! You have created your own simple RibbonBar in C#.

You know, RibbonBar has a custom window. You can also create a customized window. By creating a customized window, you can add controls to the Top layer. Don't know how to create Custom Windows Forms ? Go to :

Up Next
    Ebook Download
    View all
    Learn
    View all