Windows Form Design in .NET-Interview Questions

No.of Views1641
By Pankaj Kumar Gupta  On 15 May 2011
emailbookmarkadd commentsprint

1. What is the use of Menus?

  • Menus expose functionality to your users by holding commands that are grouped by a common theme.
  • The MenuStrip control is new to this version of Visual Studio and the .NET Framework. With the control, you can easily create menus like those found in Microsoft Office.
  • The MenuStrip control supports the multiple-document interface (MDI) and menu merging, tool tips, and overflow. You can enhance the usability and readability of your menus by adding access keys, shortcut keys, check marks, images, and separator bars.
  • The MenuStrip control replaces and adds functionality to the MainMenu control; however, the MainMenu control is retained for backward compatibility and future use if you choose.

2. What are the ways to use the MenuStrip Control?

Use the MenuStrip control to:-

  • Create easily customized, commonly employed menus that support advanced user interface and layout features, such as text and image ordering and alignment, drag-and-drop operations, MDI, overflow, and alternate modes of accessing menu commands.
  • Support the typical appearance and behavior of the operating system.
  • Handle events consistently for all containers and contained items, in the same way you handle events for other controls.

3. How the menu shortcut works?

  • The focused control's ProcessCmdKey will be called first.
  • The Control class implementation of this method will check if there is a ContextMenu associated with the Control and if so let the context menu handle the shortcut.
  • If not handled, the ProcessCmdKey of the parent will be called recursively until the Form is reached.
  • When the Form is reached its MainMenu will be requested to handle the key
  • You can override ProcessCmdKey of any Control and interrupt the normal processing. Note that you can also override the ProcessCmdKey method is the MainMenu and ContextMenu classes (this is not documented) to override default processing.

4. How to know when a menu is Closed/Started so that we can refresh the status bar?

There are MenuComplete and MenuStart events in the Form that will inform you of the corresponding menu events in the form.

5. Is there an idle event which will get fired from which I could update my menu item's enabled state?

This used to be a very useful feature in MFC. You can do something similar with the System.Windows.Forms.Application.Idle event which will get fired when the app becomes Idle. Where you could parse through all the menu items and update their states.

6. What is easy way to create the "Window" menu that shows the list of child forms open?

Create a "Window" menu in you mdi parent form's menu and then drop another MenuItem into it, setting it's MdiList propety to true. It will then expand to show all the available mdi children during runtime.

7. How to work around the menu shortcuts from showing up incorrectly when we use Ctrl+Number?

When you assign Ctrl1, Ctrl2 etc as shorcuts for menuitems they show up as Ctrl+D1, Ctrl+D2. This can be worked around by creating and adding the menuitem through code as demonstrated below : [In C#]

//Create the menuitem 
MenuItem mymenuItem = new MenuItem(); 

mymenuItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1; 

//Add Event Handler for the menuitem 
mymenuItem.Click +=new EventHandler(this.mymenuItem_Click); 

//ShortCut Text to be displayed 
mymenuItem.Text = "My MenuItem" +"\t"+ "Ctrl+1"; 

//hide shortcut 
mymenuItem.ShowShortcut = false; 

//Add it to the bottom of the first menu 

8. How to add a Separator in a Menu?

By Using: contextmenu.MenuItems.Add("-");

9. How to enable the mnemonics (underline) to show when the application is launched?

Usually the underline appears only after you press the ALT Key, but you can enable it by changing the Operating System Settings. On Windows XP, Right Click Desktop to bring up the Display Properties Dialog and then choose Appearance tab and then the Effects Button and uncheck the checkbox "Hide Underlined letters for keyboard navigation until I press the ALT Key".

10. What is Multiple Document Interface (MDI)?

MDI is a popular interface because it allows you to have multiple documents (or forms) open in one application. Examples of MDI applications include Microsoft Word, Microsoft Excel, Microsoft PowerPoint®, and even the Visual Studio integrated development environment itself. Each application consists of one (or more) parent windows, each containing an MDI client area, the area where the child forms (or documents) will be displayed. Code you write displays as many instances of each of the child forms that you want displayed, and each child form can only be displayed within the confines of the parent window, this means you can't drag the child forms outside the MDI container.


Image Loading

11. What is Single Document Interface (SDI)?

Don't be misled: MDI is only one of several possible paradigms for creating a user interface. You can also create applications that display just a single form. They're easier to create, in fact. Those applications are called Single Document Interface (SDI) applications. Microsoft Windows® Notepad is an SDI application, and you can only open a single document at a time. (If you want multiple documents open, you simply run Notepad multiple times.) You are under no obligation to create your applications using the MDI paradigm. Even if you have multiple forms in your project, you can simply have each one as a stand-alone form, not contained by any parent form. 

12. What are the uses of MDI?

A MDI is used most often in applications where the user might like to have multiple forms or documents open concurrently. Word processing applications (like Microsoft Word), spreadsheet applications (like Microsoft Excel), and project manager applications (like Microsoft Project) are all good candidates for MDI applications. MDI is also handy when you have a large application, and you want to provide a simple mechanism for closing all the child forms when the user exits the application.

13. How to check if a child form is already displayed so we don't have two instances showing?

Here are two ways we can do this:-

Within the parent MDI form, use code such as this:-
1) First Solution-

MyChildForm childForm = null; 		              
     // MyChildForm is the one I'm looking for
     foreach(Form f in this.MdiChildren) 
          if(f is MyChildForm) 
               // found it 
               childForm = (MyChildForm) f; 

     if( childForm != null) 
          childForm = new MyChildForm(); 
          childForm.MdiParent = this; 

