how do i have the checked dot of a radio button change color when clicked?

Aquitus 146 Reputation points
2022-02-13T00:53:27.557+00:00

i want my radio button to be clicked and it gets checked as normal but if you click it again then instead of being unchecked it changes color.
best i could find was this link
but i didnt have any luck implementing the code myself
in case its easier a checkbox that does the same thing is also OK in my book

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-02-15T21:34:35.38+00:00

    You can override Paint event

    A basic sample, to be improved :

    174584-coloredradiobutton.gif

    Public Class MyColoredRadioButton  
        Inherits RadioButton  
      
        Private m_CheckedColor As Color  
        Private m_UncheckedColor As Color  
      
        Public Sub New()  
            m_CheckedColor = Color.Blue  
            m_UncheckedColor = Color.White  
        End Sub  
      
        Public Property CheckedColor() As Color  
            Get  
                Return m_CheckedColor  
            End Get  
            Set(value As Color)  
                m_CheckedColor = value  
            End Set  
        End Property  
      
        Public Property UncheckedColor() As Color  
            Get  
                Return m_UncheckedColor  
            End Get  
            Set(value As Color)  
                m_UncheckedColor = value  
            End Set  
        End Property  
      
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)  
            MyBase.OnPaint(e)  
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias  
            Dim rectRadio As New RectangleF(0, (ClientRectangle.Height - 13) / 2 - 1, 13, 13)  
            e.Graphics.DrawEllipse(Pens.Black, rectRadio)  
            'rectRadio.Offset(New Point(1, 1))  
            rectRadio.Inflate(New Size(-3, -3))  
            If Me.Checked Then  
                e.Graphics.FillEllipse(New SolidBrush(CheckedColor), rectRadio)  
            Else  
                e.Graphics.FillEllipse(New SolidBrush(UncheckedColor), rectRadio)  
            End If  
        End Sub  
    End Class  
    

    To initialize colors for the 2 first Radio Buttons in the test :

        MyColoredRadioButton1.CheckedColor = Color.Red  
        MyColoredRadioButton1.UncheckedColor = Color.Green  
    
        MyColoredRadioButton2.CheckedColor = Color.Purple  
        MyColoredRadioButton2.UncheckedColor = Color.Orange  
    
    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2022-02-14T03:30:50.343+00:00

    Hi @Aquitus ,

    but i didnt have any luck implementing the code myself

    Create the following class from your link.

    Public Class ColoredRadioButton  
        Inherits RadioButton  
      
        Private m_CheckedColor As Color  
        Public Property CheckedColor() As Color  
            Get  
                Return m_CheckedColor  
            End Get  
            Set(ByVal Value As Color)  
                m_CheckedColor = Value  
                SetColor()  
            End Set  
        End Property  
      
        Private m_UncheckedColor As Color  
        Public Property UncheckedColor() As Color  
            Get  
                Return m_UncheckedColor  
            End Get  
            Set(ByVal Value As Color)  
                m_UncheckedColor = Value  
                SetColor()  
            End Set  
        End Property  
      
        Private Sub ColoredRadioButton_CheckedChanged(ByVal _  
            sender As Object, ByVal e As System.EventArgs) _  
            Handles MyBase.CheckedChanged  
            SetColor()  
        End Sub  
      
        Private Sub SetColor()  
            If Me.Checked Then  
                Me.ForeColor = m_CheckedColor  
            Else  
                Me.ForeColor = m_UncheckedColor  
            End If  
        End Sub  
    End Class  
    

    Then build the project and you can see 'ColoredRadioButton' in your 'Toolbox'.
    You need to set the 'UncheckedColor' or 'CheckedColor' in your code.

            ColoredRadioButton1.UncheckedColor = Color.Red  
            ColoredRadioButton1.CheckedColor = Color.Green  
    

    Result of my test.
    Unchecked: 173963-1.png
    Checked: 173944-2.png
    If I have any misunderstanding, please let me know.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. LesHay 7,141 Reputation points
    2022-02-13T09:03:58.127+00:00

    Hi

    RadioButtons are usually used in multiples to allow the user to select from a list of options etc. In that case, the RadioButtons automatically change to reflect a click on one of them.

    However, if you want to use a single RadioButton, then put this in your code:

    In the Form Load event handler, put RadioButton1.Checked=True

    Add this Sub to tour code:

      Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
        Static ch As Boolean = False
        RadioButton1.Checked = ch
        ch = Not ch
      End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.