Export (0) Print
Expand All

How to: Set the ToolStrip Renderer at Run Time

You can customize the appearance of your ToolStrip control by creating a custom ProfessionalColorTable class.

The following code example demonstrates how to create a custom ProfessionalColorTable class. This class defines gradients for a MenuStrip and a ToolStrip control.

To use this code example, compile and run the application, and then click Change Colors to apply the gradients defined in the custom ProfessionalColorTable class.

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing


...


' This code example demonstrates how to use a ProfessionalRenderer 
' to define custom professional colors at runtime. 
Class Form2
   Inherits Form

   Public Sub New()
      ' Create a new ToolStrip control. 
      Dim ts As New ToolStrip()

      ' Populate the ToolStrip control.
      ts.Items.Add("Apples")
      ts.Items.Add("Oranges")
      ts.Items.Add("Pears")
      ts.Items.Add("Change Colors", Nothing, New EventHandler(AddressOf ChangeColors_Click))

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

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

      ' Add the top-level menu items.
      ms.Items.Add("File")
      ms.Items.Add("Edit")
      ms.Items.Add("View")
      ms.Items.Add("Window")

      ' Add the ToolStrip to Controls collection. 
      Me.Controls.Add(ts)

      ' Add the MenuStrip control last. 
      ' This is important for correct placement in the z-order. 
      Me.Controls.Add(ms)
    End Sub 

   ' This event handler is invoked when the "Change colors" 
   ' ToolStripItem is clicked. It assigns the Renderer 
   ' property for the ToolStrip control. 
    Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
        ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
    End Sub 
End Class 

' This class defines the gradient colors for  
' the MenuStrip and the ToolStrip. 
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get 
         Return Color.BlueViolet
      End Get  
   End Property 

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get 
         Return Color.CadetBlue
      End Get  
   End Property 

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get 
         Return Color.CornflowerBlue
      End Get  
   End Property 

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get 
         Return Color.Salmon
      End Get  
   End Property 

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get 
         Return Color.OrangeRed
      End Get 
    End Property 

End Class

Defining a Custom ProfessionalColorTable class

The custom gradients are defined in the CustomProfessionalColors class.

' This class defines the gradient colors for  
' the MenuStrip and the ToolStrip. 
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get 
         Return Color.BlueViolet
      End Get  
   End Property 

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get 
         Return Color.CadetBlue
      End Get  
   End Property 

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get 
         Return Color.CornflowerBlue
      End Get  
   End Property 

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get 
         Return Color.Salmon
      End Get  
   End Property 

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get 
         Return Color.OrangeRed
      End Get 
    End Property 

End Class

Assigning a Custom Renderer

Create a new ToolStripProfessionalRenderer with a CustomProfessionalColors class, and assign it to the ToolStripManager.Renderer property.

' This event handler is invoked when the "Change colors" 
' ToolStripItem is clicked. It assigns the Renderer 
' property for the ToolStrip control. 
 Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
     ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
 End Sub

This example requires:

  • References to the System.Design, 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.

Show:
© 2014 Microsoft