2). Second solution that implements a singleton pattern on the child form-
In the MDI Child form put this code in (where frmChildForm is the MDI child form we want to control)-

// Used for singleton pattern 
     static frmChildForm childForm; 
     public static ChildForm GetInstance 
          if (childForm == null) 
               childForm = new frmChildForm; 
          return childForm; 

In the Parent MDI form use the following code to call the child MDI form- 
     frmChildForm childForm = frmChildForm.GetInstance(); 
     childForm.MdiParent = this; 

The first time this code is called, the static GetInstance method will create and return an instance of the child form. Every other time this code is called, the GetInstance method will return the existing instance of the child from, stored in the static field childForm. If the child form instance is ever destroyed, the next time you call GetInstance, a new instance will be created and then used for its lifetime.

Also, if you need constructors or even overloaded constructors for your MDI Child Form, just add the needed parameters to the GetInstance function and pass them along to the class constructor.

14. How to determine that a MDI child form is added or removed from the MDI Container form?

MDIContainer forms have an MDIClient child window and it is to this MDIClient window that MDI child forms are parented. The MDIClient's ControlAdded/ControlRemoved events will be fired whenever a child form is added or removed. You can subscribe to these events and add the required processing code from within the handlers.

// From within the MDIContainer form, subscribe to the MDIClient's ControlAdded/ControlRemoved events 
foreach(Control ctrl in this.Controls) 
     if(ctrl.GetType() == typeof(MdiClient)) 
          ctrl.ControlAdded += new ControlEventHandler(this.MDIClient_ControlAdded); 
          ctrl.ControlRemoved += new ControlEventHandler(this.MDIClient_ControlRemoved); 

protected void MDIClient_ControlAdded(object sender, ControlEventArgs e) 
     Form childform = e.Control as Form; 
     Trace.WriteLine(String.Concat(childform.Text, " - MDI child form was added.")); 

protected void MDIClient_ControlRemoved(object sender, ControlEventArgs e) 
     Trace.WriteLine(String.Concat(e.Control.Text, " - MDI child form was removed.")); 

15. How to make the Child Form fill the entire MDI Client without being maximized?

Here is how it can be done. This takes into account all docked controls (including menus) in the mdi parent form.

private void FillActiveChildFormToClient() 
               Form child = this.ActiveMdiChild; 
               Rectangle mdiClientArea = Rectangle.Empty; 
               foreach(Control c in this.Controls) 
                    if(c is MdiClient) 
                         mdiClientArea = c.ClientRectangle; 
               child.Bounds = mdiClientArea; 

16. How to change the background of MDI Client Container?

The default behavior is to make the client container use the Control color from the Control panel. You can change this behavior by makingthe MDI Client container use the form's BackColor and Image. To do this, after the call to InitializeComponents(),

// set back color 
foreach(Control c in this.Controls) 
     if(c is MdiClient) 
          c.BackColor = this.BackColor; 
          c.BackgroundImage = this.BackgroundImage; 

17. What do you mean by Anchoring?

Use the Anchor property to define how a control is automatically resized as its parent control is resized. Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

We can anchor a control to one or more edges of its container. For example, if you have a Form with a Button whose Anchor property value is set to Top and Bottom, the Button is stretched to maintain the anchored distance to the top and bottom edges of the Form as the Height of the Form is increased.

Anchoring and docking are mutually exclusive.

18. How can we prevent resizing of Controls by the user, via Docking or Anchoring or Manual Resizing during design-time?

The best place to ensure a particular height/width for you control is in the SetBoundsCore override of your Control, as follows:

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) 
               int prefHeight = this.GetPreferredHeight(); 
               // Ensure that the height is atleast as big as prefHeight 
               if(height < prefHeight) 
                    height = prefHeight; 

               base.SetBoundsCore(x, y, width, height, specified); 

19. How can we programmatically manipulate the Anchor styles?

You can do this using the bitwise operators &, | and ^ ( And, Or and Xor (or &, Or, ^) in VB.Net). Here is code that will toggle label1 being anchored on the left. [In C#]

private void button1_Click(object sender, System.EventArgs e) 
          if ((label1.Anchor & AnchorStyles.Left) == 0) 
          {     //add it 
               label1.Anchor = label1.Anchor | AnchorStyles.Left; 
          else if ((label1.Anchor & AnchorStyles.Left) != 0) 
          {     //remove 
               label1.Anchor = label1.Anchor ^ AnchorStyles.Left; 

20. What is the use of OnPaint() method?

The OnPaint() method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting.

About Author
Pankaj Kumar Gupta
Occupation-Software Engineer
Company-Miri Infotech (P) Ltd, India
Member Type-Junior
Joined date-27 Oct 2010
- I am working with Miri Infotech (P) Ltd, India as Senior Software Engineer. - Having 5+ years experience in .NET Technologies (ASP.NET,,, SQL Server, XML, Web Services, MS Dynamics CRM, SharePoint). - I have masters M.Sc-Computer Science, M.Tech-Information Technology with Honours. - I have completed MCP, MCSD.NET, MCAD, MCDBA SQL Server, MCTS, MCPD-EAD - Microsoft Certified Professional & Technology Specialist - Obsessed in OOP, MVC, MVP style design and programming. - Designing and developing the client/server applications for a number of doimains. - Designing and implementing Business Planning Tools & Applications. - Good understanding of formal software engineering tools & technologies.
Other popular Interview Questions On .NET Frameworks
^ Scroll to Top