Export (0) Print
Expand All

RadioButtonRenderer Class

Provides methods used to render an option button control (also known as a radio button) with or without visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.RadioButtonRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public NotInheritable Class RadioButtonRenderer

The RadioButtonRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberRenderMatchingApplicationStateGets or sets a value indicating whether the renderer uses the application state to determine rendering style.
Top

  NameDescription
Public methodStatic memberDrawParentBackgroundDraws the background of a control's parent in the specified area.
Public methodStatic memberDrawRadioButton(Graphics, Point, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and text formatting, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and image, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location; with the specified text, text formatting, and image; and with an optional focus rectangle.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberGetGlyphSizeReturns the size, in pixels, of the option button (also known as a radio button) glyph.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberIsBackgroundPartiallyTransparentIndicates whether the background of the option button (also known as a radio button) has semitransparent or alpha-blended pieces.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The RadioButtonRenderer class provides a set of static methods that can be used to render an option button control. Rendering a control refers to drawing the user interface of a control. To draw an option button, use one of the DrawRadioButton methods. These methods provide a variety of options, such as drawing text or an image with the option button.

If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawRadioButton will draw the option button with the current visual style. Otherwise, DrawRadioButton will draw the option button with the classic Windows style. This is useful if you are drawing a custom control that should automatically match the current visual style setting of the operating system.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.Button.RadioButton class. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to write a custom control that uses the DrawRadioButton method to draw an option button that responds to mouse clicks.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace RadioButtonRendererSample

    Class Form1
        Inherits Form

        Dim WithEvents button1 As Button

        Public Sub New()
            Dim RadioButton1 As New CustomRadioButton()
            button1 = New Button
            Me.button1.Location = New System.Drawing.Point(185, 231)
            Me.button1.Size = New System.Drawing.Size(105, 23)
            Me.button1.Text = "Toggle Styles"
            Controls.Add(RadioButton1)
            Controls.Add(button1)

            If Application.RenderWithVisualStyles Then 
                Me.Text = "Visual Styles Enabled" 
            Else 
                Me.Text = "Visual Styles Disabled" 
            End If 
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' If you do not call EnableVisualStyles below, then  
            ' RadioButtonRenderer.DrawRadioButton automatically detects  
            ' this and draws the radio button without visual styles.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 

        ' Match application style and toggle visual styles off 
        ' and on for the application. 
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
            Handles button1.Click

            RadioButtonRenderer.RenderMatchingApplicationState = True

            Application.VisualStyleState = _
                Application.VisualStyleState Xor _
                VisualStyleState.ClientAndNonClientAreasEnabled

            If Application.RenderWithVisualStyles Then 
                Me.Text = "Visual Styles Enabled" 
            Else 
                Me.Text = "Visual Styles Disabled" 
            End If 

        End Sub 
    End Class 

    Public Class CustomRadioButton
        Inherits Control

        Private textRectangleValue As New Rectangle()
        Private clicked As Boolean = False 
        Private state As RadioButtonState = RadioButtonState.UncheckedNormal

        Public Sub New()
            With Me
                .Location = New Point(50, 50)
                .Size = New Size(100, 20)
                .Text = "Click here"
                .Font = SystemFonts.IconTitleFont
            End With 
        End Sub 'New 

        ' Define the text bounds so that the text rectangle  
        ' does not include the radio button. 
        Public ReadOnly Property TextRectangle() As Rectangle
            Get 
                Using g As Graphics = Me.CreateGraphics()
                    With textRectangleValue
                        .X = Me.ClientRectangle.X + _
                            RadioButtonRenderer.GetGlyphSize(g, _
                            RadioButtonState.UncheckedNormal).Width
                        .Y = Me.ClientRectangle.Y
                        .Width = Me.ClientRectangle.Width - _
                            RadioButtonRenderer.GetGlyphSize(g, _
                            RadioButtonState.UncheckedNormal).Width
                        .Height = Me.ClientRectangle.Height
                    End With 
                End Using 
                Return textRectangleValue
            End Get 
        End Property 

        ' Draw the radio button in the current state. 
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
            RadioButtonRenderer.DrawRadioButton(e.Graphics, _
                Me.ClientRectangle.Location, TextRectangle, Me.Text, _
                Me.Font, clicked, state)
        End Sub 

        ' Draw the radio button in the checked or unchecked state. 
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)

            If Not clicked Then
                clicked = True 
                Me.Text = "Clicked!"
                state = RadioButtonState.CheckedPressed
                Invalidate()
            Else
                clicked = False 
                Me.Text = "Click here"
                state = RadioButtonState.UncheckedNormal
                Invalidate()
            End If 

        End Sub 

        ' Draw the radio button in the hot state.  
        Protected Overrides Sub OnMouseHover(ByVal e As EventArgs)
            MyBase.OnMouseHover(e)

            If clicked Then
                state = RadioButtonState.CheckedHot
            Else
                state = RadioButtonState.UncheckedHot
            End If
            Invalidate()
        End Sub 

        ' Draw the radio button in the hot state. 
        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            Me.OnMouseHover(e)
        End Sub 

        ' Draw the radio button in the unpressed state. 
        Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
            MyBase.OnMouseLeave(e)

            If clicked Then
                state = RadioButtonState.CheckedNormal
            Else
                state = RadioButtonState.UncheckedNormal
            End If
            Invalidate()
        End Sub 

    End Class 
End Namespace

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft