C# – Creating a Navbar through User Controls

Share on WhatsApp
Share via email Share
Overview:

User controls can be added to Windows Form or Winform application very easily. In this article, we will try to make simple navigation bar using Custom control and some piece of code. So, lets begin!

STEP 1: Create a new Winform application and name it whatever you like.
STEP 2: Go to project form main menu and click on ‘Add User Control’.

Add User Control

Add User Control

 

Name it Navbar.cs or any name that suites you and click “Add”, you should have the following screen.

 

User Control

 

This is your custom control which is empty right now. You can add and merge .NET’s predefined controls and create a new control for yourself.

STEP 3: Creating Buttons

Open the properties panel and go to events sections, no add an OnLoad event handler for Load event. We will generate 10 random buttons and add them to our custom control using this code. You should have the following screen:

 

C# OnLoad event

C# OnLoad event

 

Add the following code to generate some buttons:

STEP 4: Adding our control to Form1

Save this custom control and close the code/designer. No go to Form1 and you should see your Navbar User Control in the Toolbox. Drag and draw a new Navbar in your Form1.

NOTE: If you don’t see this control in toolbox, unload/reload your solution for this project. It bugs sometime.

 

Adding our control

 

Set the Dock to top in properties and give it some height. Now hit F5 to compile execute and you should see the following:

 

Adding User Control

 

Well done! You have created and added a User Control to your WinForm application. You can create as many User  Control as you want and add them to your WinForms. This makes it very easy for working with repetitive designs like same navigation bar on multiple forms. But with this, all you need to do is drag a User Control from toolbox and you’re good to go.

Remember, whenever you need to do changes to the design of your control, you edit the Navbar.cs or your control and save it. On rebuild, it will update on all forms automatically.

 

Conclusion:

User controls gives you greater design modification and allow you to create out of the box design as you like. In the next upcoming article, I will cover more advanced topics related to this like making controls responsive, creating complex controls and such.

I hope you like, please share it with others and let everyone know about User controls.

 

Share on WhatsApp
Share via email Share

Leave a Reply

Your email address will not be published.