Skip to content

Adding User Controls to a Form

by on March 1, 2014

Currently still in the design phase of creating my game.

Got most of the classes fleshed out with fields now and created class diagrams for the different elements of the game.

I decided to take a break from the class designing and start work on the actual GUI itself. My previous attempt I used a SplitPanel container to work inside of. This sat over the main form. However, I wanted to work out how to embed my UserControl files directly into the main form without needing any containers.

After a little help from Google, I figured it out.

Here’s the basic code:


UserControl ob = new UserControl();
this.Controls.Add(ob);

I wanted to implement this into a menu item. So when the user clicks “New Game” from the File menu, it adds the user control to the form:


private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
 {
 //Start New Game from menu
 //Add user control RollNewCharacter.cs to the form to start the Character Creator
 RollNewCharacter rollchar = new RollNewCharacter();
 this.Controls.Add(rollchar);
 }

However, there’s a pitfall I’ve found. If you have a menu strip docked at the top of your main form, then you need to adjust the user control file to prevent it from being covered over by the menu. I’d like to find a way for it to show properly without needing to alter the actual layout of the user control itself. However, I may end up just creating a container to display the items inside of like I had in my previous design.

One Comment
  1. After my initial post on this, I spent probably several hours searching for a way to resolve this to no avail. So i gave in and just adjusted the layout of the user control design page so the menu bar wouldn’t cover up the controls on screen.

Leave a comment

NE1 Atoll

The Official blog of NE1 Games

Selenium for .Net using C# language

Adventures in Coding, gaming and other fun things in my life

Coded UI 101 - Understanding Coded UI Tests

Adventures in Coding, gaming and other fun things in my life