Windows窗体应用程序单选按钮

时间:2014-08-27 18:34:59

标签: c# radio-button text-formatting

这是我第一次问这里。在我的程序中有一些错误,如果我点击rbUnderline,label1将加下划线但是如果我点击另一个rb让我们说rbBold(obv。标签将变为粗体)然后点击rbUnderline标签不会再加下划线。 / p>

这是我的代码:

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代码

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;
    }
}

2 个答案:

答案 0 :(得分:0)

您应该将单选按钮组合在一起,将它们放在面板或组合框等容器中,您的问题应该消失。

答案 1 :(得分:0)

这可能是竞争条件,因为radioButton1_CheckedChanged将被调用两次。如果所有三个单选按钮都使用CheckChanged事件的相同事件处理程序。对于单击的按钮,将调用一次事件处理程序,对于未选中的按钮,将调用一次事件处理程序。

尝试使用3个单独的if语句:

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);
}   

编辑:为了响应您的更新,您还需要更改以下内容:

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

到:

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