Custom Radio Button C#

Hemanth B 886 Reputation points
2022-02-13T08:16:03.21+00:00

Hi I created a custom radio button in WinForms.
The problem is this:
173740-image.png

As you may know, if a radio button is checked, then the other radio buttons under the same parent control are unchecked automatically.
This is the source code:

using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Drawing;  
    using System.Data;  
    using System.Windows.Forms;  
    namespace Test  
    {  
        class MTARadioButton : System.Windows.Forms.RadioButton  
        {  
            public MTARadioButton()  
            {  
                lb.BringToFront();  
                lb.Text = "";  
                lb.Font = new Font("Segoe Fluent Icons", 16, FontStyle.Regular);  
                lb.Click += Lb_Click;  
                this.Controls.Add(lb);  
                this.CheckedChanged += CustomCheckBox_CheckedChanged;  
            }  
            private string _text = "MTACheckBox1";  
            public string RadioButtonText  
            {  
                get { return _text; }  
                set  
                {  
                    _text = value;  
                    lb2.Text = _text;  
                }  
            }  
      
            Label lb2 = new Label();  
            private void CustomCheckBox_CheckedChanged(object? sender, EventArgs e)  
            {  
                if (this.Checked == false)  
                {  
                    lb.ForeColor = Color.Black;  
                    lb.Text = "";  
                }  
                else  
                {  
                    lb.ForeColor = Color.DodgerBlue;  
                    lb.Text = "";  
                }  
            }  
      
            Label lb = new Label();  
            private void Lb_Click(object? sender, EventArgs e)  
            {  
                    lb.ForeColor = Color.DodgerBlue;  
                    lb.Text = "";  
            }  
        }  
    }  
  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Reza-Ameri 45,666 Reputation points Volunteer Moderator
    2022-02-13T16:40:30.657+00:00

    You should check your UI view code for example XAML , Razor , HTML ,... and make sure they are in the same group.
    Are you observing this behavior, if you are not using above code?
    In addition, you may use the object name , so instead of this.Checked you may try RadioButton1 for example and set it to be checked or not.

    0 comments No comments

  2. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2022-02-13T17:52:45.44+00:00

    Consider instead using a library, Material Skin, GitHub, NuGet.

    173780-demo.gif

    0 comments No comments

Your answer

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