Search code examples
c#radio-buttontext-formatting

Windows Form Application Radio Button


it is my first time asking here. there is some kind of bug in my program where if i click the rbUnderline the label1 will be underlined but if i click another rb let's say rbBold(obv. the label will become bold) then click the rbUnderline the label wont be underlined again.

here is my code:

public Form1()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

            if (rbBold.Checked == true)
            {
                label1.Font = new Font(label1.Font, FontStyle.Bold);
            }
            else if (rbItalic.Checked == true)
            {
                label1.Font = new Font(label1.Font, FontStyle.Italic);
            }
            else if (rbUnderline.Checked == true)
            {
                label1.Font = new Font(label1.Font, FontStyle.Underline);
            }





        }

Designed.cs Code

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.rbBold = new System.Windows.Forms.RadioButton();
            this.rbItalic = new System.Windows.Forms.RadioButton();
            this.rbUnderline = new System.Windows.Forms.RadioButton();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(58, 65);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(158, 25);
            this.label1.TabIndex = 0;
            this.label1.Text = "SAMPLE TEXT";
            // 
            // rbBold
            // 
            this.rbBold.AutoSize = true;
            this.rbBold.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.rbBold.Location = new System.Drawing.Point(15, 20);
            this.rbBold.Name = "rbBold";
            this.rbBold.Size = new System.Drawing.Size(66, 28);
            this.rbBold.TabIndex = 1;
            this.rbBold.TabStop = true;
            this.rbBold.Text = "Bold";
            this.rbBold.UseVisualStyleBackColor = true;
            this.rbBold.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // rbItalic
            // 
            this.rbItalic.AutoSize = true;
            this.rbItalic.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.rbItalic.Location = new System.Drawing.Point(87, 20);
            this.rbItalic.Name = "rbItalic";
            this.rbItalic.Size = new System.Drawing.Size(64, 28);
            this.rbItalic.TabIndex = 2;
            this.rbItalic.TabStop = true;
            this.rbItalic.Text = "Italic";
            this.rbItalic.UseVisualStyleBackColor = true;
            // 
            // rbUnderline
            // 
            this.rbUnderline.AutoSize = true;
            this.rbUnderline.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.rbUnderline.Location = new System.Drawing.Point(157, 20);
            this.rbUnderline.Name = "rbUnderline";
            this.rbUnderline.Size = new System.Drawing.Size(110, 28);
            this.rbUnderline.TabIndex = 3;
            this.rbUnderline.TabStop = true;
            this.rbUnderline.Text = "Underline";
            this.rbUnderline.UseVisualStyleBackColor = true;
            this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(270, 104);
            this.Controls.Add(this.rbUnderline);
            this.Controls.Add(this.rbItalic);
            this.Controls.Add(this.rbBold);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.RadioButton rbBold;
        private System.Windows.Forms.RadioButton rbItalic;
        private System.Windows.Forms.RadioButton rbUnderline;
    }
}

Solution

  • It could be a race condition since radioButton1_CheckedChanged will be called twice. That's if all three of your radio buttons are using the same event handler for the CheckChanged event. The event handler will be called once for the button that was clicked and once for the button that gets unchecked.

    Try using 3 seperate if statements instead:

    if (rbBold.Checked == true)
    {
        label1.Font = new Font(label1.Font, FontStyle.Bold);
    }
    if (rbItalic.Checked == true)
    {
        label1.Font = new Font(label1.Font, FontStyle.Italic);
    }
    if (rbUnderline.Checked == true)
    {
        label1.Font = new Font(label1.Font, FontStyle.Underline);
    }   
    

    Edit: In response to your update you'll also need to change the following :

    this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
    

    to :

    this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);