Export (0) Print
Expand All

How to: Configure MenuStrip Check Margins and Image Margins

You can customize a MenuStrip by setting the ShowImageMargin and ShowCheckMargin properties in various combinations.

The following code example demonstrates how to set and customize the ContextMenuStrip check margins and image margins. The procedure is the same for a ContextMenuStrip or a MenuStrip.

' This code example demonstrates how to set the check 
' and image margins for a ToolStripMenuItem. 
Class Form5
   Inherits Form

   Public Sub New()
      ' Size the form to show three wide menu items. 
      Me.Width = 500
      Me.Text = "ToolStripContextMenuStrip: Image and Check Margins" 

      ' Create a new MenuStrip control. 
      Dim ms As New MenuStrip()

      ' Create the ToolStripMenuItems for the MenuStrip control. 
      Dim bothMargins As New ToolStripMenuItem("BothMargins")
      Dim imageMarginOnly As New ToolStripMenuItem("ImageMargin")
      Dim checkMarginOnly As New ToolStripMenuItem("CheckMargin")
      Dim noMargins As New ToolStripMenuItem("NoMargins")

      ' Customize the DropDowns menus. 
      ' This ToolStripMenuItem has an image margin  
      ' and a check margin.
      bothMargins.DropDown = CreateCheckImageContextMenuStrip()
      CType(bothMargins.DropDown, ContextMenuStrip).ShowImageMargin = True 
      CType(bothMargins.DropDown, ContextMenuStrip).ShowCheckMargin = True 

      ' This ToolStripMenuItem has only an image margin.
      imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
      CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = True 
      CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = False 

      ' This ToolStripMenuItem has only a check margin.
      checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
      CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = False 
      CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = True 

      ' This ToolStripMenuItem has no image and no check margin.
      noMargins.DropDown = CreateCheckImageContextMenuStrip()
      CType(noMargins.DropDown, ContextMenuStrip).ShowImageMargin = False 
      CType(noMargins.DropDown, ContextMenuStrip).ShowCheckMargin = False 

      ' Populate the MenuStrip control with the ToolStripMenuItems.

      ' Dock the MenuStrip control to the top of the form.
      ms.Dock = DockStyle.Top

      ' Add the MenuStrip control to the controls collection last. 
      ' This is important for correct placement in the z-order. 
    End Sub 

   ' This utility method creates a Bitmap for use in  
   ' a ToolStripMenuItem's image margin. 
    Friend Function CreateSampleBitmap() As Bitmap

        ' The Bitmap is a smiley face. 
        Dim sampleBitmap As New Bitmap(32, 32)
        Dim g As Graphics = Graphics.FromImage(sampleBitmap)

        Dim p As New Pen(ProfessionalColors.ButtonPressedBorder)
            ' Set the Pen width.
            p.Width = 4

            ' Set up the mouth geometry. 
            Dim curvePoints() As Point = _
            {New Point(4, 14), New Point(16, 24), New Point(28, 14)}

            ' Draw the mouth.
            g.DrawCurve(p, curvePoints)

            ' Draw the eyes.
            g.DrawEllipse(p, New Rectangle(New Point(7, 4), New Size(3, 3)))
            g.DrawEllipse(p, New Rectangle(New Point(22, 4), New Size(3, 3)))
        End Try 

        Return sampleBitmap
    End Function 

   ' This utility method creates a ContextMenuStrip control 
   ' that has four ToolStripMenuItems showing the four  
   ' possible combinations of image and check margins. 
   Friend Function CreateCheckImageContextMenuStrip() As ContextMenuStrip
      ' Create a new ContextMenuStrip control. 
      Dim checkImageContextMenuStrip As New ContextMenuStrip()

      ' Create a ToolStripMenuItem with a 
      ' check margin and an image margin. 
      Dim yesCheckYesImage As New ToolStripMenuItem("Check, Image")
      yesCheckYesImage.Checked = True
      yesCheckYesImage.Image = CreateSampleBitmap()

      ' Create a ToolStripMenuItem with no 
      ' check margin and with an image margin. 
      Dim noCheckYesImage As New ToolStripMenuItem("No Check, Image")
      noCheckYesImage.Checked = False
      noCheckYesImage.Image = CreateSampleBitmap()

      ' Create a ToolStripMenuItem with a 
      ' check margin and without an image margin. 
      Dim yesCheckNoImage As New ToolStripMenuItem("Check, No Image")
      yesCheckNoImage.Checked = True 

      ' Create a ToolStripMenuItem with no 
      ' check margin and no image margin. 
      Dim noCheckNoImage As New ToolStripMenuItem("No Check, No Image")
      noCheckNoImage.Checked = False 

      ' Add the ToolStripMenuItems to the ContextMenuStrip control.

      Return checkImageContextMenuStrip
    End Function 
End Class

This example requires:

  • References to the System, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

© 2015 Microsoft